def _instrument_user_manager(app, status, payload):
    auth_service = auth._get_auth_service(app)
    user_manager = auth_service.user_manager
    recorder = []
    user_manager._session.mount(
        _user_mgt.ID_TOOLKIT_URL,
        testutils.MockAdapter(payload, status, recorder))
    return user_manager, recorder
Beispiel #2
0
def _outer_exchange_refresh_token(refresh_token, **kwargs):
    """Exchanges a user's refreshToken.
    Args:
        refresh_token.

    Returns:
        UserRecord: A UserRecord instance.

    Raises:
        AuthError: If an error occurs while authenticating.
    """
    app = kwargs.pop('app', None)
    user_manager = _get_auth_service(app).user_manager
    try:
        return user_manager.exchange_refresh_token(refresh_token=refresh_token)
    except firebase_admin._user_mgt.ApiCallError as error:
        raise AuthError(error.code, str(error), error.detail)
 def test_sign_with_discovered_service_account(self):
     request = testutils.MockRequest(200, 'discovered-service-account')
     options = {'projectId': 'mock-project-id'}
     app = firebase_admin.initialize_app(testutils.MockCredential(), name='iam-signer-app',
                                         options=options)
     try:
         _overwrite_iam_request(app, request)
         # Force initialization of the signing provider. This will invoke the Metadata service.
         auth_service = auth._get_auth_service(app)
         assert auth_service.token_generator.signing_provider is not None
         # Now invoke the IAM signer.
         signature = base64.b64encode(b'test').decode()
         request.response = testutils.MockResponse(
             200, '{{"signature": "{0}"}}'.format(signature))
         custom_token = auth.create_custom_token(MOCK_UID, app=app).decode()
         assert custom_token.endswith('.' + signature.rstrip('='))
         self._verify_signer(custom_token, 'discovered-service-account')
         assert len(request.log) == 2
         assert request.log[0][1]['headers'] == {'Metadata-Flavor': 'Google'}
     finally:
         firebase_admin.delete_app(app)
Beispiel #4
0
def _outer_verify_user(password, **kwargs):
    """Verifies a user given password and one of uid or email.
    Args:
        uid: A user ID string.
        email: user e-mail address.
        app: An App instance (optional).
    
    Returns:
        UserRecord: A UserRecord instance.
        
    Raises:
        ValueError: if both user ID and email are None, empty, or malformed
        AuthError: If an error occurs while authenticating.
    """
    app = kwargs.pop('app', None)
    user_manager = _get_auth_service(app).user_manager
    kwargs['password'] = password
    try:
        return user_manager.verify_user(**kwargs)
    except firebase_admin._user_mgt.ApiCallError as error:
        raise AuthError(error.code, str(error), error.detail)
Beispiel #5
0
def _overwrite_iam_request(app, request):
    auth_service = auth._get_auth_service(app)
    auth_service.token_generator.request = request
Beispiel #6
0
def _overwrite_cert_request(app, request):
    auth_service = auth._get_auth_service(app)
    auth_service.token_verifier.request = request
Beispiel #7
0
 def test_fail_on_no_project_id(self):
     app = firebase_admin.initialize_app(testutils.MockCredential(),
                                         name='userMgt2')
     with pytest.raises(ValueError):
         auth._get_auth_service(app)
     firebase_admin.delete_app(app)
Beispiel #8
0
 def test_bad_action_type(self, user_mgt_app):
     with pytest.raises(ValueError):
         auth._get_auth_service(user_mgt_app) \
             .user_manager \
             .generate_email_action_link('BAD_TYPE', '*****@*****.**',
                                         action_code_settings=MOCK_ACTION_CODE_SETTINGS)