44import jwt
55import requests
66import json
7+ from .constants import SUPPLIER , SECONDARY_SUPPLIER
78from .secret import Secret
89
910
@@ -32,38 +33,39 @@ def generate_authentication(self, env, base_url, path, supplier_id = None):
3233 # the backend. The test will only check that the API doesn't return a 401,
3334 # a 404 response means the authentication is working.
3435 test_url = f"{ base_url } { path } "
36+ api_key = None
3537
3638 if env == "internal-dev" :
37- if (supplier_id == "TestSupplier1" ):
39+ if (supplier_id == SECONDARY_SUPPLIER ):
3840 api_key = os .environ ["NON_PROD_SECONDARY_API_KEY" ]
39- else :
41+ elif ( supplier_id == SUPPLIER ) :
4042 api_key = os .environ ["NON_PROD_API_KEY" ]
4143
4244 private_key = os .environ ["NON_PROD_PRIVATE_KEY" ]
4345 url = "https://internal-dev.api.service.nhs.uk/oauth2/token"
4446 kid = "internal-dev-test-1"
4547 elif env == "ref" :
46- if (supplier_id == "TestSupplier1" ):
48+ if (supplier_id == SECONDARY_SUPPLIER ):
4749 api_key = os .environ ["NON_PROD_SECONDARY_API_KEY" ]
48- else :
50+ elif ( supplier_id == SUPPLIER ) :
4951 api_key = os .environ ["NON_PROD_API_KEY" ]
5052
5153 private_key = os .environ ["NON_PROD_PRIVATE_KEY" ]
5254 url = "https://ref.api.service.nhs.uk/oauth2/token"
5355 kid = "internal-dev-test-1"
5456 elif env == "int" :
55- if (supplier_id == "TestSupplier1" ):
57+ if (supplier_id == SECONDARY_SUPPLIER ):
5658 api_key = os .environ ["INTEGRATION_SECONDARY_API_KEY" ]
57- else :
59+ elif ( supplier_id == SUPPLIER ) :
5860 api_key = os .environ ["INTEGRATION_API_KEY" ]
5961
6062 private_key = os .environ .get ("INTEGRATION_PRIVATE_KEY" )
6163 url = "https://int.api.service.nhs.uk/oauth2/token"
6264 kid = "internal-dev-test-1"
6365 elif env == "prod" :
64- if (supplier_id == "TestSupplier1" ):
66+ if (supplier_id == SECONDARY_SUPPLIER ):
6567 api_key = os .environ .get ("PRODUCTION_SECONDARY_API_KEY" )
66- else :
68+ elif ( supplier_id == SUPPLIER ) :
6769 api_key = os .environ .get ("PRODUCTION_API_KEY" )
6870
6971 private_key = os .environ .get ("PRODUCTION_PRIVATE_KEY" )
@@ -82,10 +84,10 @@ def generate_authentication(self, env, base_url, path, supplier_id = None):
8284 _ , latest_token_expiry = self .tokens .get (env , (None , 0 ))
8385
8486 # Generate new token if latest token will expire in 15 seconds
85- if env not in self .tokens or latest_token_expiry < int (time ()) + 15 :
86- self .tokens [env ] = self .generate_and_test_new_token (api_key , private_key , url , kid , test_url )
87+ if ( env , supplier_id ) not in self .tokens or latest_token_expiry < int (time ()) + 15 :
88+ self .tokens [( env , supplier_id ) ] = self .generate_and_test_new_token (api_key , private_key , url , kid , test_url )
8789
88- authentication_secret = self .tokens [env ][0 ]
90+ authentication_secret = self .tokens [( env , supplier_id ) ][0 ]
8991 return Secret (authentication_secret )
9092
9193 def generate_and_test_new_token (self , api_key , private_key , url , kid , test_url ):
0 commit comments