Exemplo n.º 1
0
def save_token(token_data, request, *args, **kwargs):
    # For the implicit flow
    # Check issue: https://github.com/lepture/flask-oauthlib/issues/209
    if request.grant_type == 'authorization_code':
        user = request.user
    elif request.grant_type is None:  # implicit flow
        user = session.user
    else:
        raise ValueError('Invalid grant_type')
    requested_scopes = set(token_data['scope'].split())
    token = (OAuthToken.query.filter(
        OAuthApplication.client_id == request.client.client_id,
        OAuthToken.user == user).join(OAuthApplication).first())
    if token is None:
        application = OAuthApplication.query.filter_by(
            client_id=request.client.client_id).one()
        token = OAuthToken(application=application, user=user)
        db.session.add(token)
        token.access_token = token_data['access_token']
        token.scopes = requested_scopes
    elif requested_scopes - token.scopes:
        logger.info('Added scopes to %s: %s', token,
                    requested_scopes - token.scopes)
        # use the new access_token when extending scopes
        token.access_token = token_data['access_token']
        token.scopes |= requested_scopes
    else:
        token_data['access_token'] = token.access_token
    token_data.pop('refresh_token',
                   None)  # we don't support refresh tokens so far
    token_data.pop('expires_in', None)  # our tokens currently do not expire
    return token
Exemplo n.º 2
0
 def _create_tokens(**params):
     params.setdefault('access_token', unicode(uuid4()))
     params.setdefault('user', dummy_user)
     params.setdefault('application', dummy_application)
     params.setdefault('scopes', ['read:api', 'write:api'])
     token = OAuthToken(**params)
     db.session.add(token)
     db.session.flush()
     return token