icynet-auth-server/src/modules/oauth2/adapter/access-token.adapter.ts

103 lines
2.7 KiB
TypeScript
Raw Normal View History

2022-03-09 18:37:04 +00:00
import {
OAuth2AccessTokenAdapter,
OAuth2AccessToken,
} from '@icynet/oauth2-provider';
import { OAuth2TokenType } from 'src/modules/objects/oauth2-token/oauth2-token.entity';
2024-03-12 15:49:06 +00:00
import { Injectable } from '@nestjs/common';
import { OAuth2ClientService } from 'src/modules/objects/oauth2-client/oauth2-client.service';
import { UserService } from 'src/modules/objects/user/user.service';
import { TokenService } from 'src/modules/utility/services/token.service';
import { FormUtilityService } from 'src/modules/utility/services/form-utility.service';
import { OAuth2TokenService } from 'src/modules/objects/oauth2-token/oauth2-token.service';
2022-03-09 18:37:04 +00:00
2024-03-12 15:49:06 +00:00
@Injectable()
2022-03-09 18:37:04 +00:00
export class AccessTokenAdapter implements OAuth2AccessTokenAdapter {
2024-03-12 15:49:06 +00:00
constructor(
private readonly clientService: OAuth2ClientService,
private readonly userService: UserService,
private readonly token: TokenService,
private readonly form: FormUtilityService,
private readonly tokenService: OAuth2TokenService,
) {}
2022-03-09 18:37:04 +00:00
2022-03-16 18:37:50 +00:00
public ttl = 604800;
2022-03-09 18:37:04 +00:00
getToken(token: OAuth2AccessToken): string {
return token.token;
}
async create(
userId: number,
clientId: string,
scope: string | string[],
ttl: number,
): Promise<string> {
2024-03-12 15:49:06 +00:00
const client = await this.clientService.getById(clientId);
const user = await this.userService.getById(userId);
const accessToken = this.token.generateString(64);
2022-03-09 18:37:04 +00:00
// Standardize scope value
const scopes = (
2024-03-12 15:49:06 +00:00
!Array.isArray(scope) ? this.form.splitScope(scope) : scope
2022-03-09 18:37:04 +00:00
).join(' ');
const expiresAt = new Date(Date.now() + ttl * 1000);
2024-03-12 15:49:06 +00:00
this.tokenService.insertToken(
2022-03-09 18:37:04 +00:00
accessToken,
OAuth2TokenType.ACCESS_TOKEN,
client,
scopes,
expiresAt,
user,
);
return accessToken;
}
async fetchByToken(
token: string | OAuth2AccessToken,
): Promise<OAuth2AccessToken> {
const findBy = typeof token === 'string' ? token : token.token;
2024-03-12 15:49:06 +00:00
const find = await this.tokenService.fetchByToken(
2022-03-09 18:37:04 +00:00
findBy,
OAuth2TokenType.ACCESS_TOKEN,
);
2022-03-26 07:22:14 +00:00
if (!find) {
return null;
}
2022-03-09 18:37:04 +00:00
return {
...find,
client_id: find.client.client_id,
user_id: find.user.id,
};
}
checkTTL(token: OAuth2AccessToken): boolean {
return new Date() < token.expires_at;
}
getTTL(token: OAuth2AccessToken): number {
return token.expires_at.getTime() - Date.now();
}
async fetchByUserIdClientId(
userId: number,
clientId: string,
): Promise<OAuth2AccessToken> {
2024-03-12 15:49:06 +00:00
const find = await this.tokenService.fetchByUserIdClientId(
2022-03-09 18:37:04 +00:00
userId,
clientId,
OAuth2TokenType.ACCESS_TOKEN,
);
return {
...find,
client_id: find.client.client_id,
user_id: find.user.id,
};
}
}