import { HttpService , Injectable } from '@nestjs/common' ; import { AppConfigService as ConfigService } from '../../ConfigModule/service/app-config.service' ; import { GeneratedTokenDTO } from '../dto/generated-token.dto' ; import { NewUserDTO } from '../dto/new-user.dto' ; import { AxiosResponse } from 'axios' ; import { LoginPasswordDTO } from '../dto/login-password.dto' ; import { UserJWTDTO } from '../../CommonsModule/dto/user-jwt.dto' ; @ Injectable ( ) export class SecurityIntegration { constructor ( private readonly http , private readonly config ) { } public async getAccessToken ( ) { const clientCredentialsBase64 = this . config . getClientCredentialsBase64 ( ) ; const headers = { Authorization : `Basic ${ clientCredentialsBase64 }` , } ; const body = { grant_type : 'client_credentials' , } ; const { data : { accessToken } , } = await this . http . post < GeneratedTokenDTO > ( this . config . securityOauthTokenUrl , body , { headers , } ) . toPromise ( ) ; return accessToken ; } public async refreshToken ( { authorizationHeader , refreshToken , } ) { const body = { grant_type : 'refresh_token' , refresh_token : refreshToken , } ; const headers = { Authorization : authorizationHeader , } ; return await this . http . post < GeneratedTokenDTO > ( this . config . securityOauthTokenUrl , body , { headers , } ) . toPromise ( ) ; } public async userLogin ( { base64Login , ... params } ) { const headers = { authorization : `Basic ${ base64Login }` , } ; const body = { grant_type : 'password' , ... params , } ; return await this . http . post < GeneratedTokenDTO > ( this . config . securityOauthTokenUrl , body , { headers , } ) . toPromise ( ) ; } public async clientCredentialsLogin ( { base64Login , } ) { const headers = { authorization : `Basic ${ base64Login }` , } ; const body = { grant_type : 'client_credentials' , } ; return await this . http . post < GeneratedTokenDTO > ( this . config . securityOauthTokenUrl , body , { headers , } ) . toPromise ( ) ; } public async addNewStudent ( params ) { const accessToken = await this . getAccessToken ( ) ; const headers = { authorization : `Bearer ${ accessToken }` , } ; const body = { ... params , roleName : 'STUDENT' , } ; console . log ( 'fazendo request pra salvar o usuário' , body , accessToken ) ; return await this . http . post ( this . config . securityAddNewUserUrl , body , { headers , } ) . toPromise ( ) ; } getTokenDetails ( authorizationHeader ) { return this . http . post < UserJWTDTO > ( this . config . securityOauthTokenDetailsUrl , null , { headers : { authorization : authorizationHeader } , } ) . toPromise ( ) ; } }