2022-03-09 18:37:04 +00:00
|
|
|
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') {
|
2022-03-16 18:37:50 +00:00
|
|
|
client = await this.clientRepository.findOne(
|
|
|
|
{ client_id: id },
|
|
|
|
{ relations: ['urls', 'picture'] },
|
|
|
|
);
|
2022-03-09 18:37:04 +00:00
|
|
|
} else {
|
2022-03-16 18:37:50 +00:00
|
|
|
client = await this.clientRepository.findOne(
|
|
|
|
{ id },
|
|
|
|
{ relations: ['urls', 'picture'] },
|
|
|
|
);
|
2022-03-09 18:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getClientURLs(
|
|
|
|
id: string,
|
|
|
|
type?: OAuth2ClientURLType,
|
|
|
|
): Promise<OAuth2ClientURL[]> {
|
2022-03-16 18:37:50 +00:00
|
|
|
return this.clientUrlRepository.find({
|
|
|
|
where: {
|
|
|
|
client: { client_id: id },
|
|
|
|
type,
|
|
|
|
},
|
|
|
|
relations: ['client'],
|
|
|
|
});
|
2022-03-09 18:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async checkRedirectURI(id: string, url: string): Promise<boolean> {
|
2022-03-16 18:37:50 +00:00
|
|
|
return !!(await this.clientUrlRepository.findOne(
|
|
|
|
{
|
|
|
|
client: { client_id: id },
|
|
|
|
url,
|
|
|
|
type: OAuth2ClientURLType.REDIRECT_URI,
|
|
|
|
},
|
|
|
|
{ relations: ['client'] },
|
|
|
|
));
|
2022-03-09 18:37:04 +00:00
|
|
|
}
|
|
|
|
}
|