icynet-auth-server/src/modules/features/oauth2/adapter/user.adapter.ts

69 lines
1.8 KiB
TypeScript
Raw Normal View History

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> {
return req.user;
2022-03-09 18:37:04 +00:00
}
async consented(
userId: number,
clientId: string,
scope: string | string[],
): Promise<boolean> {
2022-03-20 14:50:12 +00:00
return this._service.clientService.hasAuthorized(
userId,
clientId,
this._service.splitScope(scope),
);
2022-03-09 18:37:04 +00:00
}
async consent(
userId: number,
clientId: string,
scope: string | string[],
): Promise<boolean> {
2022-03-20 14:50:12 +00:00
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),
);
2022-03-09 18:37:04 +00:00
return true;
}
}