lhisp-oauth-client 1.0.7 → 1.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -2
- package/src/lhisp-oauth-client.js +66 -45
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lhisp-oauth-client",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.11",
|
|
4
4
|
"main": "src/index",
|
|
5
5
|
"types": "src/index.d.ts",
|
|
6
6
|
"repository": "git@bitbucket.org:leandro_costa/lhisp-oauth-client.git",
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"typescript": "^5.1.3"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"axios": "^1.4.0"
|
|
22
|
+
"axios": "^1.4.0",
|
|
23
|
+
"lhisp-logger": "^1.0.11"
|
|
23
24
|
}
|
|
24
25
|
}
|
|
@@ -17,6 +17,7 @@ const querystring_1 = __importDefault(require("querystring"));
|
|
|
17
17
|
const https_1 = __importDefault(require("https"));
|
|
18
18
|
const axios_1 = __importDefault(require("axios"));
|
|
19
19
|
const lhisp_oauth_client_t_1 = require("./lhisp-oauth-client.t");
|
|
20
|
+
const lhisp_logger_1 = __importDefault(require("lhisp-logger"));
|
|
20
21
|
class LhispOauthClient {
|
|
21
22
|
constructor(params) {
|
|
22
23
|
if (params.certificado) {
|
|
@@ -69,36 +70,42 @@ class LhispOauthClient {
|
|
|
69
70
|
}
|
|
70
71
|
getAccessToken() {
|
|
71
72
|
return __awaiter(this, void 0, void 0, function* () {
|
|
72
|
-
|
|
73
|
-
|
|
73
|
+
try {
|
|
74
|
+
if (this.accessToken && this.isTokenValid(this.accessToken)) {
|
|
75
|
+
return this.accessToken;
|
|
76
|
+
}
|
|
77
|
+
// TODO: Implementar Refresh Token.
|
|
78
|
+
let authRequestOpt = {
|
|
79
|
+
method: "POST",
|
|
80
|
+
url: this.authUrl,
|
|
81
|
+
httpsAgent: this.agent,
|
|
82
|
+
headers: {
|
|
83
|
+
[this.authHeaderName]: this.getAuthHeaderValue(),
|
|
84
|
+
"Content-Type": this.authContentType,
|
|
85
|
+
},
|
|
86
|
+
data: {},
|
|
87
|
+
};
|
|
88
|
+
if (this.grantType)
|
|
89
|
+
authRequestOpt.data.grant_type = this.grantType;
|
|
90
|
+
if (this.authScope)
|
|
91
|
+
authRequestOpt.data.scope = this.authScope;
|
|
92
|
+
if (this.sendAuthCredentialsOnRequestBody) {
|
|
93
|
+
if (this.clientId)
|
|
94
|
+
authRequestOpt.data.client_id = this.clientId;
|
|
95
|
+
if (this.clientSecret)
|
|
96
|
+
authRequestOpt.data.client_secret = this.clientSecret;
|
|
97
|
+
}
|
|
98
|
+
authRequestOpt.data = this.parseData({
|
|
99
|
+
data: authRequestOpt.data,
|
|
100
|
+
contentType: this.authContentType,
|
|
101
|
+
});
|
|
102
|
+
const response = yield axios_1.default.request(authRequestOpt);
|
|
103
|
+
return this.buildAccessToken(response.data);
|
|
74
104
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
url: this.authUrl,
|
|
79
|
-
httpsAgent: this.agent,
|
|
80
|
-
headers: {
|
|
81
|
-
[this.authHeaderName]: this.getAuthHeaderValue(),
|
|
82
|
-
"Content-Type": this.authContentType,
|
|
83
|
-
},
|
|
84
|
-
data: {},
|
|
85
|
-
};
|
|
86
|
-
if (this.grantType)
|
|
87
|
-
authRequestOpt.data.grant_type = this.grantType;
|
|
88
|
-
if (this.authScope)
|
|
89
|
-
authRequestOpt.data.scope = this.authScope;
|
|
90
|
-
if (this.sendAuthCredentialsOnRequestBody) {
|
|
91
|
-
if (this.clientId)
|
|
92
|
-
authRequestOpt.data.client_id = this.clientId;
|
|
93
|
-
if (this.clientSecret)
|
|
94
|
-
authRequestOpt.data.client_secret = this.clientSecret;
|
|
105
|
+
catch (error) {
|
|
106
|
+
lhisp_logger_1.default.error({ message: "LhispOauthClient.getAccessToken", error });
|
|
107
|
+
throw error;
|
|
95
108
|
}
|
|
96
|
-
authRequestOpt.data = this.parseData({
|
|
97
|
-
data: authRequestOpt.data,
|
|
98
|
-
contentType: this.authContentType,
|
|
99
|
-
});
|
|
100
|
-
const response = yield axios_1.default.request(authRequestOpt);
|
|
101
|
-
return this.buildAccessToken(response.data);
|
|
102
109
|
});
|
|
103
110
|
}
|
|
104
111
|
buildAccessToken(data) {
|
|
@@ -112,24 +119,38 @@ class LhispOauthClient {
|
|
|
112
119
|
executarRequest({ method, path, data, params, contentType = lhisp_oauth_client_t_1.ContentType.APPLICATION_JSON, }) {
|
|
113
120
|
var _a;
|
|
114
121
|
return __awaiter(this, void 0, void 0, function* () {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
122
|
+
try {
|
|
123
|
+
yield this.getAccessToken();
|
|
124
|
+
if (!((_a = this.accessToken) === null || _a === void 0 ? void 0 : _a.token_type)) {
|
|
125
|
+
console.log("## LHOAUTH2 NO TOKEN ?:", this.accessToken);
|
|
126
|
+
}
|
|
127
|
+
let headers = {
|
|
128
|
+
"Content-Type": contentType,
|
|
129
|
+
[this.tokenHeaderName]: this.getAuthToken(),
|
|
130
|
+
// ...this.headers
|
|
131
|
+
};
|
|
132
|
+
const response = yield axios_1.default.request({
|
|
133
|
+
method,
|
|
134
|
+
url: `${this.apiUrl}${path}`,
|
|
135
|
+
httpsAgent: this.agent,
|
|
136
|
+
headers,
|
|
137
|
+
data,
|
|
138
|
+
params,
|
|
139
|
+
});
|
|
140
|
+
return response.data;
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
lhisp_logger_1.default.error({
|
|
144
|
+
message: "LhispOauthClient.executarRequest",
|
|
145
|
+
method,
|
|
146
|
+
url: `${this.apiUrl}${path}`,
|
|
147
|
+
data,
|
|
148
|
+
params,
|
|
149
|
+
contentType,
|
|
150
|
+
error,
|
|
151
|
+
});
|
|
152
|
+
throw error;
|
|
118
153
|
}
|
|
119
|
-
let headers = {
|
|
120
|
-
"Content-Type": contentType,
|
|
121
|
-
[this.tokenHeaderName]: this.getAuthToken(),
|
|
122
|
-
// ...this.headers
|
|
123
|
-
};
|
|
124
|
-
const response = yield axios_1.default.request({
|
|
125
|
-
method,
|
|
126
|
-
url: `${this.apiUrl}${path}`,
|
|
127
|
-
httpsAgent: this.agent,
|
|
128
|
-
headers,
|
|
129
|
-
data,
|
|
130
|
-
params,
|
|
131
|
-
});
|
|
132
|
-
return response.data;
|
|
133
154
|
});
|
|
134
155
|
}
|
|
135
156
|
get({ path, contentType = lhisp_oauth_client_t_1.ContentType.APPLICATION_JSON, params }) {
|