icynet-auth-server/src/modules/objects/oauth2-client/oauth2-client.service.ts

48 lines
1.4 KiB
TypeScript

import { Inject, Injectable } from '@nestjs/common';
import { Repository } from 'typeorm';
import { OAuth2ClientAuthorization } from './oauth2-client-authorization.entity';
import {
OAuth2ClientURL,
OAuth2ClientURLType,
} from './oauth2-client-url.entity';
import { OAuth2Client } from './oauth2-client.entity';
@Injectable()
export class OAuth2ClientService {
constructor(
@Inject('CLIENT_REPOSITORY')
private clientRepository: Repository<OAuth2Client>,
@Inject('CLIENT_URL_REPOSITORY')
private clientUrlRepository: Repository<OAuth2ClientURL>,
@Inject('CLIENT_AUTHORIZATION_REPOSITORY')
private clientAuthRepository: Repository<OAuth2ClientAuthorization>,
) {}
public async getById(id: string | number): Promise<OAuth2Client> {
let client: OAuth2Client;
if (typeof id === 'string') {
client = await this.clientRepository.findOne({ client_id: id });
} else {
client = await this.clientRepository.findOne({ id });
}
return client;
}
public async getClientURLs(
id: string,
type?: OAuth2ClientURLType,
): Promise<OAuth2ClientURL[]> {
return this.clientUrlRepository.find({ client: { client_id: id }, type });
}
public async checkRedirectURI(id: string, url: string): Promise<boolean> {
return !!(await this.clientUrlRepository.findOne({
client: { client_id: id },
url,
type: OAuth2ClientURLType.REDIRECT_URI,
}));
}
}