backend: Refactor AuthService and TokenService to use User object for authentication and token generation
This commit is contained in:
parent
5e1e418c13
commit
a9274005ef
@ -1,17 +1,32 @@
|
|||||||
import { MEET_ADMIN_SECRET, MEET_ADMIN_USER, MEET_PRIVATE_ACCESS, MEET_SECRET, MEET_USER } from '../environment.js';
|
import { MEET_ADMIN_SECRET, MEET_ADMIN_USER, MEET_PRIVATE_ACCESS, MEET_SECRET, MEET_USER } from '../environment.js';
|
||||||
import { injectable } from '../config/dependency-injector.config.js';
|
import { inject, injectable } from '../config/dependency-injector.config.js';
|
||||||
|
import { User } from '@typings-ce';
|
||||||
|
import { UserService } from './user.service.js';
|
||||||
|
|
||||||
@injectable()
|
@injectable()
|
||||||
export class AuthService {
|
export class AuthService {
|
||||||
authenticateAdmin(username: string, password: string): boolean {
|
constructor(@inject(UserService) protected userService: UserService) {}
|
||||||
|
|
||||||
|
authenticate(username: string, password: string): User | null {
|
||||||
|
const isAdmin = this.authenticateAdmin(username, password);
|
||||||
|
const isUser = this.authenticateUser(username, password);
|
||||||
|
|
||||||
|
if (isAdmin || isUser) {
|
||||||
|
return this.userService.getUser(username);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private authenticateAdmin(username: string, password: string): boolean {
|
||||||
return username === MEET_ADMIN_USER && password === MEET_ADMIN_SECRET;
|
return username === MEET_ADMIN_USER && password === MEET_ADMIN_SECRET;
|
||||||
}
|
}
|
||||||
|
|
||||||
authenticateUser(username: string, password: string): boolean {
|
private authenticateUser(username: string, password: string): boolean {
|
||||||
if (MEET_PRIVATE_ACCESS === 'true') {
|
if (MEET_PRIVATE_ACCESS === 'true') {
|
||||||
return username === MEET_USER && password === MEET_SECRET;
|
return username === MEET_USER && password === MEET_SECRET;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,23 +6,24 @@ import {
|
|||||||
} from '../environment.js';
|
} from '../environment.js';
|
||||||
import { injectable } from '../config/dependency-injector.config.js';
|
import { injectable } from '../config/dependency-injector.config.js';
|
||||||
import { AccessToken, AccessTokenOptions, ClaimGrants, TokenVerifier } from 'livekit-server-sdk';
|
import { AccessToken, AccessTokenOptions, ClaimGrants, TokenVerifier } from 'livekit-server-sdk';
|
||||||
|
import { User } from '@typings-ce';
|
||||||
|
|
||||||
@injectable()
|
@injectable()
|
||||||
export class TokenService {
|
export class TokenService {
|
||||||
async generateAccessToken(username: string): Promise<string> {
|
async generateAccessToken(user: User): Promise<string> {
|
||||||
return await this.generateJwtToken(username, MEET_ACCESS_TOKEN_EXPIRATION);
|
return await this.generateJwtToken(user, MEET_ACCESS_TOKEN_EXPIRATION);
|
||||||
}
|
}
|
||||||
|
|
||||||
async generateRefreshToken(username: string): Promise<string> {
|
async generateRefreshToken(user: User): Promise<string> {
|
||||||
return await this.generateJwtToken(username, MEET_REFRESH_TOKEN_EXPIRATION);
|
return await this.generateJwtToken(user, MEET_REFRESH_TOKEN_EXPIRATION);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async generateJwtToken(username: string, expiration: string): Promise<string> {
|
private async generateJwtToken(user: User, expiration: string): Promise<string> {
|
||||||
const options: AccessTokenOptions = {
|
const options: AccessTokenOptions = {
|
||||||
identity: username,
|
identity: user.username,
|
||||||
ttl: expiration,
|
ttl: expiration,
|
||||||
metadata: JSON.stringify({
|
metadata: JSON.stringify({
|
||||||
role: 'admin'
|
role: user.role
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
const at = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET, options);
|
const at = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET, options);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user