-
Notifications
You must be signed in to change notification settings - Fork 1
/
oauth2client.pl
308 lines (262 loc) · 8.75 KB
/
oauth2client.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
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
/* $Id$
Author:
E-mail: pasikuuskasi windowslive.com
WWW:
Copyright (C):
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
As a special exception, if you link this library with other files,
compiled with a Free Software compiler, to produce an executable, this
library does not by itself cause the resulting executable to be covered
by the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
/** <module> Oauth2 client (for Google Accounts installed application flow)
Module implements client-side for OAuth2 protocol for installed
applications
Tested with Google Compute Engine, Google Youtube
Windows Xp, Windows 8, Firefox
Module will use
refresh_token.txt permanent storage (plain text!)
flag(access_token(A,A) access_token for API calls
flag(refresh_token(A,A) refresh_token temporary
flag(server_port,A,A) port of localhost server where user is redirected
flag(redirect_uri,A,A) uri for localhost server
flag(autcode,A,A) authorization code from user approval
oauth2_clientid(CID) fact NEEDS to be asserted from Developers Console,
Service account Id
oauth2_clientsecret fact NEEDS to be asserted from Developers Console,
Client Secret
*/
:- module(oauth2client,[valid_refresh_token/0, from_refreshtoken_accesstoken/0]).
:- use_module(library(http/http_open)).
:- use_module(library(http/http_ssl_plugin)).
:- use_module(library(http/json)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/json_convert)).
:- http_handler(root(oauth2accesscode), handle_autcode, []).
:- http_handler(root(test), handle_test, []).
%% oauth2_client_secret(CS) is det.
%Security stuff for OAuth2
%from personal Google API developers console
oauth2_client_secret(REPLACE_WITH_CLIENTSECRET_FROM_DEVELOPERS_CONSOLE).
%% oauth2_client_id(CID) is det.
%security stuff for OAuth2
%from personal Google API console
oauth2_client_id(REPLACE_WITH_SERVICE_ACCOUNT_ID_FROM_DEVELOPER_CONSOLE).
%% oauth2_endpoint_baseurl(EBU) is det.
%Security stuff for OAuth2
%from personal Google API developers console
oauth2_endpoint_baseurl('https://accounts.google.com/o/oauth2/auth').
%% oauth2_endpoint_token(GoogleUrl) is det.
%
% Used for refresh_token and access_token
%
oauth2_endpoint_token('https://accounts.google.com/o/oauth2/token').
%% oauth2_scope(WhitespaceSeparatedScopesAtom) is det.
%
% Used to specify what services user is asked for consent
oauth2_scope('https://www.googleapis.com/auth/compute https://www.googleapis.com/auth/devstorage.read_write').
%% handle_test(Request) is det.
%
% Test for checking http server
%
handle_test(_Request):-
format('Content-type: text/plain~n~n', []),
format('This is a nice new test new response~n'),
flag(server_port,Port,Port),
http_current_worker(Port, ThreadID),
format('Current worker threadID ~s~n',[ThreadID]),
%tell('testRequest.txt'),writeln(Request),told,
write_time.
%% write_time is det.
%
% Human readable time
%
write_time:-
get_time(Time),
format_time(atom(TD),'%a %d %b %Y %T',Time),
format('~s',[TD]).
%% autcode_server(Port) is det.
%
% https://developers.google.com/youtube/v3/guides/authentication
% Installed applications flow
% https://developers.google.com/accounts/docs/OAuth2InstalledApp
%
%local server to receive authorization code from Google
%After user approves the consent at consent page, user is
%redirected here
autcode_server(Port) :-
http_server(http_dispatch, [port(Port),timeout(10)]),
flag(server_port,_,Port).
%Shows in the web browser the OAuth2 consent page at Google's.
%Shows up finally if no file named refreshtoken.txt
%Needs propably main/superuser user rights on Windows
show_consent(Port,Scope,Url):-
oauth2_client_id(CID),
atom_concat('http://localhost:',Port,A),
atom_concat(A,'/oauth2accesscode',RDIRU),
flag(redirect_uri,_,RDIRU),
oauth2_endpoint_baseurl(Base),
parse_url(Url,Base,
[search([
client_id=CID,
redirect_uri=RDIRU,
%scope='https://www.googleapis.com/auth/youtube',
scope=Scope,
response_type=code,
access_type=offline
])]),
win_shell(open, Url).
%% handle_autocode(Request) is det.
%
% This is a handler for server where user is redirected after
% approving consent
%
%gets here (What gets here?), this is a handler for the autcode_server
handle_autcode(Request) :-
format('Content-type: text/plain~n~n'),
flag(server_port,Port,Port),
format('User consent received Ok~n'),
member(search(A),Request),!,
A=[(code=Code)],
flag(autcode,_,Code),
autcode_to_refreshtoken_accesstoken,
format('Token(s) received Ok~n'),
write_time.
%% autcode_to_refreshtoken_accesstoken is det.
%
%with temporary authorization code, get the refresh- and accesstokens
autcode_to_refreshtoken_accesstoken:-
oauth2_endpoint_token(Base),
%Base='https://accounts.google.com/o/oauth2/token',
flag(autcode,AC,AC),
oauth2_client_id(CID),
oauth2_client_secret(CS),
%oauth2(redirect_uri,RDIRU),
flag(redirect_uri,RDIRU,RDIRU),
ListofData=[
code=AC,
client_id=CID,
client_secret=CS,
redirect_uri=RDIRU,
grant_type=authorization_code
],
http_open(Base, In,
[ cert_verify_hook(cert_verify),
method(post),post(form(ListofData))
]),
json_read(In,JSon),
close(In),
json(List)=JSon,
member((access_token=AccToken),List),!,
flag(access_token,_,AccToken),
maybe_refreshtoken(List).
%% from_refreshtoken_accesstoken is det.
%
%If refreshtoken is valid it gets a valid accesstoken and inserts it to
%flag(access_token,A,A)
%
%accesstoken expires in 60 minutes, 3600 seconds
from_refreshtoken_accesstoken:-
oauth2_endpoint_token(Base),
%Base='https://accounts.google.com/o/oauth2/token',
flag(refresh_token,RT,RT),
oauth2_client_id(CID),
oauth2_client_secret(CS),
ListofData=[
refresh_token=RT,
client_id=CID,
client_secret=CS, %from Google API console
grant_type=refresh_token
],
http_open(Base, In,
[ cert_verify_hook(cert_verify),
method(post),post(form(ListofData))
]),
json_read(In,JSon),
close(In),
JSon=json(List),
member((access_token=AccToken),List),!,
flag(access_token,_,AccToken).
%stupid name for a predicate
maybe_refreshtoken(JSon):-
member((refresh_token=RefToken),JSon),
!,
flag(refresh_token,_,RefToken),
save_refresh_token.
maybe_refreshtoken(_).
%% u is det.
%
% Shortcut for getting accesstoken
u:-
from_refreshtoken_accesstoken.
%% valid_refresh_token is nondet.
%
% If no refresh_token set up http
% server and show a webcite etc. etc.
% checks if already done
%
% run/0 is shortcut to this
%
% refresh_token is saved with plain text (bad idea)
% to file refresh_token.txt
valid_refresh_token:-
\+ (flag(refresh_token,A,A),A=0),
!.
valid_refresh_token:-
catch((
see('refresh_token.txt'),
read(RefreshToken),
seen,
flag(refresh_token,_,RefreshToken)),
_,
fail),
!,
from_refreshtoken_accesstoken.
valid_refresh_token:-
autcode_server(A),
debug(oauth2client(valid_refresh_token),'~s ~s',['authorization server port',A]),
flag(server_port,Port,Port),
oauth2_scope(ScopesAtom),
%show_consent(Port,'https://www.googleapis.com/auth/compute https://www.googleapis.com/auth/devstorage.read_write',Url),
show_consent(Port,ScopesAtom,Url),
!,
debug(oauth2client(valid_refresh_token),'~s ~s',['consent url for local browser ',Url]).
%% save_refresh_token is det.
%
% Saves refresh_token to a plain txt file!
%
save_refresh_token:-
flag(refresh_token,RT,RT),
catch((
tell('refresh_token.txt'),
put('\''),write(RT),put('\''),put('.'),
told),_,true).
%% stop is det.
%
% Should stop local server that is used for redirection
% after user approves consent
stop:-
threads,
flag(server_port,Port,Port),
http_stop_server(Port,[]).
%% cert_verify(SSL, ProblemCert, AllCerts, FirstCert, Error)
% is det.
%
% Is used with HTTPS
%
%
cert_verify(_SSL, _ProblemCert, _AllCerts, _FirstCert, _Error) :-
debug(ytapp(cert_verify),'~s', ['Accepting certificate']).