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

57 lines
1.6 KiB
TypeScript

import { OAuth2ClientAdapter, OAuth2Client } from '@icynet/oauth2-provider';
import { OAuth2ClientURLType } from 'src/modules/objects/oauth2-client/oauth2-client-url.entity';
import { OAuth2Service } from '../oauth2.service';
export class ClientAdapter implements OAuth2ClientAdapter {
constructor(private _service: OAuth2Service) {}
getId(client: OAuth2Client): string {
return client.id as string;
}
async fetchById(id: string): Promise<OAuth2Client> {
const find = await this._service.clientService.getById(id);
return {
id: find.client_id,
scope: this._service.splitScope(find.scope),
grants: find.grants.split(' '),
secret: find.client_secret,
};
}
checkSecret(client: OAuth2Client, secret: string): boolean {
return client.secret === secret;
}
checkGrantType(client: OAuth2Client, grant: string): boolean {
return client.grants.includes(grant);
}
async hasRedirectUri(client: OAuth2Client): Promise<boolean> {
const redirectUris = await this._service.clientService.getClientURLs(
client.id as string,
OAuth2ClientURLType.REDIRECT_URI,
);
return !!redirectUris.length;
}
async checkRedirectUri(
client: OAuth2Client,
redirectUri: string,
): Promise<boolean> {
return this._service.clientService.checkRedirectURI(
client.id as string,
redirectUri,
);
}
transformScope(scope: string | string[]): string[] {
return Array.isArray(scope) ? scope : this._service.splitScope(scope);
}
checkScope(client: OAuth2Client, scope: string[]): boolean {
return scope.every((one) => client.scope.includes(one));
}
}