-
Notifications
You must be signed in to change notification settings - Fork 13
/
OsmApi.pm
485 lines (437 loc) · 14.1 KB
/
OsmApi.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#!/usr/bin/perl
# OsmApi.pm
# ---------
#
# Implements OSM API connectivity
#
# Part of the "osmtools" suite of programs
# Originally written by Frederik Ramm <[email protected]>; public domain
package OsmApi;
use strict;
use warnings;
use Module::Load::Conditional qw[can_load];
use LWP::UserAgent;
use MIME::Base64 qw(encode_base64 encode_base64url);
use HTTP::Cookies;
use URI::Escape;
use Digest::SHA qw(sha256);
our $prefs_path;
our $prefs;
our $prefs_eol = 1;
our $ua;
our $agent;
our $dummy;
our $noversion;
our $cookie_jar;
our $auth_token;
our $oauth2_client_ids = {
"api06.dev.openstreetmap.org:443" => "FEGTbR13GBJ8o3Z1FJLFUqcgMYrvmwzEbN2mciMz528",
"master.apis.dev.openstreetmap.org:443" => "FEGTbR13GBJ8o3Z1FJLFUqcgMYrvmwzEbN2mciMz528",
"www.openstreetmap.org:443" => "j2hkpmK8D3XRgXqU-X0fyaIZsehbTUdfZDE4eg-7JJA",
"api.openstreetmap.org:443" => "j2hkpmK8D3XRgXqU-X0fyaIZsehbTUdfZDE4eg-7JJA",
"www.openhistoricalmap.org:443" => "JH6N562wvXBEEntUsYIhXVcfiizLSQvU6Hgw7nVIkVg"
};
our $translated_weburls = {
"https://api.openstreetmap.org/" => "https://www.openstreetmap.org/"
};
INIT
{
if (can_load(modules => {'File::HomeDir' => undef}))
{
$prefs_path = File::HomeDir::home()."/.osmtoolsrc";
}
else
{
$prefs_path = $ENV{HOME}."/.osmtoolsrc";
}
$prefs = { "dryrun" => 1 };
open (PREFS, $prefs_path) or die "cannot open $prefs_path";
while(<PREFS>)
{
if (/^([^=]*)=(.*)/)
{
$prefs->{$1} = $2;
}
$prefs_eol = substr ($_, -1) eq "\n";
}
close (PREFS);
# override user name and password from environment if given
$prefs->{username} = $ENV{OSMTOOLS_USERNAME} if (defined($ENV{OSMTOOLS_USERNAME}));
$prefs->{password} = $ENV{OSMTOOLS_PASSWORD} if (defined($ENV{OSMTOOLS_PASSWORD}));
foreach my $required("apiurl")
{
die "$prefs_path does not have $required" unless defined($prefs->{$required});
}
if (!defined($prefs->{instance}))
{
$prefs->{instance} = sprintf "%010x", $$ * rand(100000000);
append_pref("instance");
}
$prefs->{apiurl} =~ m!(https?)://([^/]+)/!;
my $protocol = $1;
my $host = $2;
if ($host !~ /:/)
{
$host .= sprintf ":%d", ($protocol eq "https") ? 443 : 80;
}
$ua = LWP::UserAgent->new;
my $revision = '$Revision: 30253 $';
my $revno = 0;
$revno = $1 if ($revision =~ /:\s*(\d+)/);
$agent = "osmtools/$revno ($^O, ".$prefs->{instance}.")";
$ua->agent($agent);
$ua->timeout(600);
push @{$ua->requests_redirectable}, 'POST';
push @{$ua->requests_redirectable}, 'PUT';
$prefs->{debug} = $prefs->{dryrun} unless (defined($prefs->{debug}));
$dummy = HTTP::Response->new(200);
my $weburl = $prefs->{'apiurl'};
if ($weburl =~ /(.*\/)api\/0.6\//)
{
$weburl = $1;
}
if (defined($translated_weburls->{$weburl}))
{
$weburl = $translated_weburls->{$weburl};
}
$prefs->{'weburl'} = $weburl;
if (!defined($prefs->{oauth2_client_id}) && defined($oauth2_client_ids->{$host}))
{
$prefs->{oauth2_client_id} = $oauth2_client_ids->{$host};
}
}
# API subs
# --------
sub get
{
my $url = shift;
my $body = shift;
my $privileged = shift;
my $req = HTTP::Request->new(GET => $prefs->{apiurl}.$url);
return run_api_request($req, $privileged);
}
sub exists
{
my $url = shift;
my $body = shift;
my $privileged = shift;
my $req = HTTP::Request->new(HEAD => $prefs->{apiurl}.$url);
my $resp = run_api_request($req, $privileged);
return($resp->code < 400);
}
sub put
{
my $url = shift;
my $body = shift;
my $privileged = shift;
return dummylog("PUT", $url, $body) if ($prefs->{dryrun});
my $req = HTTP::Request->new(PUT => $prefs->{apiurl}.$url);
$req->header("Content-type" => "text/xml");
$req->content($body) if defined($body);
return run_api_request($req, $privileged);
}
sub post
{
my $url = shift;
my $body = shift;
my $privileged = shift;
return dummylog("POST", $url, $body) if ($prefs->{dryrun});
my $req = HTTP::Request->new(POST => $prefs->{apiurl}.$url);
$req->content($body) if defined($body);
# some not-proper-API-calls will expect HTTP form POST data;
# try to determine magically whether we have an XML or form message.
if (defined($body) && ($body !~ /^</))
{
$req->header("Content-type" => "application/x-www-form-urlencoded");
}
else
{
$req->header("Content-type" => "text/xml");
}
return run_api_request($req, $privileged);
}
sub post_multipart
{
use HTTP::Request::Common;
my $url = shift;
my $req = HTTP::Request::Common::POST($prefs->{apiurl}.$url, @_, 'Content_Type' => 'form-data');
return run_api_request($req);
}
sub delete
{
my $url = shift;
my $body = shift;
my $privileged = shift;
return dummylog("DELETE", $url, $body) if ($prefs->{dryrun});
my $req = HTTP::Request->new(DELETE => $prefs->{apiurl}.$url);
$req->header("Content-type" => "text/xml");
$req->content($body) if defined($body);
return run_api_request($req, $privileged);
}
# Web subs
# --------
sub login
{
require_username_and_password();
$ua->cookie_jar($cookie_jar = HTTP::Cookies->new());
my $req = HTTP::Request->new(GET => $prefs->{weburl}."login");
my $resp = $ua->request($req);
debuglog($req, $resp) if ($prefs->{"debug"});
die unless($resp->is_success);
my $cont = $resp->content;
die unless($cont =~ /<meta name="csrf-token" content="(.*)" \/>/);
$auth_token = $1;
$req = HTTP::Request->new(POST => $prefs->{weburl}."login");
$req->content(
"authenticity_token=" . uri_escape($auth_token) .
"&referer=%2F".
"&openid_url=".
"&utf8=%E2%9C%93".
"&commit=Login".
"&username=". uri_escape($prefs->{'username'}).
"&password=". uri_escape($prefs->{'password'}));
$req->header("Content-type" => "application/x-www-form-urlencoded");
$req->header("Content-length" => length($req->content));
$resp = $ua->request($req);
debuglog($req, $resp) if ($prefs->{"debug"});
die unless($resp->content =~ /<head[^>]* data-user="(\d+)"/);
print("logged in as user $1\n");
}
sub load_web
{
my $form = shift;
login() unless defined($cookie_jar);
my $resp = $ua->get($prefs->{'weburl'}.$form);
return undef unless($resp->is_success);
my $cont = $resp->content;
return undef unless($cont =~ /<meta name="csrf-token" content="(.*)" \/>/);
$auth_token = $1;
return 1;
}
# modified form of post method, that uses the web base URL
# and also automatically adds a potentially existing auth token
# to form post content.
sub post_web
{
my $url = shift;
my $body = shift;
return dummylog("POST", $url, $body) if ($prefs->{dryrun});
login() unless defined($cookie_jar);
my $req = HTTP::Request->new(POST => $prefs->{weburl}.$url);
if (defined($auth_token))
{
$body .= "&" if defined($body);
$body .= "authenticity_token=".uri_escape($auth_token);
undef $auth_token;
}
$req->content($body) if defined($body);
$req->header("Content-type" => "application/x-www-form-urlencoded");
my $resp = repeat($req);
$auth_token = $1 if ($resp->content =~ /<meta name="csrf-token" content="(.*)" \/>/);
debuglog($req, $resp) if ($prefs->{"debug"});
return $resp;
}
# Utility subs
# ------------
sub weburl
{
my $path = shift || '';
return $prefs->{'weburl'} . $path;
}
sub append_pref
{
my $pref_name = shift;
open(PREFS, ">>$prefs_path");
printf PREFS "\n" unless $prefs_eol;
printf PREFS "$pref_name=".$prefs->{$pref_name};
close(PREFS);
$prefs_eol = 0;
}
sub run_api_request
{
my $req = shift;
my $privileged = shift;
add_credentials($req, $privileged);
my $resp = repeat($req);
debuglog($req, $resp) if ($prefs->{"debug"});
return $resp;
}
sub add_credentials
{
my $req = shift;
my $privileged = shift;
if (defined($prefs->{oauth2_client_id}) && $prefs->{oauth2_client_id})
{
my $token = read_existing_oauth2_token($privileged);
if (!$token)
{
$token = request_oauth2_token("oauth2_token");
}
$req->header("Authorization" => "Bearer $token");
}
else
{
require_username_and_password($privileged);
$req->header("Authorization" => "Basic ".encode_base64($prefs->{username}.":".$prefs->{password}));
}
}
sub require_username_and_password
{
if (can_load(modules => {'Term::ReadKey' => undef}, autoload => 1))
{
if (defined($prefs->{username}))
{
# only print user name if we're about to read password interactively
unless (defined($prefs->{password}))
{
print 'User name: ' . $prefs->{username} . "\n"
}
}
else
{
# read user name from terminal if not set
print 'User name: ';
$prefs->{username} = $1 if (ReadLine(0) =~ /^(.*)\n$/);
print "\n";
}
unless (defined($prefs->{password}))
{
# read password from terminal if not set
print 'Password: ';
ReadMode('noecho');
$prefs->{password} = $1 if (ReadLine(0) =~ /^(.*)\n$/);
ReadMode('restore');
print "\n";
}
}
foreach my $required("username","password")
{
die "$prefs_path does not have $required" unless defined($prefs->{$required});
}
}
sub read_existing_oauth2_token
{
my $privileged = shift;
if (!$privileged && defined($prefs->{"oauth2_token_secondary"}))
{
return $prefs->{"oauth2_token_secondary"};
}
if (defined($prefs->{"oauth2_token"}))
{
return $prefs->{"oauth2_token"};
}
return undef;
}
sub introspect_existing_oauth2_token
{
my $privileged = shift;
my $req = HTTP::Request->new(POST => $prefs->{weburl}."oauth2/introspect");
$req->content(
"client_id=" . uri_escape($prefs->{oauth2_client_id}) .
"&token=" . uri_escape(read_existing_oauth2_token($privileged)));
$req->header("Content-type" => "application/x-www-form-urlencoded");
$req->header("Content-length" => length($req->content));
my $resp = $ua->request($req);
debuglog($req, $resp) if ($prefs->{"debug"});
return $resp;
}
sub check_oauth2_token
{
my $token_name = shift;
return defined($prefs->{$token_name});
}
sub request_oauth2_token
{
die "oauth2 token request requires typing/pasting a code, but STDIN is busy with piped input\ntry running 'tokens.pl request' first to get oauth2 tokens" unless -t STDIN;
die "Requesting oauth2 tokens requires 'oauth2_client_id' to be set in .osmtoolsrc for custom 'apiurl'." unless (defined($prefs->{oauth2_client_id}) && $prefs->{oauth2_client_id});
my ($token_name, $scope, $no_scope) = @_;
$scope = "read_prefs write_api write_notes read_gpx write_gpx write_redactions" unless defined($scope);
if (defined($no_scope))
{
my %excluded_scopes = map { $_ => 1 } split /\s+/, $no_scope;
$scope = join " ", grep { !$excluded_scopes{$_} } split /\s+/, $scope;
}
my $code_verifier_bytes;
if (can_load(modules => {'Bytes::Random::Secure' => undef}))
{
$code_verifier_bytes = Bytes::Random::Secure::random_bytes(48);
}
else
{
$code_verifier_bytes = join "", map({ chr(int(rand() * 256)) } 1..48);
print "WARNING: Using unsecure random number generator to generate PKCE code verifier, consider installing Bytes::Random::Secure module\n";
}
my $redirect_uri = "urn:ietf:wg:oauth:2.0:oob";
my $code_verifier = encode_base64url $code_verifier_bytes;
my $code_challenge = encode_base64url sha256($code_verifier);
my $request_code_url = "$prefs->{weburl}oauth2/authorize?" .
"client_id=" . uri_escape($prefs->{oauth2_client_id}) .
"&redirect_uri=" . uri_escape($redirect_uri) .
"&scope=" . uri_escape($scope) .
"&response_type=code" .
"&code_challenge=" . uri_escape($code_challenge) .
"&code_challenge_method=S256";
print "Open the following url:\n$request_code_url\n\n";
print "Copy the code here: ";
my $code;
while ($code = <STDIN>)
{
chomp $code;
last if $code ne "";
}
my $req = HTTP::Request->new(POST => $prefs->{weburl}."oauth2/token");
$req->content(
"client_id=" . uri_escape($prefs->{oauth2_client_id}) .
"&redirect_uri=" . uri_escape($redirect_uri) .
"&grant_type=authorization_code" .
"&code=" . uri_escape($code) .
"&code_verifier=" . uri_escape($code_verifier));
$req->header("Content-type" => "application/x-www-form-urlencoded");
$req->header("Content-length" => length($req->content));
my $resp = $ua->request($req);
debuglog($req, $resp) if ($prefs->{"debug"});
die "no token in code exchange response" unless($resp->content =~ /"access_token":"([^"]+)"/);
my $token = $1;
$prefs->{$token_name} = $token;
append_pref($token_name);
return $token;
}
sub repeat
{
my $req = shift;
my $resp;
for (my $i=0; $i<3; $i++)
{
$resp = $ua->request($req);
return $resp unless ($resp->code == 502 || $resp->code == 500);
sleep 1;
}
return $resp;
}
sub debuglog
{
my ($request, $response) = @_;
printf STDERR "%s %s... %s %s (%db)\n",
$request->method(),
$request->uri(),
$response->code(),
$response->message(),
length($response->content());
print STDERR "Request Headers:\n".$request->headers_as_string()."\n" if ($prefs->{"debug_request_headers"});
print STDERR "Request:\n".$request->content()."\n" if ($prefs->{"debug_request_body"});
print STDERR "Response Headers:\n".$response->headers_as_string()."\n" if ($prefs->{"debug_response_headers"});
print STDERR "Response:\n".$response->content()."\n" if ($prefs->{"debug_response_body"});
}
sub dummylog
{
my ($method, $url, $body) = @_;
print STDERR "$method $url\n";
print STDERR "$body\n\n" if defined($body);
return $dummy;
}
sub set_timeout
{
my $to = shift;
$ua->timeout($to);
}
1;