1+ import base64
12import json
23import os
34
4- from oidcop .configure import OPConfiguration
55import pytest
66from cryptojwt import JWT
77from cryptojwt .key_jar import build_keyjar
1515from oidcop .authn_event import create_authn_event
1616from oidcop .authz import AuthzHandling
1717from oidcop .client_authn import verify_client
18+ from oidcop .configure import OPConfiguration
1819from oidcop .cookie_handler import CookieHandler
1920from oidcop .exception import UnAuthorizedClient
2021from oidcop .oidc import userinfo
2627from oidcop .session import MintingNotAllowed
2728from oidcop .user_authn .authn_context import INTERNETPROTOCOLPASSWORD
2829from oidcop .user_info import UserInfo
30+ from oidcop .util import lv_pack
2931
3032KEYDEFS = [
3133 {"type" : "RSA" , "key" : "" , "use" : ["sig" ]},
@@ -113,7 +115,7 @@ def conf():
113115 },
114116 "refresh" : {
115117 "class" : "oidcop.token.jwt_token.JWTToken" ,
116- "kwargs" : {"lifetime" : 3600 , "aud" : ["https://example.org/appl" ],},
118+ "kwargs" : {"lifetime" : 3600 , "aud" : ["https://example.org/appl" ], },
117119 },
118120 "id_token" : {"class" : "oidcop.token.id_token.IDToken" , "kwargs" : {}},
119121 },
@@ -127,8 +129,8 @@ def conf():
127129 "class" : ProviderConfiguration ,
128130 "kwargs" : {},
129131 },
130- "registration" : {"path" : "registration" , "class" : Registration , "kwargs" : {},},
131- "authorization" : {"path" : "authorization" , "class" : Authorization , "kwargs" : {},},
132+ "registration" : {"path" : "registration" , "class" : Registration , "kwargs" : {}, },
133+ "authorization" : {"path" : "authorization" , "class" : Authorization , "kwargs" : {}, },
132134 "token" : {
133135 "path" : "token" ,
134136 "class" : Token ,
@@ -164,7 +166,7 @@ def conf():
164166 "usage_rules" : {
165167 "authorization_code" : {
166168 "expires_in" : 300 ,
167- "supports_minting" : ["access_token" , "refresh_token" , "id_token" ,],
169+ "supports_minting" : ["access_token" , "refresh_token" , "id_token" , ],
168170 "max_usage" : 1 ,
169171 },
170172 "access_token" : {"expires_in" : 600 },
@@ -741,3 +743,97 @@ def test_configure_grant_types(self):
741743 assert len (self .token_endpoint .helper ) == 1
742744 assert "access_token" in self .token_endpoint .helper
743745 assert "refresh_token" not in self .token_endpoint .helper
746+
747+
748+ class TestOldTokens (object ):
749+ @pytest .fixture (autouse = True )
750+ def create_endpoint (self , conf ):
751+ server = Server (OPConfiguration (conf = conf , base_path = BASEDIR ), cwd = BASEDIR )
752+
753+ endpoint_context = server .endpoint_context
754+ endpoint_context .cdb ["client_1" ] = {
755+ "client_secret" : "hemligt" ,
756+ "redirect_uris" : [("https://example.com/cb" , None )],
757+ "client_salt" : "salted" ,
758+ "endpoint_auth_method" : "client_secret_post" ,
759+ "response_types" : ["code" , "token" , "code id_token" , "id_token" ],
760+ }
761+ endpoint_context .keyjar .import_jwks (CLIENT_KEYJAR .export_jwks (), "client_1" )
762+ self .session_manager = endpoint_context .session_manager
763+ self .token_endpoint = server .server_get ("endpoint" , "token" )
764+ self .user_id = "diana"
765+ self .endpoint_context = endpoint_context
766+
767+ def _create_session (self , auth_req , sub_type = "public" , sector_identifier = "" ):
768+ if sector_identifier :
769+ authz_req = auth_req .copy ()
770+ authz_req ["sector_identifier_uri" ] = sector_identifier
771+ else :
772+ authz_req = auth_req
773+ client_id = authz_req ["client_id" ]
774+ ae = create_authn_event (self .user_id )
775+ return self .session_manager .create_session (
776+ ae , authz_req , self .user_id , client_id = client_id , sub_type = sub_type
777+ )
778+
779+ def _mint_code (self , grant , client_id ):
780+ session_id = self .session_manager .encrypted_session_id (self .user_id , client_id , grant .id )
781+ usage_rules = grant .usage_rules .get ("authorization_code" , {})
782+ _exp_in = usage_rules .get ("expires_in" )
783+
784+ # Constructing an authorization code is now done
785+ _code = grant .mint_token (
786+ session_id = session_id ,
787+ endpoint_context = self .endpoint_context ,
788+ token_class = "authorization_code" ,
789+ token_handler = self .session_manager .token_handler ["authorization_code" ],
790+ usage_rules = usage_rules ,
791+ )
792+
793+ if _exp_in :
794+ if isinstance (_exp_in , str ):
795+ _exp_in = int (_exp_in )
796+ if _exp_in :
797+ _code .expires_at = utc_time_sans_frac () + _exp_in
798+ return _code
799+
800+ def test_old_default_token (self ):
801+ session_id = self ._create_session (AUTH_REQ )
802+ grant = self .session_manager [session_id ]
803+ code = self ._mint_code (grant , AUTH_REQ ["client_id" ])
804+
805+ # pack and unpack
806+ _handler = self .session_manager .token_handler .handler ["authorization_code" ]
807+ _res = dict (zip (["_id" , "token_class" , "sid" , "exp" ], _handler .split_token (code .value )))
808+
809+ _old_type_value = base64 .b64encode (
810+ _handler .crypt .encrypt (lv_pack (_res ["_id" ], "A" , _res ["sid" ], _res ["exp" ]).encode ())
811+ ).decode ("utf-8" )
812+
813+ _info = self .session_manager .token_handler .info (_old_type_value )
814+ assert _info ["token_class" ] == "authorization_code"
815+
816+ def test_old_jwt_token (self ):
817+ session_id = self ._create_session (AUTH_REQ )
818+ grant = self .session_manager [session_id ]
819+ code = self ._mint_code (grant , AUTH_REQ ["client_id" ])
820+
821+ _handler = self .session_manager .token_handler .handler ["access_token" ]
822+ _old_type_token = _handler (session_id = session_id , token_class = "T" )
823+
824+ _info = self .session_manager .token_handler .info (_old_type_token )
825+ assert _info ["token_class" ] == "access_token"
826+
827+ payload = {"sid" : session_id , "ttype" : "T" }
828+ payload = _handler .load_custom_claims (payload )
829+
830+ # payload.update(kwargs)
831+ _context = _handler .server_get ("endpoint_context" )
832+ signer = JWT (
833+ key_jar = _context .keyjar , iss = _handler .issuer , lifetime = 300 , sign_alg = _handler .alg ,
834+ )
835+
836+ _old_type_token = signer .pack (payload )
837+
838+ _info = self .session_manager .token_handler .info (_old_type_token )
839+ assert _info ["token_class" ] == "access_token"
0 commit comments