Skip to content

Commit 103bd65

Browse files
committed
Implement KEY_STORE_ROOT
1 parent 094a5c9 commit 103bd65

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ Full list of settings parameters with their defaults::
192192
'SESSION_AUDIENCE': None,
193193
'AUTHORIZATION_AUDIENCE': None,
194194
'ACCEPTED_ISSUERS': None,
195+
'KEY_STORE_ROOT': None,
195196
'PUBLIC_KEYS': {},
196197
'PRIVATE_KEYS': {},
197198

rest_framework_sso/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
'SESSION_AUDIENCE': None,
3030
'AUTHORIZATION_AUDIENCE': None,
3131
'ACCEPTED_ISSUERS': None,
32+
'KEY_STORE_ROOT': None,
3233
'PUBLIC_KEYS': {},
3334
'PRIVATE_KEYS': {},
3435

rest_framework_sso/utils.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# coding: utf-8
22
from __future__ import absolute_import, unicode_literals
33

4+
import os
45
from datetime import datetime
56

67
import jwt
@@ -125,6 +126,14 @@ def decode_jwt_token(token):
125126
return payload
126127

127128

129+
def read_key_file(file_name):
130+
if api_settings.KEY_STORE_ROOT:
131+
file_path = os.path.abspath(os.path.join(api_settings.KEY_STORE_ROOT, file_name))
132+
else:
133+
file_path = os.path.abspath(file_name)
134+
return open(file_path, 'rt').read()
135+
136+
128137
def get_private_key_and_key_id(issuer, key_id=None):
129138
if not api_settings.PRIVATE_KEYS.get(issuer):
130139
raise InvalidKeyError('No private keys defined for the given issuer')
@@ -133,7 +142,7 @@ def get_private_key_and_key_id(issuer, key_id=None):
133142
private_keys_setting = [private_keys_setting]
134143
for pks in private_keys_setting:
135144
if not key_id or key_id == pks:
136-
return open(pks, 'rt').read(), pks
145+
return read_key_file(file_name=pks), pks
137146
raise InvalidKeyError('No private key matches the given key_id')
138147

139148

@@ -150,7 +159,7 @@ def get_public_key_and_key_id(issuer, key_id=None):
150159
public_keys_setting = [public_keys_setting]
151160
for pks in public_keys_setting:
152161
if not key_id or key_id == pks:
153-
return open(pks, 'rt').read(), pks
162+
return read_key_file(file_name=pks), pks
154163
raise InvalidKeyError('No public key matches the given key_id')
155164

156165

0 commit comments

Comments
 (0)