-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtokens.pl
212 lines (189 loc) · 5.76 KB
/
tokens.pl
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
#!/usr/bin/perl
use strict;
use FindBin;
use lib "$FindBin::Bin/lib";
use Getopt::Long;
use OsmApi;
if ($ARGV[0] eq "request")
{
my $primary = 1;
my $secondary = 1;
my $scope;
my $add_scope;
my $remove_scope;
my $no_scope;
my $correct_options = GetOptions(
"primary!" => \$primary,
"secondary!" => \$secondary,
"scope=s" => \$scope,
"add-scope=s" => \$add_scope,
"remove-scope=s" => \$remove_scope,
"no-scope=s" => \$no_scope
);
if ($correct_options)
{
$scope = OsmApi::DEFAULT_SCOPE unless defined($scope);
$scope = add_scope($scope, $add_scope) if defined($add_scope);
$scope = remove_scope($scope, $remove_scope) if defined($remove_scope);
$scope = remove_scope($scope, $no_scope) if defined($no_scope);
if ($primary)
{
my $login_message = "Login with your osm account that has full permissions.\n";
request_token($scope, "oauth2_token", "primary", $login_message);
}
if ($secondary)
{
my $login_message = "Login with your bot/mechanical edit account.\n";
$login_message .= "Alternatively, if you want to use only one account, interrupt the script.\n" if $primary;
request_token($scope, "oauth2_token_secondary", "secondary", $login_message);
}
exit;
}
}
if ($ARGV[0] eq "check")
{
my $user_details = 1;
my $permissions = 0;
my $introspect = 1;
my $correct_options = GetOptions(
"user-details!" => \$user_details,
"permissions!" => \$permissions,
"introspect!" => \$introspect
);
if ($correct_options)
{
check_tokens($user_details, $permissions, $introspect);
exit;
}
}
print <<EOF;
Usage:
$0 request <options> request oauth2 tokens
$0 check <options> check details of stored tokens
request options:
--no-primary don't request primary token
--no-secondary don't request secondary token
--scope <permissions> request specified permissions
--add-scope <permissions> include permissions
--remove-scope <permissions> exclude permissions
--no-scope <permissions> exclude permissions
<permissions>:
space-separated list of osm permissions like write_api
default value for --scope is:
"@{[ OsmApi::DEFAULT_SCOPE ]}"
check options:
--no-user-details don't check user details
--no-introspect don't check token with /oauth2/introspect endpoint
--permissions check permissions with /api/0.6/permissions endpoint
EOF
exit;
sub add_scope
{
my ($scope, $add_scope) = @_;
my %permissions = map { $_ => 1 } split /\s+/, $scope;
my @new_permissions = grep { !$permissions{$_} } split /\s+/, $add_scope;
join " ", $scope, @new_permissions;
}
sub remove_scope
{
my ($scope, $remove_scope) = @_;
my %removed_permissions = map { $_ => 1 } split /\s+/, $remove_scope;
join " ", grep { !$removed_permissions{$_} } split /\s+/, $scope;
}
sub request_token
{
my ($scope, $token_name, $token_title, $login_message) = @_;
if (OsmApi::check_oauth2_token($token_name))
{
print "The $token_title token is already received. Delete '$token_name' from .osmtoolsrc to request it again.\n";
}
else
{
print "\n=== Requesting the $token_title token. ===\n\n$login_message";
OsmApi::request_oauth2_token($token_name, $scope);
}
}
sub check_tokens
{
if (OsmApi::check_oauth2_token("oauth2_token"))
{
print "Primary token details:\n";
print_token_details(1, @_);
}
else
{
print "No primary token stored.\n\n";
}
if (OsmApi::check_oauth2_token("oauth2_token_secondary"))
{
print "Secondary token details:\n";
print_token_details(0, @_);
}
else
{
print "No secondary token stored.\n\n";
}
}
sub print_token_details
{
use HTTP::Date qw(time2isoz);
my ($primary, $user_details, $permissions, $introspect) = @_;
print "- token: " . OsmApi::read_existing_oauth2_token($primary) . "\n";
my $resp;
if ($user_details)
{
$resp = OsmApi::get("user/details", undef, $primary);
if (!$resp->is_success)
{
print "- failed to get user details\n";
}
else
{
open my $fh, '<', \$resp->content;
while (<$fh>)
{
if (/<user/)
{
print "- user name: $1\n" if (/display_name="([^"]+)"/);
print "- user id: $1\n" if (/id="([^"]+)"/);
}
print "- moderator role\n" if (/<moderator/);
print "- administrator role\n" if (/<administrator/);
}
}
}
if ($permissions)
{
$resp = OsmApi::get("permissions", undef, $primary);
if (!$resp->is_success)
{
print "- failed to get permissions\n";
}
else
{
open my $fh, '<', \$resp->content;
while (<$fh>)
{
if (/<permission/)
{
print "- $1 permission\n" if (/name="([^"]+)"/);
}
}
}
}
if ($introspect)
{
$resp = OsmApi::introspect_existing_oauth2_token($primary);
if (!$resp->is_success)
{
print "- failed to introspect the token\n";
}
else
{
print "- token is inactive\n" if ($resp->content =~ /"active":false/);
print "- permissions: $1\n" if ($resp->content =~ /"scope":"([^"]+)"/);
print "- issued at: " . time2isoz($1) . "\n" if ($resp->content =~ /"iat":(\d+)/);
}
}
print "\n";
}