1111from jwt .exceptions import MissingRequiredClaimError , InvalidIssuerError
1212from rest_framework import exceptions
1313
14+ from rest_framework_sso import claims
1415from rest_framework_sso .settings import api_settings
1516
1617
1718def create_session_payload (session_token , user , ** kwargs ):
1819 return {
19- 'type' : 'session' ,
20- 'sid' : session_token .pk ,
21- 'uid' : user .pk ,
22- 'email' : user .email ,
20+ claims . TOKEN : claims . TOKEN_SESSION ,
21+ claims . SESSION_ID : session_token .pk ,
22+ claims . USER_ID : user .pk ,
23+ claims . EMAIL : user .email ,
2324 }
2425
2526
2627def create_authorization_payload (session_token , user , ** kwargs ):
2728 return {
28- 'type' : 'auth' ,
29- 'sid' : session_token .pk ,
30- 'uid' : user .pk ,
31- 'email' : user .email ,
32- 'scope' : [],
29+ claims . TOKEN : claims . TOKEN_AUTHORIZATION ,
30+ claims . SESSION_ID : session_token .pk ,
31+ claims . USER_ID : user .pk ,
32+ claims . EMAIL : user .email ,
33+ claims . SCOPES : [],
3334 }
3435
3536
3637def encode_jwt_token (payload ):
37- if payload .get ('type' ) not in ('session' , 'auth' ):
38+ if payload .get (claims . TOKEN ) not in (claims . TOKEN_SESSION , claims . TOKEN_AUTHORIZATION ):
3839 raise RuntimeError ('Unknown token type' )
3940
40- if not payload .get ('iss' ):
41+ if not payload .get (claims . ISSUER ):
4142 if api_settings .IDENTITY is not None :
42- payload ['iss' ] = api_settings .IDENTITY
43+ payload [claims . ISSUER ] = api_settings .IDENTITY
4344 else :
4445 raise RuntimeError ('IDENTITY must be specified in settings' )
4546
46- if not payload .get ('aud' ):
47- if payload .get ('type' ) == 'session' and api_settings .SESSION_AUDIENCE is not None :
48- payload ['aud' ] = api_settings .SESSION_AUDIENCE
49- elif payload .get ('type' ) == 'auth' and api_settings .AUTHORIZATION_AUDIENCE is not None :
50- payload ['aud' ] = api_settings .AUTHORIZATION_AUDIENCE
47+ if not payload .get (claims . AUDIENCE ):
48+ if payload .get (claims . TOKEN ) == claims . TOKEN_SESSION and api_settings .SESSION_AUDIENCE is not None :
49+ payload [claims . AUDIENCE ] = api_settings .SESSION_AUDIENCE
50+ elif payload .get (claims . TOKEN ) == claims . TOKEN_AUTHORIZATION and api_settings .AUTHORIZATION_AUDIENCE is not None :
51+ payload [claims . AUDIENCE ] = api_settings .AUTHORIZATION_AUDIENCE
5152 elif api_settings .IDENTITY is not None :
52- payload ['aud' ] = [api_settings .IDENTITY ]
53+ payload [claims . AUDIENCE ] = [api_settings .IDENTITY ]
5354 else :
5455 raise RuntimeError ('SESSION_AUDIENCE must be specified in settings' )
5556
56- if not payload .get ('exp' ):
57- if payload .get ('type' ) == 'session' and api_settings .SESSION_EXPIRATION is not None :
58- payload ['exp' ] = datetime .utcnow () + api_settings .SESSION_EXPIRATION
59- elif payload .get ('type' ) == 'auth' and api_settings .AUTHORIZATION_EXPIRATION is not None :
60- payload ['exp' ] = datetime .utcnow () + api_settings .AUTHORIZATION_EXPIRATION
57+ if not payload .get (claims . EXPIRATION_TIME ):
58+ if payload .get (claims . TOKEN ) == claims . TOKEN_SESSION and api_settings .SESSION_EXPIRATION is not None :
59+ payload [claims . EXPIRATION_TIME ] = datetime .utcnow () + api_settings .SESSION_EXPIRATION
60+ elif payload .get (claims . TOKEN ) == claims . TOKEN_AUTHORIZATION and api_settings .AUTHORIZATION_EXPIRATION is not None :
61+ payload [claims . EXPIRATION_TIME ] = datetime .utcnow () + api_settings .AUTHORIZATION_EXPIRATION
6162
62- if payload ['iss' ] not in api_settings .PRIVATE_KEYS :
63+ if payload [claims . ISSUER ] not in api_settings .PRIVATE_KEYS :
6364 raise RuntimeError ('Private key for specified issuer was not found in settings' )
6465
65- private_key = open (api_settings .PRIVATE_KEYS .get (payload ['iss' ]), 'rt' ).read ()
66+ private_key = open (api_settings .PRIVATE_KEYS .get (payload [claims . ISSUER ]), 'rt' ).read ()
6667
6768 return jwt .encode (
6869 payload = payload ,
@@ -75,10 +76,10 @@ def encode_jwt_token(payload):
7576def decode_jwt_token (token ):
7677 unverified_claims = jwt .decode (token , verify = False )
7778
78- if 'iss' not in unverified_claims :
79- raise MissingRequiredClaimError ('iss' )
79+ if claims . ISSUER not in unverified_claims :
80+ raise MissingRequiredClaimError (claims . ISSUER )
8081
81- unverified_issuer = six .text_type (unverified_claims ['iss' ])
82+ unverified_issuer = six .text_type (unverified_claims [claims . ISSUER ])
8283
8384 if api_settings .ACCEPTED_ISSUERS is not None and unverified_issuer not in api_settings .ACCEPTED_ISSUERS :
8485 raise InvalidIssuerError ('Invalid issuer' )
@@ -115,13 +116,13 @@ def authenticate_payload(payload):
115116 session_token = SessionToken .objects .\
116117 active ().\
117118 select_related ('user' ).\
118- get (pk = payload .get ('sid' ), user_id = payload .get ('uid' ))
119+ get (pk = payload .get (claims . SESSION_ID ), user_id = payload .get (claims . USER_ID ))
119120 user = session_token .user
120121 except SessionToken .DoesNotExist :
121122 raise exceptions .AuthenticationFailed (_ ('Invalid token.' ))
122123 else :
123124 try :
124- user = user_model .objects .get (pk = payload .get ('uid' ))
125+ user = user_model .objects .get (pk = payload .get (claims . USER_ID ))
125126 except user_model .DoesNotExist :
126127 raise exceptions .AuthenticationFailed (_ ('Invalid token.' ))
127128
0 commit comments