Beispiel #1
0
def verify_token():
    ExtUser = current_app.mongodb_conn.ExtUser

    # fake data
    #
    # user = ExtUser.find_one()
    # if not user:
    #     user = ExtUser()
    #     user['scope'] = u'tester/testapp'
    #     user['open_id'] = u'test-open-id'
    #     user.save()
    # g.curr_user = user
    #
    # return

    open_id = current_app.sup_oauth.load_ext_token(request.headers)

    if not open_id:
        raise AuthFailed('invalid open id')

    user = ExtUser.find_one_activated_by_open_id(open_id)
    if user is None:
        raise AuthFailed("User Not Exist")

    if not user['access_token'] or user['expires_at'] < now():
        try:
            assert bool(user['refresh_token'])
            resp = current_app.sup_oauth.\
                refresh_access_token(user['refresh_token'])
            assert 'access_token' in resp
        except Exception:
            current_app.logger.warn(
                "Refresh token failed:\n{}".format(traceback.format_exc()))
            raise AuthFailed('refresh token failed')

        try:
            profile = current_app.sup_oauth.\
                get_profile(resp['access_token'])
        except:
            profile = None

        user['access_token'] = resp['access_token']
        user['expires_at'] = resp['expires_in'] + now()
        user['owner'] = resp['owner']
        user['app'] = resp['app']
        user['token_type'] = resp['token_type']

        if profile:
            user['display_name'] = profile['display_name']
            user['title'] = profile['title']
            user['locale'] = profile['locale']
            user['description'] = profile['description']
            user['type'] = profile['type']
            user['snapshot'] = profile['snapshot']
            user['scope'] = pre_process_scope(profile['owner_alias'],
                                              profile['app_alias'])
        user.save()

    g.curr_user = user
Beispiel #2
0
def verify_token():
    ExtUser = current_app.mongodb_conn.ExtUser

    # fake data
    # if current_app.use_fake_data:
    #     user = ExtUser.find_one()
    #     if not user:
    #         user = ExtUser()
    #         user['scope'] = u'tester/testapp'
    #         user['open_id'] = u'test-open-id'
    #         user.save()
    #     g.curr_user = user

    #     return

    open_id = current_app.sup_oauth.load_ext_token(request.headers)

    if not open_id:
        raise AuthFailed('invalid open id')

    user = ExtUser.find_one_activated_by_open_id(open_id)
    if user is None:
        raise AuthFailed("User Not Exist")

    if not user['access_token'] or user['expires_at'] < now():
        try:
            assert bool(user['refresh_token'])
            resp = current_app.sup_oauth.\
                refresh_access_token(user['refresh_token'])
            assert 'access_token' in resp
        except Exception:
            raise AuthFailed('refresh token failed')

        try:
            profile = current_app.sup_oauth.\
                get_profile(resp['access_token'])
        except:
            profile = None

        user['access_token'] = resp['access_token']
        user['expires_at'] = resp['expires_in'] + now()
        user['owner'] = resp['owner']
        user['app'] = resp['app']
        user['token_type'] = resp['token_type']

        if profile:
            user['display_name'] = profile['display_name']
            user['title'] = profile['title']
            user['locale'] = profile['locale']
            user['description'] = profile['description']
            user['type'] = profile['type']
            user['snapshot'] = profile['snapshot']
            user['scope'] = pre_process_scope(profile['owner_alias'],
                                              profile['app_alias'])
        user.save()

    g.curr_user = user
Beispiel #3
0
def get_oauth_access_token(open_id):
    Struct.Id(open_id)

    state = get_param('state', Struct.Sid, True)
    code = get_param('code', Struct.Sid, True)

    if not current_app.sup_oauth.match_random_string(state, open_id):
        raise UserStateInvalid

    ExtUser = current_app.mongodb_conn.ExtUser

    user = ExtUser.find_one_by_open_id(open_id)

    if not user:
        user = ExtUser()
        user['open_id'] = open_id

    try:
        resp = current_app.sup_oauth.get_access_token(code)
        print resp
        assert 'access_token' in resp
    except Exception as e:
        raise RequestAccessTokenFailed('access')

    try:
        profile = current_app.sup_oauth.get_profile(resp['access_token'])
    except current_app.sup_oauth.OAuthInvalidAccessToken as e:
        raise RequestAccessTokenFailed('profile')
    except Exception as e:
        raise UserProfileFailed(str(e))

    try:
        ext_token = current_app.sup_oauth.generate_ext_token(open_id)
    except Exception as e:
        raise UserTokenFailed(str(e))
    

    user['access_token'] = resp['access_token']
    user['refresh_token'] = resp['refresh_token']
    user['expires_at'] = resp['expires_in']+now()
    user['token_type'] = resp['token_type']
    user['status'] = ExtUser.STATUS_ACTIVATED

    user['display_name'] = profile['display_name']
    user['title'] = profile['title']
    user['locale'] = profile['locale']
    user['description'] = profile['description']
    user['type'] = profile['type']
    user['snapshot'] = profile['snapshot']
    user['scope'] = pre_process_scope(profile['owner_alias'],
                                      profile['app_alias'])
    user.save()
    
    logged_user = output_user(user)
    logged_user['token'] = ext_token

    return logged_user
Beispiel #4
0
def get_oauth_access_token(open_id):
    Struct.Id(open_id)

    state = get_param('state', Struct.Sid, True)
    code = get_param('code', Struct.Sid, True)

    if not current_app.sup_oauth.match_random_string(state, open_id):
        raise UserStateInvalid

    ExtUser = current_app.mongodb_conn.ExtUser

    user = ExtUser.find_one_by_open_id(open_id)

    if not user:
        user = ExtUser()
        user['open_id'] = open_id

    try:
        resp = current_app.sup_oauth.get_access_token(code)
        print resp
        assert 'access_token' in resp
    except Exception as e:
        raise RequestAccessTokenFailed('access')

    try:
        profile = current_app.sup_oauth.get_profile(resp['access_token'])
    except current_app.sup_oauth.OAuthInvalidAccessToken as e:
        raise RequestAccessTokenFailed('profile')
    except Exception as e:
        raise UserProfileFailed(str(e))

    try:
        ext_token = current_app.sup_oauth.generate_ext_token(open_id)
    except Exception as e:
        raise UserTokenFailed(str(e))

    user['access_token'] = resp['access_token']
    user['refresh_token'] = resp['refresh_token']
    user['expires_at'] = resp['expires_in'] + now()
    user['token_type'] = resp['token_type']
    user['status'] = ExtUser.STATUS_ACTIVATED

    user['display_name'] = profile['display_name']
    user['title'] = profile['title']
    user['locale'] = profile['locale']
    user['description'] = profile['description']
    user['type'] = profile['type']
    user['snapshot'] = profile['snapshot']
    user['scope'] = pre_process_scope(profile['owner_alias'],
                                      profile['app_alias'])
    user.save()

    logged_user = output_user(user)
    logged_user['token'] = ext_token

    return logged_user