Tratando grantType padrao, e enviando no corpo da request

This commit is contained in:
Leandro Costa 2023-01-26 23:06:00 -03:00
parent 05b2091b0a
commit a72fe1d9c8

View file

@ -4,9 +4,10 @@ import axios, { AxiosRequestConfig } from 'axios';
import { import {
AccessToken, ContentType, defaultAuthContentType, AccessToken, ContentType, defaultAuthContentType,
defaultAuthHeaderName, defaultAuthHeaderName,
defaultGrantType,
defaultTokenHeaderName, defaultTokenHeaderName,
ExecutarRequestParams, ExecutarRequestParams,
GrantType, LhispOauthClientConstructorParams, LhispOauthClientConstructorParams,
} from "./lhisp-oauth-client.t"; } from "./lhisp-oauth-client.t";
export default class LhispOauthClient { export default class LhispOauthClient {
@ -21,7 +22,7 @@ export default class LhispOauthClient {
protected senhaCertificado?: string; protected senhaCertificado?: string;
protected authScope?: string; protected authScope?: string;
protected headers?: Headers; protected headers?: Headers;
protected grantType?: GrantType; protected grantType?: string;
protected agent: https.Agent; protected agent: https.Agent;
protected accessToken?: AccessToken; protected accessToken?: AccessToken;
protected refreshToken?: AccessToken; protected refreshToken?: AccessToken;
@ -46,7 +47,7 @@ export default class LhispOauthClient {
this.apiUrl = params.apiUrl; this.apiUrl = params.apiUrl;
this.authUrl = params.authUrl; this.authUrl = params.authUrl;
this.authScope = params.authScope; this.authScope = params.authScope;
this.grantType = params.grantType; this.grantType = params.grantType || defaultGrantType;
this.authContentType = params.authContentType || defaultAuthContentType; this.authContentType = params.authContentType || defaultAuthContentType;
this.clientId = params.clientId; this.clientId = params.clientId;
this.clientSecret = params.clientSecret; this.clientSecret = params.clientSecret;
@ -60,6 +61,8 @@ export default class LhispOauthClient {
} }
parseData({ data, contentType = ContentType.APPLICATION_JSON }: { data: any, contentType: string }) { parseData({ data, contentType = ContentType.APPLICATION_JSON }: { data: any, contentType: string }) {
if(!data || Object.keys(data).length===0) return undefined;
switch (contentType) { switch (contentType) {
case ContentType.APPLICATION_JSON: return JSON.stringify(data); case ContentType.APPLICATION_JSON: return JSON.stringify(data);
case ContentType.APPLICATION_X_WWW_FORM_URLENCODED: return qs.stringify(data); case ContentType.APPLICATION_X_WWW_FORM_URLENCODED: return qs.stringify(data);
@ -90,18 +93,18 @@ export default class LhispOauthClient {
[this.authHeaderName]: this.getAuthHeaderValue(), [this.authHeaderName]: this.getAuthHeaderValue(),
'Content-Type': this.authContentType, 'Content-Type': this.authContentType,
}, },
data: {},
} }
if(this.grantType) authRequestOpt.data.grant_type = this.grantType;
if(this.authScope) authRequestOpt.data.scope = this.authScope;
if(this.sendAuthCredentialsOnRequestBody) { if(this.sendAuthCredentialsOnRequestBody) {
let data: any = {}; if(this.clientId) authRequestOpt.data.client_id = this.clientId;
if(this.grantType) data.grant_type = this.grantType; if(this.clientSecret) authRequestOpt.data.client_secret = this.clientSecret;
if(this.authScope) data.scope = this.authScope;
if(this.clientId) data.client_id = this.clientId;
if(this.clientSecret) data.client_secret = this.clientSecret;
authRequestOpt.data = this.parseData({ data, contentType: this.authContentType });
} }
authRequestOpt.data = this.parseData({ data: authRequestOpt.data, contentType: this.authContentType });
const response = await axios.request(authRequestOpt); const response = await axios.request(authRequestOpt);
return this.buildAccessToken(response.data); return this.buildAccessToken(response.data);
} }