2022-03-09 18:37:04 +00:00
|
|
|
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<OAuth2User> {
|
|
|
|
const find = await this._service.userService.getById(id);
|
|
|
|
return {
|
|
|
|
id: find.id,
|
|
|
|
username: find.username,
|
|
|
|
password: find.password,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async fetchByUsername(username: string): Promise<OAuth2User> {
|
|
|
|
const find = await this._service.userService.getByUsername(username);
|
|
|
|
return {
|
|
|
|
id: find.id,
|
|
|
|
username: find.username,
|
|
|
|
password: find.password,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
checkPassword(user: OAuth2User, password: string): Promise<boolean> {
|
|
|
|
return this._service.userService.comparePasswords(user.password, password);
|
|
|
|
}
|
|
|
|
|
|
|
|
async fetchFromRequest(
|
|
|
|
req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>,
|
|
|
|
): Promise<OAuth2User> {
|
2022-03-10 18:31:05 +00:00
|
|
|
return req.user;
|
2022-03-09 18:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async consented(
|
|
|
|
userId: number,
|
|
|
|
clientId: string,
|
|
|
|
scope: string | string[],
|
|
|
|
): Promise<boolean> {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
async consent(
|
|
|
|
userId: number,
|
|
|
|
clientId: string,
|
|
|
|
scope: string | string[],
|
|
|
|
): Promise<boolean> {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|