-
Notifications
You must be signed in to change notification settings - Fork 13
/
tokens.pl
178 lines (162 loc) · 4.83 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
#!/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 $no_scope;
my $correct_options = GetOptions(
"primary!" => \$primary,
"secondary!" => \$secondary,
"scope=s" => \$scope,
"no-scope=s" => \$no_scope
);
if ($correct_options)
{
if ($primary)
{
my $login_message = "Login with your osm account that has full permissions.\n";
request_token($scope, $no_scope, "oauth2_token", "primary", $login_message);
}
if ($secondary)
{
my $login_message = "Login with your bot/mechanical edit account.\n";
$login_message .= "Altenatively, if you want to use only one account, interrupt the script.\n" if $primary;
request_token($scope, $no_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 <space-separated permissions> include permissions
--no-scope <space-separated permissions> exclude permissions
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 request_token
{
my ($scope, $no_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, $no_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";
}