def create_token(self, client_id, data, grant_type, scopes, user_id):
        if self.unique_token:
            if user_id is None:
                raise UserIdentifierMissingError

            try:
                access_token = self.access_token_store.\
                    fetch_existing_token_of_user(
                        client_id,
                        grant_type,
                        user_id)

                if (access_token.scopes == scopes
                        and access_token.is_expired() is False):
                    token_data = {
                        "access_token": access_token.token,
                        "token_type": "Bearer"
                    }

                    if access_token.refresh_token is not None:
                        token_data[
                            "refresh_token"] = access_token.refresh_token
                        token_data["expires_in"] = access_token.expires_in

                    return token_data
            except AccessTokenNotFound:
                pass

        token_data = self.token_generator.create_access_token_data(grant_type)

        access_token = AccessToken(client_id=client_id,
                                   data=data,
                                   grant_type=grant_type,
                                   token=token_data["access_token"],
                                   scopes=scopes,
                                   user_id=user_id)

        if "refresh_token" in token_data:

            expires_at = int(time.time()) + token_data["expires_in"]
            access_token.expires_at = expires_at
            access_token.refresh_token = token_data["refresh_token"]
            refresh_expires_in = self.token_generator.refresh_expires_in
            refresh_expires_at = int(time.time()) + refresh_expires_in
            access_token.refresh_expires_at = refresh_expires_at

        self.access_token_store.save_token(access_token)

        return token_data
Пример #2
0
    def create_token(self, client_id, data, grant_type, scopes, user_id):
        if self.unique_token:
            if user_id is None:
                raise UserIdentifierMissingError

            try:
                access_token = self.access_token_store.\
                    fetch_existing_token_of_user(
                        client_id,
                        grant_type,
                        user_id)

                if (access_token.scopes == scopes
                        and access_token.is_expired() is False):
                    token_data = {"access_token": access_token.token,
                                  "token_type": "Bearer"}

                    if access_token.refresh_token is not None:
                        token_data["refresh_token"] = access_token.refresh_token
                        token_data["expires_in"] = access_token.expires_in

                    return token_data
            except AccessTokenNotFound:
                pass

        token_data = self.token_generator.create_access_token_data(grant_type)

        access_token = AccessToken(client_id=client_id, data=data,
                                   grant_type=grant_type,
                                   token=token_data["access_token"],
                                   scopes=scopes,
                                   user_id=user_id)

        if "refresh_token" in token_data:
            expires_at = int(time.time()) + token_data["expires_in"]
            access_token.expires_at = expires_at
            access_token.refresh_token = token_data["refresh_token"]
            refresh_expires_in = self.token_generator.refresh_expires_in
            refresh_expires_at = int(time.time()) + refresh_expires_in
            access_token.refresh_expires_at = refresh_expires_at

        self.access_token_store.save_token(access_token)

        return token_data