Flask controller for Authlib get token returns "unsupported_grant_type"

64 Views Asked by At

I need to migrate old authentication flow which uses Flask-OAuthlib to Authlib. Grant, Client and Token models were already in place, so I had to modify Client using ClientMixin and additional methods. However I'm getting {"error": "unsupported_grant_type"} response from /token endpoint

Here is a grant type

class AuthorizationCodeGrant(grants.AuthorizationCodeGrant):
    def save_authorization_code(self, code, request):
        """Saves a grant from mongodb and returns it as a Grant or None.
        @param client_id:
        @param code:
        @param grant_request:
        """
        LOGGER_PREFIX = "SAVE_AUTHORIZATION_CODE"
        logger.debug(f'{LOGGER_PREFIX}: code == {str(code)}')
        logger.debug(f'{LOGGER_PREFIX}: request == {str(request.__dict__)}')

        expires = datetime.utcnow() + timedelta(seconds=100)
        user = current_user()
        logger.debug(f'{LOGGER_PREFIX}: user == {str(user)}')

        client = request.client
        client_id = client.client_id

        grant = Grant(
            client_id=client_id,
            code=code,
            redirect_uri=request.redirect_uri,
            scopes=request.scope,
            expires=expires,
            user=user,
        )

        result = mongo.db.oauth_grant.update(
            {"user.user_id": user["user_id"], "client_id": client_id}, class_to_json(grant), upsert=True
        )

        logger.debug(f'{LOGGER_PREFIX}: result == {str(result)}')

        return grant

    def query_authorization_code(self, code, client):
        """Loads a grant from mongodb and returns it as a Grant or None.
        @param client_id:
        @param code:
        """
        LOGGER_PREFIX = "QUERY_AUTHORIZATION_CODE"

        client_id = client.client_id

        json = mongo.db.oauth_grant.find_one({"client_id": client_id, "code": code})
        grant = class_from_json(json, Grant)

        logger.debug(f'{LOGGER_PREFIX}: client_id == {str(client_id)}')
        logger.debug(f'{LOGGER_PREFIX}: json == {str(json)}')
        logger.debug(f'{LOGGER_PREFIX}: grant == {str(grant)}')

        return grant

    def delete_authorization_code(self, authorization_code):
        LOGGER_PREFIX = 'DELETE_AUTHORIZATION_CODE'
        logger.debug(f'{LOGGER_PREFIX}: authorization_code == {str(authorization_code)}')
        # db.session.delete(authorization_code)
        # db.session.commit()

    def authenticate_user(self, authorization_code):
        LOGGER_PREFIX = 'AUTHENTICATE_USER'
        logger.debug(f'{LOGGER_PREFIX}: authorization_code == {str(authorization_code)}')
        # return User.query.get(authorization_code.user_id)

    def check_authorization_endpoint(request):
        logger.debug(f'Check auth endpoint called...')
        return True

Here /token controller

@app.route("/token", methods=["GET", "POST"])
# @oauth.token_handler
def access_token():
    LOGGER_PREFIX = 'OAUTH2_TOKEN'
    logger.debug(f'{LOGGER_PREFIX}: Getting a token...')

    token = server.create_token_response()

    logger.debug(f'{LOGGER_PREFIX}: token == {str(token)}')

    return token
0

There are 0 best solutions below