feat: Manage Images using Minio Service
This commit is contained in:
parent
5e5ecb6cd1
commit
bdbbaeaa65
@ -3,14 +3,12 @@ import { HttpService } from '@nestjs/axios';
|
|||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import { firstValueFrom } from 'rxjs';
|
import { firstValueFrom } from 'rxjs';
|
||||||
import { UserInfo, UserServiceClient } from '../interfaces/user.service.interface';
|
import { UserInfo, UserServiceClient } from '../interfaces/user.service.interface';
|
||||||
|
import { KeycloakConfig } from 'src/config/keycloak.config';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class HttpUserServiceClient implements UserServiceClient {
|
export class HttpUserServiceClient implements UserServiceClient {
|
||||||
private readonly baseUrl: string;
|
private readonly baseUrl: string;
|
||||||
private readonly keycloakUrl: string;
|
private readonly keycloakConfig: KeycloakConfig;
|
||||||
private readonly keycloakRealm: string;
|
|
||||||
private readonly clientId: string;
|
|
||||||
private readonly clientSecret: string;
|
|
||||||
|
|
||||||
private accessToken: string | null = null;
|
private accessToken: string | null = null;
|
||||||
private tokenExpiry: number = 0;
|
private tokenExpiry: number = 0;
|
||||||
@ -19,21 +17,17 @@ export class HttpUserServiceClient implements UserServiceClient {
|
|||||||
private readonly httpService: HttpService,
|
private readonly httpService: HttpService,
|
||||||
private readonly configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
) {
|
) {
|
||||||
|
this.keycloakConfig = this.getKeycloakConfig();
|
||||||
this.baseUrl = this.configService.get<string>('USER_SERVICE') || 'http://localhost:3001';
|
this.baseUrl = this.configService.get<string>('USER_SERVICE') || 'http://localhost:3001';
|
||||||
|
|
||||||
const keycloakUrl = this.configService.get<string>('KEYCLOAK_SERVER_URL');
|
|
||||||
const keycloakRealm = this.configService.get<string>('KEYCLOAK_REALM');
|
|
||||||
const clientId = this.configService.get<string>('KEYCLOAK_CLIENT_ID');
|
|
||||||
const clientSecret = this.configService.get<string>('KEYCLOAK_CLIENT_SECRET');
|
|
||||||
|
|
||||||
if (!keycloakUrl || !keycloakRealm || !clientId || !clientSecret) {
|
|
||||||
throw new Error('Missing required Keycloak configuration');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.keycloakUrl = keycloakUrl;
|
// === CONFIGURATION ===
|
||||||
this.keycloakRealm = keycloakRealm;
|
private getKeycloakConfig(): KeycloakConfig {
|
||||||
this.clientId = clientId;
|
const config = this.configService.get<KeycloakConfig>('keycloak');
|
||||||
this.clientSecret = clientSecret;
|
if (!config) {
|
||||||
|
throw new Error('Keycloak configuration not found');
|
||||||
|
}
|
||||||
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getAccessToken(): Promise<string> {
|
private async getAccessToken(): Promise<string> {
|
||||||
@ -43,12 +37,12 @@ export class HttpUserServiceClient implements UserServiceClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const tokenUrl = `${this.keycloakUrl}/realms/${this.keycloakRealm}/protocol/openid-connect/token`;
|
const tokenUrl = `${this.keycloakConfig.serverUrl}/realms/${this.keycloakConfig.realm}/protocol/openid-connect/token`;
|
||||||
|
|
||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
params.append('grant_type', 'client_credentials');
|
params.append('grant_type', 'client_credentials');
|
||||||
params.append('client_id', this.clientId);
|
params.append('client_id', this.keycloakConfig.authClientId);
|
||||||
params.append('client_secret', this.clientSecret);
|
params.append('client_secret', this.keycloakConfig.authClientSecret);
|
||||||
|
|
||||||
const response = await firstValueFrom(
|
const response = await firstValueFrom(
|
||||||
this.httpService.post(tokenUrl, params.toString(), {
|
this.httpService.post(tokenUrl, params.toString(), {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user