def needs_mfa(tenant_id): if conf.turn_off_mfa: return False logger.debug("checking if tenant needs mfa") tenant_config = tenant_configs_cache.get_config(tenant_id) logger.debug(tenant_config.mfa_config) try: mfa_config = json.loads(tenant_config.mfa_config) except Exception as e: logger.debug(f"Error parsing mfa config: {e}") return False return not not mfa_config
def call_mfa(token, tenant_id, username): logger.debug(f"calling mfa for: {username}") tenant_config = tenant_configs_cache.get_config(tenant_id) try: mfa_config = json.loads(tenant_config.mfa_config) except Exception as e: logger.debug(f"Error parsing mfa config: {e}") return e logger.debug(f"Tenant mfa config: {mfa_config}") if not mfa_config: return '' if "tacc" in mfa_config: return privacy_idea_tacc(mfa_config, token, username)
def get_custom_ldap_config(tenant_id): """ Checks the authenticator tenant config for a custom idp config and returns the attributes as a python dictionary. :return: dictionary of attributes related to customizing the ldap configuration. """ # if this is migrations, we won't be able to access the custom authenticator tenant config (in the db) so we just # return immediately -- if MIGRATIONS_RUNNING: return {} # this is the authenticator configuration for the tenant -- authenticator_tenant_config = tenant_configs_cache.get_config(tenant_id) custom_idp_configuration = json.loads( authenticator_tenant_config.custom_idp_configuration) try: return custom_idp_configuration['ldap'] except KeyError: return {}
def __init__(self, tenant_id, is_local_development=False): # the tenant id for this OAuth2 provider self.tenant_id = tenant_id # the custom tenant config for this tenant self.tenant_config = tenant_configs_cache.get_config( tenant_id=tenant_id) # the type of OAuth2 provider, such as github self.ext_type = tenant_configs_cache.get_custom_oa2_extension_type( tenant_id) # the actual custom_idp_configuration object, as a python dictionary self.custom_idp_config_dict = json.loads( self.tenant_config.custom_idp_configuration) # whether or not this authenticator is running in local development mode (i.e., on localhost) self.is_local_development = is_local_development # validate that this tenant should be using the OAuth2 extension module. if not self.ext_type: raise errors.ResourceError( f"Tenant {tenant_id} not configured for a custom OAuth2 extension." ) tenant_base_url = t.tenant_cache.get_tenant_config(tenant_id).base_url if self.is_local_development: self.callback_url = f'http://localhost:5000/v3/oauth2/extensions/oa2/callback' else: self.callback_url = f'{tenant_base_url}/v3/oauth2/extensions/oa2/callback' # These attributes get computed later, as a result of the OAuth flow ---------- self.authorization_code = None self.access_token = None self.username = None # Custom configs for each provider --------------- if self.ext_type == 'github': # github calls the client_key the "client_secret" self.client_id = self.custom_idp_config_dict.get('github').get( 'client_id') self.client_key = self.custom_idp_config_dict.get('github').get( 'client_secret') # initial redirect URL; used to start the oauth flow and log in the user self.identity_redirect_url = 'https://github.com/login/oauth/authorize' # URL to use to exchange the code for an qccess token self.oauth2_token_url = 'https://github.com/login/oauth/access_token' elif self.ext_type == 'cii': # we configure the CII redirect URL directly in the config because there are different CII environments. self.identity_redirect_url = self.custom_idp_config_dict.get( 'cii').get('login_url') if not self.identity_redirect_url: raise errors.ServiceConfigError( f"Missing required cii config, identity_redirect_url. " f"Config: {self.custom_idp_config_dict}") self.jwt_decode_key = self.custom_idp_config_dict.get('cii').get( 'jwt_decode_key') if not self.jwt_decode_key: raise errors.ServiceConfigError( f"Missing required cii config, jwt_decode_key. " f"Config: {self.custom_idp_config_dict}") self.check_jwt_signature = self.custom_idp_config_dict.get( 'check_jwt_signature') # note that CII does not implement standard OAuth2; they do not require a client id and key and they do not # create an authorization code to be exchanged for a token. self.client_id = 'not_used' self.client_key = 'not_used' # # NOTE: each provider type must implement this check # elif self.ext_type == 'google' # ... else: logger.error( f"ERROR! OAuth2ProviderExtension constructor not implemented for OAuth2 provider " f"extension {self.ext_type}.") raise errors.ServiceConfigError( f"Error processing callback URL: extension type {self.ext_type} not " f"supported.")
def authentication(): """ Entry point for checking authentication for all requests to the authenticator. :return: """ # The authenticator uses different authentication methods for different endpoints. For example, the service # APIs such as clients and profiles use pure JWT authentication, while the OAuth endpoints use Basic Authentication # with OAuth client credentials. logger.debug(f"base_url: {request.base_url}; url_rule: {request.url_rule}") if not hasattr(request, 'url_rule') or not hasattr( request.url_rule, 'rule') or not request.url_rule.rule: raise common_errors.ResourceError( "The endpoint and HTTP method combination " "are not available from this service.") # the metadata endpoint is publicly available if '/v3/oauth2/.well-known/' in request.url_rule.rule: logger.debug( ".well-known endpoint; request is allowed to be made unauthenticated." ) auth.resolve_tenant_id_for_request() return True # only the authenticator's own service token and tenant admins for the tenant can retrieve or modify the tenant # config if '/v3/oauth2/admin' in request.url_rule.rule: logger.debug( "admin endpoint; checking for authentictor service token or tenant admin role..." ) # admin endpoints always require tapis token auth auth.authentication() # we'll need to use the request's tenant_id, so make sure it is resolved now auth.resolve_tenant_id_for_request() # first, make sure this request is for a tenant served by this authenticator if g.request_tenant_id not in conf.tenants: raise common_errors.PermissionsError( f"The request is for a tenant ({g.request_tenant_id}) that is not " f"served by this authenticator.") # we only want to honor tokens from THIS authenticator; i.e., not some other authenticator. therefore, we need # to check that the tenant_id associated with the token (g.tenant_id) is the same as THIS authenticator's tenant # id; if g.username == conf.service_name and g.tenant_id == conf.service_tenant_id: logger.info( f"allowing admin request because username was {conf.service_name} " f"and tenant was {conf.service_tenant_id}") return True logger.debug( f"request token does not represent THIS authenticator: token username: {g.username};" f" request tenant: {g.tenant_id}. Now checking for tenant admin..." ) # all other service accounts are not allowed to update authenticator if g.account_type == 'service': raise common_errors.PermissionsError( "Not authorized -- service accounts are not allowed to access the" "authenticator admin endpoints.") # sanity check -- the request tenant id should be the same as the token tenant id in the remaining cases because # they are all user tokens if not g.request_tenant_id == g.tenant_id: logger.error( f"program error -- g.request_tenant_id: {g.request_tenant_id} not equal to " f"g.tenant_id: {g.tenant_id} even though account type was user!" ) raise common_errors.ServiceConfigError( f"Unexpected program error checking permissions. The tenant id of" f"the request ({g.request_tenant_id}) did not match the tenant id " f"of the access token ({g.tenant_id}). Please contact server " f"administrators.") # check SK for tenant admin -- try: rsp = t.sk.isAdmin(tenant=g.tenant_id, user=g.username) except Exception as e: logger.error( f"Got exception trying to check tenant admin role for tenant: {g.tenant_id} " f"and user: {g.username}; exception: {e}") raise common_errors.PermissionsError( "Could not check tenant admin role with SK; this role is required for " "accessing the authenticator admin endpoints.") try: if rsp.isAuthorized: logger.info( f"user {g.username} had tenant admin role for tenant {g.tenant_id}; allowing request." ) return True else: logger.info( f"user {g.username} DID NOT have tenant admin role for tenant {g.tenant_id}; " f"NOT allowing request.") raise common_errors.PermissionsError( "Permission denied -- Tenant admin role required for accessing " "the authenticator admin endpoints.") except Exception as e: logger.error( f"got exception trying to check isAuthorized property from isAdmin() call to SK." f"username: {g.username}; tenant: {g.tenant_id}; rsp: {rsp}; e: {e}" ) logger.info( f"user {g.username} DID NOT have tenant admin role for tenant {g.tenant_id}; " f"NOT allowing request.") raise common_errors.PermissionsError( "Permission denied -- Tenant admin role required for accessing the " "authenticator admin endpoints.") # no credentials required on the authorize, login and oa2 extenion pages if '/v3/oauth2/authorize' in request.url_rule.rule or '/v3/oauth2/login' in request.url_rule.rule \ or '/oauth2/extensions' in request.url_rule.rule: # always resolve the request tenant id based on the URL: logger.debug( "authorize, login or oa2 extension page. Resolving tenant_id") auth.resolve_tenant_id_for_request() try: logger.debug(f"request_tenant_id: {g.request_tenant_id}") except AttributeError: raise common_errors.BaseTapisError( "Unable to resolve tenant_id for request.") return True # the profiles endpoints always use standard Tapis Token auth - if '/v3/oauth2/profiles' in request.url_rule.rule or \ '/v3/oauth2/userinfo' in request.url_rule.rule: auth.authentication() # always resolve the request tenant id based on the URL: auth.resolve_tenant_id_for_request() return True # the clients endpoints need to accept both standard Tapis Token auth and basic auth, if '/v3/oauth2/clients' in request.url_rule.rule: # first check for basic auth header: parts = get_basic_auth_parts() if parts: logger.debug("oauth2 clients page, with basic auth header.") # do basic auth against the ldap # always resolve the request tenant id based on the URL: auth.resolve_tenant_id_for_request() try: logger.debug(f"request_tenant_id: {g.request_tenant_id}") except AttributeError: raise common_errors.BaseTapisError( "Unable to resolve tenant_id for request.") check_username_password(parts['tenant_id'], parts['username'], parts['password']) return True else: logger.debug("oauth2 clients page, no basic auth header.") # check for a Tapis token auth.authentication() # always resolve the request tenant id based on the URL: auth.resolve_tenant_id_for_request() try: logger.debug(f"request_tenant_id: {g.request_tenant_id}") except AttributeError: raise common_errors.BaseTapisError( "Unable to resolve tenant_id for request.") return True if '/v3/oauth2/tokens' in request.url_rule.rule: logger.debug("oauth2 tokens URL") # the tokens endpoint uses basic auth with the client; logic handled in the controller. # however, it does # require the request tenant id: # first, check if an X-Tapis-Token header appears in the request. We do not honor JWT authentication for # generating new tokens, but we also don't want to fail for an expired token. So, we remove the token header # if it if 'X-Tapis-Token' in request.headers: logger.debug("Got an X-Tapis-Token header.") try: auth.add_headers() auth.validate_request_token() except: # we need to set the token claims because the resolve_tenant_id_for_request method depends on it: g.token_claims = {} # now, resolve the tenant_id try: auth.resolve_tenant_id_for_request() except: # we need to catch and swallow permissions errors having to do with an invalid JWT; if the JWT is invalid, # its claims (including its tenant claim) will be ignored, but then resolve_tenant_id_for_request() will # throw an error because the None tenant_id claim will not match the tenant_id of the URL. pass try: logger.debug(f"request_tenant_id: {g.request_tenant_id}") except AttributeError: raise common_errors.BaseTapisError( "Unable to resolve tenant_id for request.") return True if '/v3/oauth2/logout' in request.url_rule.rule \ or '/v3/oauth2/login' in request.url_rule.rule \ or '/v3/oauth2/tenant' in request.url_rule.rule \ or '/v3/oauth2/webapp' in request.url_rule.rule \ or '/v3/oauth2/portal-login' in request.url_rule.rule: # or '/v3/oauth2/webapp/callback' in request.url_rule.rule \ # or '/v3/oauth2/webapp/token-display' in request.url_rule.rule \ logger.debug("call is for some token webapp page.") auth.resolve_tenant_id_for_request() try: logger.debug(f"request_tenant_id: {g.request_tenant_id}") except AttributeError: raise common_errors.BaseTapisError( "Unable to resolve tenant_id for request.") # make sure this tenant allows the token web app config = tenant_configs_cache.get_config(g.request_tenant_id) logger.debug(f"got tenant config: {config.serialize}") if not config.use_token_webapp: logger.info( f"tenant {g.request_tenant_id} not configured for the token webapp. Raising error" ) raise common_errors.PermissionsError( "This tenant is not configured to use the Token Webapp.") return True