-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathcache.py
More file actions
69 lines (51 loc) · 2.24 KB
/
Copy pathcache.py
File metadata and controls
69 lines (51 loc) · 2.24 KB
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
# encoding: utf-8
"""
Copyright (c) 2020 Keitaro AB
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import logging
from saml2.ident import code, decode
from saml2.saml import NameID
log = logging.getLogger(__name__)
def set_subject_id(session, subject_id):
if isinstance(subject_id, str):
session['_saml2_subject_id'] = subject_id
else:
session['_saml2_subject_id'] = code(subject_id)
def get_subject_id(session):
try:
return decode(session['_saml2_subject_id'])
except KeyError:
return None
def set_saml_session_info(session, saml_session_info):
"""Adds information about pysaml2 AuthnResponse to CKAN's session.
`pysaml2` returns a NameID object in the session_info() call. Since we want
to serialize the object to write it into the cookie we need to convert it.
`name_id` is the same as `_saml2_subject_id` so we apply `code` as we do in
`set_subject_id`.
We are not sure if it always return an object, so we checking to be sure.
"""
if isinstance(saml_session_info['name_id'], NameID):
saml_session_info['name_id'] = code(saml_session_info['name_id'])
session['_saml_session_info'] = saml_session_info
def get_saml_session_info(session):
"""Returns the saml session info from the session object.
The session object is serializable but pysaml expect a NameID object as
name_id, so we are decoding it again as we do in get_subject_id.
"""
try:
session_info = session['_saml_session_info']
except KeyError:
return None
if isinstance(session_info['name_id'], str):
session_info['name_id'] = decode(session_info['name_id'])
return session_info