35
36:- module(config_auth_google, []). 37:- use_module(swish(lib/oauth2)). 38:- use_module(swish(lib/plugin/login)). 39:- use_module(library(http/http_dispatch)). 40:- use_module(library(http/http_session)). 41:- use_module(library(http/http_json)). 42:- use_module(library(http/http_path)). 43:- use_module(library(debug)).
67:- multifile
68 oauth2:login/3,
69 oauth2:server_attribute/3,
70 swish_config:login_item/2, 71 swish_config:login/2, 72 swish_config:user_info/2. 73
74:- http_set_session_options([create(noauto), timeout(3600)]). 75
76:- http_handler(swish(logout), google_logout, []). 77
78swish_config:login_item(google, 10-Item) :-
79 http_absolute_location(icons('social_google_box.png'), Img, []),
80 Item = img([ src(Img),
81 class('login-with'),
82 'data-server'(google),
83 'data-frame'(popup),
84 title('Login with Google')
85 ]).
86
87swish_config:login(google, Request) :-
88 oauth2_login(Request, [server(google)]).
100oauth2:login(_Request, google, TokenInfo) :-
101 token_info_to_user_info(TokenInfo, UserInfo),
102 debug(oauth, 'UserInfo: ~p', [UserInfo]),
103 http_open_session(_SessionID, []),
104 http_session_assert(oauth2(google, TokenInfo)),
105 reply_logged_in([ identity_provider('Google'),
106 name(UserInfo.name),
107 user_info(UserInfo)
108 ]).
114google_logout(_Request) :-
115 catch(http_session_retractall(oauth2(_,_)), _, true),
116 reply_logged_out([]).
123swish_config:user_info(_Request, google, UserInfo) :-
124 http_in_session(_SessionID),
125 http_session_data(oauth2(google, TokenInfo)),
126 token_info_to_user_info(TokenInfo, UserInfo).
127
128token_info_to_user_info(TokenInfo, UserInfo) :-
129 oauth2_claim(TokenInfo, Claim),
130 map_user_info(Claim, Claim1),
131 http_link_to_id(google_logout, [], LogoutURL),
132 UserInfo = Claim1.put(_{ auth_method:oauth2,
133 logout_url:LogoutURL,
134 identity_provider:google
135 }).
141map_user_info(Dict, Dict) :-
142 debug(oauth, 'Got: ~p', [Dict]).
152
153oauth2:server_attribute(google, url,
154 'https://accounts.google.com').
155oauth2:server_attribute(google, redirect_uri,
156 'https://swish.swi-prolog.org/oauth2/google/reply').
157oauth2:server_attribute(google, client_id,
158 '962580012095-91omaabga3b4s3sk1c64vuj8013bnj97.apps.googleusercontent.com').
159oauth2:server_attribute(google, client_secret,
160 'dnPlexTcHECuTkkZYmdVGd_2').
161oauth2:server_attribute(google, scope,
162 profile)
Enable login with Google
This module allows for configures login with Google. To enable this module:
config-enabled
*/