import { OAuth2UserAdapter, OAuth2User } from '@icynet/oauth2-provider'; import { OAuth2Service } from '../oauth2.service'; import { Request } from 'express'; import { ParamsDictionary } from 'express-serve-static-core'; import { ParsedQs } from 'qs'; export class UserAdapter implements OAuth2UserAdapter { constructor(private _service: OAuth2Service) {} getId(user: OAuth2User): number { return user.id as number; } async fetchById(id: number): Promise { const find = await this._service.userService.getById(id); return { id: find.id, username: find.username, password: find.password, }; } async fetchByUsername(username: string): Promise { const find = await this._service.userService.getByUsername(username); return { id: find.id, username: find.username, password: find.password, }; } checkPassword(user: OAuth2User, password: string): Promise { return this._service.userService.comparePasswords(user.password, password); } async fetchFromRequest( req: Request>, ): Promise { return req.user; } async consented( userId: number, clientId: string, scope: string | string[], ): Promise { return this._service.clientService.hasAuthorized( userId, clientId, this._service.splitScope(scope), ); } async consent( userId: number, clientId: string, scope: string | string[], ): Promise { const client = await this._service.clientService.getById(clientId); const user = await this._service.userService.getById(userId); await this._service.clientService.createAuthorization( user, client, this._service.splitScope(scope), ); return true; } }