53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
import {
|
|
JWTAdapter as OAuth2JWTAdapter,
|
|
OAuth2User,
|
|
OAuth2Client,
|
|
} from '@icynet/oauth2-provider';
|
|
import { Injectable } from '@nestjs/common';
|
|
import { ConfigurationService } from 'src/modules/config/config.service';
|
|
import { JWTService } from 'src/modules/jwt/jwt.service';
|
|
import { UserService } from 'src/modules/objects/user/user.service';
|
|
|
|
@Injectable()
|
|
export class JWTAdapter implements OAuth2JWTAdapter {
|
|
constructor(
|
|
private readonly userService: UserService,
|
|
private readonly config: ConfigurationService,
|
|
private readonly jwtService: JWTService,
|
|
) {}
|
|
|
|
async issueIdToken(
|
|
rawUser: OAuth2User,
|
|
rawClient: OAuth2Client,
|
|
scope: string[],
|
|
nonce?: string,
|
|
): Promise<string> {
|
|
const user = await this.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('picture') && user.picture) {
|
|
userData.picture = `${this.config.get('app.base_url')}/uploads/${
|
|
user.picture.file
|
|
}`;
|
|
}
|
|
|
|
return this.jwtService.issue(
|
|
userData,
|
|
user.uuid as string,
|
|
rawClient.id as string,
|
|
);
|
|
}
|
|
}
|