1 |
c8204b73
|
Serghei Mihai
|
import logging
|
2 |
|
|
|
3 |
b169c797
|
Serghei MIHAI
|
import ckan.plugins as plugins
|
4 |
|
|
import ckan.plugins.toolkit as toolkit
|
5 |
a5f39ab1
|
Serghei MIHAI
|
from ckan.common import session, c, request
|
6 |
|
|
from ckan import model
|
7 |
c8204b73
|
Serghei Mihai
|
import ckan.lib.base as base
|
8 |
|
|
|
9 |
|
|
from pylons import config, request
|
10 |
|
|
|
11 |
b71e8531
|
Serghei MIHAI
|
import conf
|
12 |
|
|
from oidc import create_client
|
13 |
c8204b73
|
Serghei Mihai
|
|
14 |
|
|
plugin_config_prefix = 'ckanext.ozwillo_pyoidc.'
|
15 |
|
|
|
16 |
|
|
log = logging.getLogger(__name__)
|
17 |
1c8b9fc4
|
Serghei MIHAI
|
plugin_controller = __name__ + ':OpenidController'
|
18 |
c8204b73
|
Serghei Mihai
|
|
19 |
b699aa44
|
Serghei MIHAI
|
_CLIENTS = {}
|
20 |
|
|
|
21 |
|
|
class Clients(object):
|
22 |
|
|
|
23 |
|
|
@classmethod
|
24 |
|
|
def get(cls, g):
|
25 |
|
|
global _CLIENTS
|
26 |
|
|
if g.id in _CLIENTS:
|
27 |
|
|
return _CLIENTS.get(g.id)
|
28 |
|
|
client = cls().get_client(g)
|
29 |
|
|
_CLIENTS.update({g.id: client})
|
30 |
|
|
return client
|
31 |
|
|
|
32 |
|
|
def get_client(self, g):
|
33 |
|
|
params = conf.CLIENT.copy()
|
34 |
|
|
params['client_registration'].update({
|
35 |
|
|
'client_id': g._extras['client_id'].value,
|
36 |
|
|
'client_secret': g._extras['client_secret'].value,
|
37 |
|
|
'redirect_uris': [toolkit.url_for(host=request.host,
|
38 |
|
|
controller=plugin_controller,
|
39 |
|
|
action='callback',
|
40 |
|
|
id=g.name,
|
41 |
|
|
qualified=True)]
|
42 |
|
|
})
|
43 |
|
|
return create_client(**params)
|
44 |
|
|
|
45 |
b169c797
|
Serghei MIHAI
|
|
46 |
|
|
class OzwilloPyoidcPlugin(plugins.SingletonPlugin):
|
47 |
|
|
plugins.implements(plugins.IConfigurer)
|
48 |
c8204b73
|
Serghei Mihai
|
plugins.implements(plugins.IRoutes)
|
49 |
|
|
plugins.implements(plugins.IAuthenticator, inherit=True)
|
50 |
b169c797
|
Serghei MIHAI
|
|
51 |
c8204b73
|
Serghei Mihai
|
def before_map(self, map):
|
52 |
a5f39ab1
|
Serghei MIHAI
|
map.connect('/organization/{id:.*}/sso',
|
53 |
|
|
controller=plugin_controller,
|
54 |
|
|
action='sso')
|
55 |
|
|
map.connect('/organization/{id:.*}/callback',
|
56 |
|
|
controller=plugin_controller,
|
57 |
|
|
action='callback')
|
58 |
1c8b9fc4
|
Serghei MIHAI
|
map.connect('/logout', controller=plugin_controller,
|
59 |
|
|
action='logout')
|
60 |
1ae62674
|
Serghei MIHAI
|
map.connect('/user/slo',
|
61 |
|
|
controller=plugin_controller,
|
62 |
1c8b9fc4
|
Serghei MIHAI
|
action='slo',
|
63 |
|
|
conditions={'method': ['POST']})
|
64 |
1ae62674
|
Serghei MIHAI
|
map.redirect('/organization/{id:.*}/logout', '/user/_logout')
|
65 |
|
|
|
66 |
c8204b73
|
Serghei Mihai
|
return map
|
67 |
|
|
|
68 |
|
|
def after_map(self, map):
|
69 |
|
|
return map
|
70 |
|
|
|
71 |
|
|
def identify(self):
|
72 |
a5f39ab1
|
Serghei MIHAI
|
user = session.get('user')
|
73 |
|
|
if user and not toolkit.c.userobj:
|
74 |
|
|
userobj = model.User.get(user)
|
75 |
|
|
toolkit.c.user = userobj.name
|
76 |
|
|
toolkit.c.userobj = userobj
|
77 |
c8204b73
|
Serghei Mihai
|
|
78 |
|
|
def login(self):
|
79 |
a5f39ab1
|
Serghei MIHAI
|
if 'organization_id' in session:
|
80 |
|
|
g = model.Group.get(session['organization_id'])
|
81 |
b699aa44
|
Serghei MIHAI
|
client = Clients.get(g)
|
82 |
|
|
url, ht_args = client.create_authn_request(session, conf.ACR_VALUES)
|
83 |
a5f39ab1
|
Serghei MIHAI
|
if ht_args:
|
84 |
|
|
toolkit.request.headers.update(ht_args)
|
85 |
|
|
toolkit.redirect_to(url)
|
86 |
|
|
else:
|
87 |
|
|
toolkit.redirect_to('/')
|
88 |
c8204b73
|
Serghei Mihai
|
|
89 |
|
|
def logout(self):
|
90 |
b87c1c93
|
Serghei MIHAI
|
log.info('Logging out user: %s' % session['user'])
|
91 |
7400c5df
|
Serghei MIHAI
|
session['user'] = None
|
92 |
b87c1c93
|
Serghei MIHAI
|
session.save()
|
93 |
|
|
g = model.Group.get(session['organization_id'])
|
94 |
|
|
if g:
|
95 |
84922cdf
|
Serghei MIHAI
|
org_url = toolkit.url_for(host=request.host,
|
96 |
|
|
controller='organization',
|
97 |
|
|
action='read',
|
98 |
|
|
id=g.name,
|
99 |
|
|
qualified=True)
|
100 |
|
|
|
101 |
b87c1c93
|
Serghei MIHAI
|
toolkit.redirect_to(str(org_url))
|
102 |
|
|
else:
|
103 |
|
|
toolkit.redirect_to('/')
|
104 |
b169c797
|
Serghei MIHAI
|
|
105 |
|
|
def update_config(self, config_):
|
106 |
|
|
toolkit.add_template_directory(config_, 'templates')
|
107 |
|
|
toolkit.add_public_directory(config_, 'public')
|
108 |
|
|
toolkit.add_resource('fanstatic', 'ozwillo_pyoidc')
|
109 |
c8204b73
|
Serghei Mihai
|
|
110 |
|
|
class OpenidController(base.BaseController):
|
111 |
|
|
|
112 |
a5f39ab1
|
Serghei MIHAI
|
def sso(self, id):
|
113 |
|
|
log.info('SSO for organization "%s"' % id)
|
114 |
|
|
session['organization_id'] = id
|
115 |
|
|
session.save()
|
116 |
|
|
log.info('redirecting to login page')
|
117 |
|
|
login_url = toolkit.url_for(host=request.host,
|
118 |
|
|
controller='user',
|
119 |
|
|
action='login',
|
120 |
|
|
qualified=True)
|
121 |
|
|
toolkit.redirect_to(login_url)
|
122 |
|
|
|
123 |
|
|
def callback(self):
|
124 |
b699aa44
|
Serghei MIHAI
|
g = model.Group.get(session['organization_id'])
|
125 |
|
|
client = Clients.get(g)
|
126 |
|
|
userinfo = client.callback(request.GET)
|
127 |
|
|
log.info('Received userinfo: %s' % userinfo)
|
128 |
e7b8bf5b
|
Serghei MIHAI
|
userobj = model.User.get(userinfo['sub'])
|
129 |
b699aa44
|
Serghei MIHAI
|
if userobj:
|
130 |
|
|
if 'given_name' in userinfo:
|
131 |
|
|
userobj.fullname = userinfo['given_name']
|
132 |
|
|
if 'family_name' in userinfo:
|
133 |
|
|
userobj.fullname += userinfo['family_name']
|
134 |
|
|
userobj.save()
|
135 |
|
|
session['user'] = userobj.id
|
136 |
|
|
session.save()
|
137 |
|
|
|
138 |
|
|
org_url = toolkit.url_for(host=request.host,
|
139 |
|
|
controller="organization",
|
140 |
|
|
action='read',
|
141 |
cdd21e6f
|
Serghei MIHAI
|
id=g.name,
|
142 |
b013655e
|
Serghei MIHAI
|
locale=userinfo.get('locale'),
|
143 |
b699aa44
|
Serghei MIHAI
|
qualified=True)
|
144 |
b013655e
|
Serghei MIHAI
|
toolkit.redirect_to(str(org_url))
|
145 |
1ae62674
|
Serghei MIHAI
|
|
146 |
1c8b9fc4
|
Serghei MIHAI
|
def logout(self):
|
147 |
|
|
toolkit.c.slo_url = toolkit.url_for(host=request.host,
|
148 |
|
|
controller=plugin_controller,
|
149 |
|
|
action="slo",
|
150 |
|
|
qualified=True)
|
151 |
|
|
return base.render('logout_confirm.html')
|
152 |
|
|
|
153 |
1ae62674
|
Serghei MIHAI
|
def slo(self):
|
154 |
|
|
"""
|
155 |
|
|
Revokes the delivered access token. Logs out the user
|
156 |
|
|
"""
|
157 |
b699aa44
|
Serghei MIHAI
|
g = model.Group.get(session['organization_id'])
|
158 |
1ae62674
|
Serghei MIHAI
|
org_url = toolkit.url_for(host=request.host,
|
159 |
|
|
controller='organization',
|
160 |
|
|
action='read',
|
161 |
cdd21e6f
|
Serghei MIHAI
|
id=g.name,
|
162 |
1ae62674
|
Serghei MIHAI
|
qualified=True)
|
163 |
1c8b9fc4
|
Serghei MIHAI
|
org_url = str(org_url)
|
164 |
|
|
|
165 |
|
|
if toolkit.c.user and request.method == 'POST':
|
166 |
|
|
client = Clients.get(g)
|
167 |
|
|
logout_url = client.end_session_endpoint
|
168 |
|
|
|
169 |
|
|
redirect_uri = org_url + '/logout'
|
170 |
|
|
|
171 |
|
|
# revoke the access token
|
172 |
|
|
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
|
173 |
|
|
data = 'token=' + client.access_token
|
174 |
|
|
data += '&token_type_hint=access_token'
|
175 |
|
|
client.http_request(client.revocation_endpoint, 'POST',
|
176 |
|
|
data=data, headers=headers)
|
177 |
|
|
|
178 |
|
|
# redirect to IDP logout
|
179 |
|
|
logout_url += '?id_token_hint=%s&' % client.id_token
|
180 |
|
|
logout_url += 'post_logout_redirect_uri=%s' % redirect_uri
|
181 |
|
|
toolkit.redirect_to(logout_url)
|
182 |
|
|
toolkit.redirect_to(org_url)
|