1- from tapisservice import errors
2- from tapisservice .config import conf
3- from tapisservice .tapisflask import utils
41import json
52import time
6- import requests
7- from service .models import TenantConfig , tenant_configs_cache
83
4+ import requests
5+ from tapisservice .config import conf
96from tapisservice .logs import get_logger
107
8+ from service .models import tenant_configs_cache
9+
1110logger = get_logger (__name__ )
1211
1312
@@ -22,7 +21,7 @@ def needs_mfa(tenant_id, mfa_timestamp=None):
2221 except Exception :
2322 return False
2423
25- # mfa_config is a JSON object; if the tenant is not configured for MFA, then
24+ # mfa_config is a JSON object; if the tenant is not configured for MFA, then
2625 # the mfa_config object will be an empty dict (i.e., {})
2726 if mfa_config and not expired :
2827 return True
@@ -31,14 +30,16 @@ def needs_mfa(tenant_id, mfa_timestamp=None):
3130
3231def check_mfa_expired (mfa_config , mfa_timestamp = None ):
3332 """
34- Based on the tenant's MFA config and an optional MFA timestamp corresponding to the
33+ Based on the tenant's MFA config and an optional MFA timestamp corresponding to the
3534 last time an MFA was completed, determine whether the MFA session should be expired.
3635 """
3736 if mfa_timestamp is not None :
3837 if "tacc" in mfa_config :
39- if ' expire' in mfa_config [' tacc' ]:
38+ if " expire" in mfa_config [" tacc" ]:
4039 current_time = time .time ()
41- if current_time - mfa_timestamp > int (mfa_config ['tacc' ]['expiry_frequency' ]):
40+ if current_time - mfa_timestamp > int (
41+ mfa_config ["tacc" ]["expiry_frequency" ]
42+ ):
4243 return True
4344 return False
4445
@@ -49,16 +50,22 @@ def check_sms(tenant_id, username):
4950 try :
5051 mfa_config = json .loads (tenant_config .mfa_config )
5152 if "tacc" in mfa_config :
52- config_data = get_config_data (mfa_config )
53+ config = get_config_data (mfa_config )
5354
54- if config_data :
55- jwt = get_privacy_idea_jwt (config_data [ 'privacy_idea_url' ], config_data [ 'privacy_idea_client_id' ], config_data [ 'privacy_idea_client_key' ] )
55+ if config :
56+ jwt = get_privacy_idea_jwt (config )
5657 headers = {"Authorization" : jwt }
5758 logger .debug (headers )
5859 data = {"serial" : username }
59- res = requests .get (f"{ config_data ['privacy_idea_url' ]} /token?serial={ username } " , headers = headers , data = data )
60+ res = requests .get (
61+ f"{ config ['privacy_idea_url' ]} /token?serial={ username } " ,
62+ headers = headers ,
63+ data = data ,
64+ )
6065 result = res .json ()["result" ]
61- logger .debug (f"Serial request from Privacy Idea for { username } : { result } " )
66+ logger .debug (
67+ f"Serial request from Privacy Idea for { username } : { result } "
68+ )
6269 return res .json ()["result" ]["value" ]["tokens" ][0 ]["tokentype" ] == "sms"
6370 except Exception as e :
6471 logger .debug (f"Error checking SMS for { username } : { e } " )
@@ -72,14 +79,18 @@ def send_sms(tenant_id, username):
7279 try :
7380 mfa_config = json .loads (tenant_config .mfa_config )
7481 if "tacc" in mfa_config :
75- config_data = get_config_data (mfa_config )
82+ config = get_config_data (mfa_config )
7683
77- if config_data :
78- jwt = conf . privacy_idea_jwt
84+ if config :
85+ jwt = get_privacy_idea_jwt ( config )
7986 headers = {"Authorization" : jwt }
8087 logger .debug (headers )
8188 data = {"serial" : username }
82- res = requests .post (f"{ config_data ['privacy_idea_url' ]} /validate/triggerchallenge" , headers = headers , data = data )
89+ res = requests .post (
90+ f"{ config ['privacy_idea_url' ]} /validate/triggerchallenge" ,
91+ headers = headers ,
92+ data = data ,
93+ )
8394 return res .status_code == 200
8495 except Exception as e :
8596 logger .debug (f"Error sending SMS to { username } : { e } " )
@@ -94,57 +105,59 @@ def call_mfa(token, tenant_id, username):
94105 return e
95106
96107 if not mfa_config :
97- return ''
108+ return ""
98109
99110 if "tacc" in mfa_config :
100111 config = get_config_data (mfa_config )
101- jwt = get_privacy_idea_jwt (config ['privacy_idea_url' ], config ['privacy_idea_client_id' ], config ['privacy_idea_client_key' ])
102- return verify_mfa_token (config ['privacy_idea_url' ], jwt , token , username , config ['realm' ])
112+ jwt = get_privacy_idea_jwt (config )
113+ return verify_mfa_token (
114+ config ["privacy_idea_url" ], jwt , token , username , config ["realm" ]
115+ )
103116
104117
105118def get_config_data (config ):
106119 data = {}
107- data ['privacy_idea_url' ] = config ['tacc' ]['privacy_idea_url' ]
108- data ['privacy_idea_client_id' ] = config ['tacc' ]['privacy_idea_client_id' ]
109- data ['privacy_idea_client_key' ] = config ['tacc' ]['privacy_idea_client_key' ]
110- data ['grant_types' ] = config ['tacc' ].get ('grant_types' , '' )
111- data ['realm' ] = config ['tacc' ].get ('realm' , 'tacc' )
120+ data ["privacy_idea_url" ] = config ["tacc" ].get ("privacy_idea_url" , None )
121+ data ["privacy_idea_client_id" ] = config ["tacc" ].get ("privacy_idea_client_id" , None )
122+ data ["privacy_idea_client_key" ] = config ["tacc" ].get (
123+ "privacy_idea_client_key" , None
124+ )
125+ data ["privacy_idea_jwt" ] = config ["tacc" ].get ("privacy_idea_jwt" , None )
126+ data ["grant_types" ] = config ["tacc" ].get ("grant_types" , "" )
127+ data ["realm" ] = config ["tacc" ].get ("realm" , "tacc" )
112128
113129 return data
114130
115131
116- def get_privacy_idea_jwt (url , username , password ):
117- jwt = conf .get (' privacy_idea_jwt' )
118- if jwt is not None :
132+ def get_privacy_idea_jwt (config ):
133+ jwt = config .get (" privacy_idea_jwt" , None )
134+ if jwt :
119135 return jwt
120136 data = {
121- "username" : username ,
122- "password" : password
137+ "username" : config [ "privacy_idea_client_id" ] ,
138+ "password" : config [ "privacy_idea_client_key" ],
123139 }
124- url = f"{ url } /auth"
125- try :
126- response = requests .post (url , json = data )
127- response .raise_for_status ()
128- except Exception :
129- return
130- jwt = response .json ()['result' ]['value' ]['token' ]
140+ if config ["privacy_idea_url" ] and data ["username" ] and data ["password" ]:
141+ try :
142+ url = f"{ config ['privacy_idea_url' ]} /auth"
143+ response = requests .post (url , json = data )
144+ response .raise_for_status ()
145+
146+ jwt = response .json ()["result" ]["value" ]["token" ]
147+ except Exception as e :
148+ logger .debug (f"Error generating jwt: { e } " )
149+
131150 return jwt
132151
133152
134153def verify_mfa_token (url , jwt , token , username , realm ):
135154 url = f"{ url } /validate/check"
136- data = {
137- "user" : username ,
138- "realm" : realm ,
139- "pass" : token
140- }
141- headers = {
142- "x-tapis-token" : jwt
143- }
155+ data = {"user" : username , "realm" : realm , "pass" : token }
156+ headers = {"x-tapis-token" : jwt }
144157 try :
145158 response = requests .post (url , data = data , headers = headers )
146159 response .raise_for_status ()
147160 except Exception :
148161 return False
149- valid = response .json ()[' result' ][ ' value' ]
162+ valid = response .json ()[" result" ][ " value" ]
150163 return valid
0 commit comments