Exemplo n.º 1
0
def authentication_oauth_post():
    is_webview = request.form.get('webview')
    app.reset_authentication()
    redirect_uri = request.form.get('redirect_uri')
    if not redirect_uri:
        return app.error('Please provide redirect_url', HTTPStatus.BAD_REQUEST)

    try:
        access, refresh = AuthenticationManager.parse_redirect_url(redirect_uri)
        app.authentication_mgr.access_token = access
        app.authentication_mgr.refresh_token = refresh
        app.authentication_mgr.authenticate(do_refresh=False)
        app.authentication_mgr.dump(app.token_file)
    except Exception as e:
        if is_webview:
            return render_template('auth_result.html',
                                   title='Login fail',
                                   result='Login failed',
                                   message='Error message: {0}'.format(str(e)),
                                   link_path='/auth/login',
                                   link_title='Try again')
        else:
            return app.error('Login failed, error: {0}'.format(str(e)))

    if is_webview:
        return render_template('auth_result.html',
                               title='Login success',
                               result='Login succeeded',
                               message='Welcome {}!'.format(app.logged_in_gamertag),
                               link_path='/auth/logout',
                               link_title='Logout')
    else:
        return app.success(message='Login success', gamertag=app.logged_in_gamertag)
Exemplo n.º 2
0
    def authenticate(self, strategy_index, proof, otc):
        """
        Perform chain of Two-Factor-Authentication (2FA) with the Windows Live Server.

        Args:
            strategy_index (int): Index of chosen auth strategy
            server_data (dict): Parsed javascript-object `serverData`, obtained from Windows Live Auth Request
            otc (str): One Time Code

        Returns:
            tuple: If authentication succeeds, `tuple` of (AccessToken, RefreshToken) is returned
        """
        strategy = self.auth_strategies[strategy_index]
        auth_type = strategy.get('type')
        auth_data = strategy.get('data')
        log.debug('Using Method: {!s}'.format(TwoFactorAuthMethods(auth_type)))

        if TwoFactorAuthMethods.TOTPAuthenticatorV2 == auth_type:
            if not self.session_lookup_key:
                raise AuthenticationException(
                    'Did not receive SessionLookupKey from Authenticator V2 request!'
                )

            session_state = self._poll_session_state()
            if session_state != AuthSessionState.APPROVED:
                raise AuthenticationException(
                    'Authentication by Authenticator V2 failed!'
                    ' State: %s' % AuthSessionState(session_state))

            # Do not send auth_data when finishing TOTPv2 authentication
            auth_data = None
        response = self._finish_auth(auth_type, auth_data, otc, proof)

        try:
            return AuthenticationManager.parse_redirect_url(
                response.headers.get('Location'))
        except Exception as e:
            log.debug('Parsing redirection url failed, error: {0}'.format(
                str(e)))
            raise AuthenticationException(
                "2FA: Location header does not hold access/refresh tokens!")