This repository has been archived on 2024-06-14. You can view files and clone it, but cannot push or open issues or pull requests.
icynet-auth-server/src/modules/oauth2/adapter/jwt.adapter.ts

44 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-09-16 15:11:36 +00:00
import { JWTAdapter, OAuth2User, OAuth2Client } from '@icynet/oauth2-provider';
import { OAuth2Service } from '../oauth2.service';
export class IcyJWTAdapter implements JWTAdapter {
constructor(private _client: OAuth2Service) {}
async issueIdToken(
rawUser: OAuth2User,
rawClient: OAuth2Client,
scope: string[],
nonce?: string,
): Promise<string> {
const user = await this._client.userService.getById(rawUser.id as number);
const userData: Record<string, unknown> = {
name: user.display_name,
preferred_username: user.username,
nickname: user.display_name,
updated_at: user.updated_at,
nonce,
};
if (scope.includes('email')) {
userData.email = user.email;
userData.email_verified = true;
}
if (
(scope.includes('image') || scope.includes('profile')) &&
user.picture
) {
userData.picture = `${this._client.config.get('app.base_url')}/uploads/${
user.picture.file
}`;
}
return this._client.jwt.issue(
userData,
user.uuid as string,
rawClient.id as string,
);
}
}