attlaz-client 1.17.3 → 1.17.5
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.
|
@@ -41,7 +41,7 @@ export class QueryString {
|
|
|
41
41
|
if (Array.isArray(value)) {
|
|
42
42
|
value = value.join(',');
|
|
43
43
|
}
|
|
44
|
-
output.push(parameter.parameter + '=' + value);
|
|
44
|
+
output.push(parameter.parameter + '=' + encodeURIComponent(value));
|
|
45
45
|
}
|
|
46
46
|
if (this.command.includes('?')) {
|
|
47
47
|
return this.command + '&' + output.join('&');
|
package/dist/Http/HttpClient.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// import {fetch} from 'popsicle';
|
|
2
1
|
import axios from 'axios';
|
|
3
2
|
import { HttpClientResponse } from './HttpClientResponse.js';
|
|
4
3
|
import { ClientError } from './ClientError.js';
|
|
@@ -14,8 +13,8 @@ export class HttpClient {
|
|
|
14
13
|
static HTTP_UNAVAILABLE = 503;
|
|
15
14
|
static HTTP_TIMEOUT = 504;
|
|
16
15
|
static async request(request) {
|
|
16
|
+
// TODo: should we catch axios error sand make them into ClientErrors?
|
|
17
17
|
const response = await HttpClient.axiosRequest(request);
|
|
18
|
-
// const response: HttpClientResponse = await HttpClient.superAgentRequest(request);
|
|
19
18
|
const error = ClientError.byStatus(response.status, response.statusText);
|
|
20
19
|
if (error !== null && error !== undefined) {
|
|
21
20
|
if (response.body !== null && response.body !== undefined && response.body.error !== null && response.body.error !== undefined && response.body.error.message !== null && response.body.error.message !== undefined) {
|
|
@@ -40,97 +39,9 @@ export class HttpClient {
|
|
|
40
39
|
const headerValue = rawResponse.headers[header];
|
|
41
40
|
httpResponse.headers[header] = headerValue === null ? '' : headerValue;
|
|
42
41
|
});
|
|
43
|
-
// console.log('Raw', rawResponse.data);
|
|
44
|
-
// console.log('Is json', HttpClient.isJson(httpResponse));
|
|
45
42
|
httpResponse.body = await rawResponse.data;
|
|
46
|
-
// if (HttpClient.isJson(httpResponse)) {
|
|
47
|
-
// let jsonData: object | null = null;
|
|
48
|
-
// if (httpResponse.body !== '') {
|
|
49
|
-
// try {
|
|
50
|
-
// jsonData = JSON.parse(httpResponse.body);
|
|
51
|
-
// } catch (e) {
|
|
52
|
-
// // console.error(, {statusText: httpResponse.statusText, error: e});
|
|
53
|
-
// throw new Error('Unable to parse response data to JSON');
|
|
54
|
-
// }
|
|
55
|
-
// }
|
|
56
|
-
// httpResponse.body = jsonData;
|
|
57
|
-
// } else {
|
|
58
|
-
// // response.body = rawData;
|
|
59
|
-
// }
|
|
60
43
|
return httpResponse;
|
|
61
44
|
}
|
|
62
|
-
// private static async popsicleRequest(request: HttpClientRequest): Promise<HttpClientResponse> {
|
|
63
|
-
// const rawRequest: CommonRequestOptions<any> = {
|
|
64
|
-
// url: request.getFullUrl(),
|
|
65
|
-
// method: request.method,
|
|
66
|
-
// headers: request.headers,
|
|
67
|
-
// omitDefaultHeaders: true,
|
|
68
|
-
// body: request.body,
|
|
69
|
-
// } as CommonRequestOptions<any>;
|
|
70
|
-
// const rawResponse: PopsicleResponse = await fetch(request.getFullUrl(), rawRequest);
|
|
71
|
-
// const httpResponse: HttpClientResponse = new HttpClientResponse(rawResponse.status, rawResponse.statusText);
|
|
72
|
-
// for (const header of rawResponse.headers.keys()) {
|
|
73
|
-
// const headerValue: string | null = rawResponse.headers.get(header);
|
|
74
|
-
// httpResponse.headers[header] = headerValue === null ? '' : headerValue;
|
|
75
|
-
// }
|
|
76
|
-
//
|
|
77
|
-
// httpResponse.body = await rawResponse.text();
|
|
78
|
-
// if (HttpClient.isJson(httpResponse)) {
|
|
79
|
-
// let jsonData: object | null = null;
|
|
80
|
-
// if (httpResponse.body !== '') {
|
|
81
|
-
// try {
|
|
82
|
-
// jsonData = JSON.parse(httpResponse.body);
|
|
83
|
-
// } catch (e) {
|
|
84
|
-
// // console.error(, {statusText: httpResponse.statusText, error: e});
|
|
85
|
-
// throw new Error('Unable to parse response data to JSON');
|
|
86
|
-
// }
|
|
87
|
-
// }
|
|
88
|
-
// httpResponse.body = jsonData;
|
|
89
|
-
// } else {
|
|
90
|
-
// // response.body = rawData;
|
|
91
|
-
// }
|
|
92
|
-
//
|
|
93
|
-
//
|
|
94
|
-
// return httpResponse;
|
|
95
|
-
// }
|
|
96
|
-
// private static async superAgentRequest(request: HttpClientRequest): Promise<HttpClientResponse> {
|
|
97
|
-
//
|
|
98
|
-
//
|
|
99
|
-
// let x: SuperAgent.Request | null = null;
|
|
100
|
-
// if (request.method === HttpClientRequest.GET) {
|
|
101
|
-
// x = SuperAgent.get(request.getFullUrl());
|
|
102
|
-
// } else if (request.method === HttpClientRequest.POST) {
|
|
103
|
-
// x = SuperAgent.post(request.getFullUrl());
|
|
104
|
-
//
|
|
105
|
-
// } else if (request.method === HttpClientRequest.PUT) {
|
|
106
|
-
// x = SuperAgent.put(request.getFullUrl());
|
|
107
|
-
// } else {
|
|
108
|
-
// throw new Error('Unknown request method `' + request.method + '`');
|
|
109
|
-
// }
|
|
110
|
-
//
|
|
111
|
-
//
|
|
112
|
-
// if (x === null) {
|
|
113
|
-
// throw new Error('Hm');
|
|
114
|
-
// }
|
|
115
|
-
//
|
|
116
|
-
//
|
|
117
|
-
// for (const key in request.headers) {
|
|
118
|
-
// if (Object.prototype.hasOwnProperty.call(request.headers, key)) {
|
|
119
|
-
// console.log('Set ' + key + ': ' + request.headers[key]);
|
|
120
|
-
// x.set(key, request.headers[key] as string);
|
|
121
|
-
// }
|
|
122
|
-
// }
|
|
123
|
-
//
|
|
124
|
-
//
|
|
125
|
-
// const rawResponse: SuperAgent.Response = await x.send(request.body);
|
|
126
|
-
//
|
|
127
|
-
//
|
|
128
|
-
// const response: HttpClientResponse = new HttpClientResponse(rawResponse.statusCode, '');
|
|
129
|
-
// response.body = rawResponse.body;
|
|
130
|
-
// // TODO: append/validate headers
|
|
131
|
-
// response.headers = rawResponse.headers;
|
|
132
|
-
// return response;
|
|
133
|
-
// }
|
|
134
45
|
static isJson(response) {
|
|
135
46
|
return response.getContentType() === 'application/json';
|
|
136
47
|
}
|
|
@@ -2,7 +2,6 @@ import { OAuthClientOptions } from './OAuthClientOptions.js';
|
|
|
2
2
|
import { OAuthClientToken } from './OAuthClientToken.js';
|
|
3
3
|
import { Parameters } from './Data/Parameters.js';
|
|
4
4
|
import { Headers } from './Data/Headers.js';
|
|
5
|
-
import { QueryString } from './Data/QueryString.js';
|
|
6
5
|
export declare class OAuthClient {
|
|
7
6
|
private readonly options;
|
|
8
7
|
private debug;
|
|
@@ -23,7 +22,6 @@ export declare class OAuthClient {
|
|
|
23
22
|
enableDebug(): void;
|
|
24
23
|
disableDebug(): void;
|
|
25
24
|
isDebugEnabled(): boolean;
|
|
26
|
-
getAuthorizationUrl(options?: QueryString | null): string;
|
|
27
25
|
getDefaultHeaders(): Headers;
|
|
28
26
|
private isNodeEnvironment;
|
|
29
27
|
private tokenToOauthClientToken;
|
package/dist/Http/OAuthClient.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// import ClientOAuth2, {Token} from 'client-oauth2';
|
|
2
1
|
import { clientCredentials, ownerCredentials, refreshToken } from 'axios-oauth-client';
|
|
3
2
|
import axios from 'axios';
|
|
4
3
|
import { ClientError } from './ClientError.js';
|
|
@@ -7,27 +6,14 @@ import { HttpClientRequest } from './HttpClientRequest.js';
|
|
|
7
6
|
import { OAuthClientToken } from './OAuthClientToken.js';
|
|
8
7
|
import { JsonSerializable } from '../Model/JsonSerializable.js';
|
|
9
8
|
import { VERSION } from '../version.js';
|
|
10
|
-
import { QueryString } from './Data/QueryString.js';
|
|
11
9
|
export class OAuthClient {
|
|
12
10
|
options;
|
|
13
11
|
debug = false;
|
|
14
12
|
axiosInstance = null;
|
|
15
|
-
// private oauthClient: ClientOAuth2;
|
|
16
|
-
// private oauthToken: Token | null = null;
|
|
17
13
|
oathClientToken = null;
|
|
18
14
|
refreshTokenPromise = null;
|
|
19
15
|
constructor(options) {
|
|
20
16
|
this.options = options;
|
|
21
|
-
// this.oauthClient = new ClientOAuth2({
|
|
22
|
-
// clientId: options.clientId === null ? undefined : options.clientId,
|
|
23
|
-
// clientSecret: options.clientSecret === null ? undefined : options.clientSecret,
|
|
24
|
-
// accessTokenUri: this.getUri(options.accessTokenUri),
|
|
25
|
-
// authorizationUri: this.getUri(options.authorizationUri),
|
|
26
|
-
// redirectUri: options.redirectUri,
|
|
27
|
-
// scopes: options.scopes,
|
|
28
|
-
// state: options.state,
|
|
29
|
-
// headers: this.getDefaultHeaders(),
|
|
30
|
-
// });
|
|
31
17
|
}
|
|
32
18
|
async authenticate(username = null, password = null) {
|
|
33
19
|
// TODO: should we encrypt the password in the client (or is https save enough)?
|
|
@@ -35,20 +21,15 @@ export class OAuthClient {
|
|
|
35
21
|
if (username !== null && username !== undefined && password !== null && password !== undefined) {
|
|
36
22
|
// Password flow / Owner Credentials grant
|
|
37
23
|
const getOwnerCredentials = ownerCredentials(this.getAxiosInstance(), this.getApiEndpointUrl(this.options.accessTokenUri), this.options.clientId === null ? undefined : this.options.clientId, this.options.clientSecret === null ? undefined : this.options.clientSecret);
|
|
38
|
-
const rawAuthToken = await getOwnerCredentials(username, password, '
|
|
24
|
+
const rawAuthToken = await getOwnerCredentials(username, password, this.options.scopes.join(' '));
|
|
39
25
|
this.oathClientToken = this.tokenToOauthClientToken(rawAuthToken);
|
|
40
|
-
// accessToken = await this.oauthClient.owner.getToken(username, password);
|
|
41
26
|
return true;
|
|
42
27
|
}
|
|
43
28
|
// Client credentials flow
|
|
44
29
|
const getClientCredentials = clientCredentials(this.getAxiosInstance(), this.getApiEndpointUrl(this.options.accessTokenUri), this.options.clientId === null ? undefined : this.options.clientId, this.options.clientSecret === null ? undefined : this.options.clientSecret);
|
|
45
|
-
const rawAuthToken = await getClientCredentials('
|
|
30
|
+
const rawAuthToken = await getClientCredentials(this.options.scopes.join(' '));
|
|
46
31
|
this.oathClientToken = this.tokenToOauthClientToken(rawAuthToken);
|
|
47
|
-
// accessToken = await this.oauthClient.credentials.getToken();
|
|
48
32
|
return true;
|
|
49
|
-
// this.oauthToken = accessToken;
|
|
50
|
-
// this.oathClientToken = this.tokenToOauthClientToken(rawAuthToken);
|
|
51
|
-
// return true;
|
|
52
33
|
}
|
|
53
34
|
catch (e) {
|
|
54
35
|
if (this.debug) {
|
|
@@ -69,8 +50,7 @@ export class OAuthClient {
|
|
|
69
50
|
}
|
|
70
51
|
try {
|
|
71
52
|
const getRefreshToken = refreshToken(this.getAxiosInstance(), this.getApiEndpointUrl(this.options.accessTokenUri), this.options.clientId === null ? undefined : this.options.clientId, this.options.clientSecret === null ? undefined : this.options.clientSecret);
|
|
72
|
-
const auth = await getRefreshToken(this.oathClientToken.refresh_token, '
|
|
73
|
-
// this.oauthToken = await this.oauthToken.refresh();
|
|
53
|
+
const auth = await getRefreshToken(this.oathClientToken.refresh_token, this.options.scopes.join(' '));
|
|
74
54
|
this.oathClientToken = this.tokenToOauthClientToken(auth);
|
|
75
55
|
}
|
|
76
56
|
catch (e) {
|
|
@@ -144,22 +124,9 @@ export class OAuthClient {
|
|
|
144
124
|
return this.oathClientToken;
|
|
145
125
|
}
|
|
146
126
|
setToken(token) {
|
|
147
|
-
// const data: {} = {
|
|
148
|
-
// access_token: token.access_token,
|
|
149
|
-
// token_type: token.token_type,
|
|
150
|
-
// refresh_token: token.refresh_token,
|
|
151
|
-
// scope: token.scope,
|
|
152
|
-
//
|
|
153
|
-
// };
|
|
154
|
-
// const oauthToken: Token = this.oauthClient.createToken(data);
|
|
155
|
-
// if (token.expires !== null && token.expires !== undefined) {
|
|
156
|
-
// oauthToken.expiresIn(token.expires);
|
|
157
|
-
// }
|
|
158
|
-
// this.oauthToken = oauthToken;
|
|
159
127
|
this.oathClientToken = token;
|
|
160
128
|
}
|
|
161
129
|
unsetToken() {
|
|
162
|
-
// this.oauthToken = null;
|
|
163
130
|
this.oathClientToken = null;
|
|
164
131
|
}
|
|
165
132
|
enableDebug() {
|
|
@@ -171,13 +138,6 @@ export class OAuthClient {
|
|
|
171
138
|
isDebugEnabled() {
|
|
172
139
|
return this.debug;
|
|
173
140
|
}
|
|
174
|
-
getAuthorizationUrl(options = null) {
|
|
175
|
-
if (options === null) {
|
|
176
|
-
options = new QueryString();
|
|
177
|
-
}
|
|
178
|
-
options.setCommand(this.getApiEndpointUrl(this.options.authorizationUri));
|
|
179
|
-
return options.build();
|
|
180
|
-
}
|
|
181
141
|
getDefaultHeaders() {
|
|
182
142
|
const userAgentInfo = {
|
|
183
143
|
version: VERSION,
|