kauth-sdk-node 2.0.4 → 2.0.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.
|
@@ -5,6 +5,9 @@ export declare class KauthApi {
|
|
|
5
5
|
private marmotUtil;
|
|
6
6
|
private cache;
|
|
7
7
|
constructor({ kauthOrigin, marmotTenantId, marmotAccessKeyId, marmotAccessKeySecret, kauthServiceName, kauthServicePort, lruMaxAge, lruMax, }: KauthConfig);
|
|
8
|
+
setTraceId(traceId: string): void;
|
|
9
|
+
setSpanId(spanId: string): void;
|
|
10
|
+
setUserId(userId: string): void;
|
|
8
11
|
moziAuthorizeURL(callback: string, state: string, prompt: string): Promise<string>;
|
|
9
12
|
moziAuthenticate(data: KauthAuthenticateRequest): Promise<KauthMoziAuthenticationInformation>;
|
|
10
13
|
moziBind(data: KauthBindRequest): Promise<void>;
|
|
@@ -21,6 +21,15 @@ class KauthApi {
|
|
|
21
21
|
});
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
|
+
setTraceId(traceId) {
|
|
25
|
+
this.marmotUtil.setTraceId(traceId);
|
|
26
|
+
}
|
|
27
|
+
setSpanId(spanId) {
|
|
28
|
+
this.marmotUtil.setSpanId(spanId);
|
|
29
|
+
}
|
|
30
|
+
setUserId(userId) {
|
|
31
|
+
this.marmotUtil.setUserId(userId);
|
|
32
|
+
}
|
|
24
33
|
async moziAuthorizeURL(callback, state, prompt) {
|
|
25
34
|
return this.marmotUtil.get(http_1.MoziAuthorizeUri, { callback, state, prompt });
|
|
26
35
|
}
|
|
@@ -35,24 +35,18 @@ export declare class MarmotUtil {
|
|
|
35
35
|
private serviceName?;
|
|
36
36
|
private servicePort?;
|
|
37
37
|
private proxyMatched;
|
|
38
|
+
private userId?;
|
|
39
|
+
private traceId?;
|
|
40
|
+
private spanId?;
|
|
38
41
|
constructor(origin: string, tenantId?: number, accessKeyId?: string, accessKeySecret?: string, serviceName?: string, servicePort?: string);
|
|
42
|
+
setUserId(userId: string): void;
|
|
43
|
+
setTraceId(traceId: string): void;
|
|
44
|
+
setSpanId(spanId: string): void;
|
|
39
45
|
get<RequestType extends ParsedUrlQueryInput | MarmotOptions, ResponseType>(path: string, params?: RequestType): Promise<ResponseType>;
|
|
40
46
|
post<RequestType extends MarmotOptions, ResponseType>(path: string, params?: RequestType, config?: AxiosRequestConfig): Promise<ResponseType>;
|
|
41
47
|
del<RequestType extends MarmotOptions | ParsedUrlQueryInput, ResponseType>(path: string, params?: RequestType): Promise<ResponseType>;
|
|
42
48
|
resolveUri(path: string, params?: any): string;
|
|
43
|
-
resolveHeaders(method: string, uri: string,
|
|
44
|
-
'x-service-name': string;
|
|
45
|
-
'x-service-port': string;
|
|
46
|
-
'x-kauth-user-id': number;
|
|
47
|
-
authorization: string;
|
|
48
|
-
'x-gw-tenant-id'?: undefined;
|
|
49
|
-
} | {
|
|
50
|
-
'x-kauth-user-id': number;
|
|
51
|
-
'x-gw-tenant-id': number;
|
|
52
|
-
'x-service-name'?: undefined;
|
|
53
|
-
'x-service-port'?: undefined;
|
|
54
|
-
authorization?: undefined;
|
|
55
|
-
};
|
|
49
|
+
resolveHeaders(method: string, uri: string): Record<string, string | number>;
|
|
56
50
|
sign(method: string, path: string, accessKeySecret?: string): string;
|
|
57
51
|
format<T>(response: AxiosResponse<BasicReponse<any>>): Promise<T>;
|
|
58
52
|
}
|
|
@@ -27,28 +27,46 @@ class MarmotUtil {
|
|
|
27
27
|
this.servicePort = servicePort;
|
|
28
28
|
this.proxyMatched = Boolean(accessKeyId && accessKeySecret && serviceName && servicePort);
|
|
29
29
|
}
|
|
30
|
+
setUserId(userId) {
|
|
31
|
+
this.userId = userId;
|
|
32
|
+
}
|
|
33
|
+
setTraceId(traceId) {
|
|
34
|
+
this.traceId = traceId;
|
|
35
|
+
}
|
|
36
|
+
setSpanId(spanId) {
|
|
37
|
+
this.spanId = spanId;
|
|
38
|
+
}
|
|
30
39
|
async get(path, params) {
|
|
31
|
-
|
|
32
|
-
|
|
40
|
+
// 兼容老的写法
|
|
41
|
+
if (params.userId) {
|
|
42
|
+
this.setUserId(String(params.userId));
|
|
43
|
+
}
|
|
44
|
+
const uri = this.resolveUri(path, params);
|
|
33
45
|
const response = await axios_1.default.get(this.origin + uri, {
|
|
34
|
-
headers: this.resolveHeaders('GET', uri
|
|
46
|
+
headers: this.resolveHeaders('GET', uri),
|
|
35
47
|
});
|
|
36
48
|
return this.format(response);
|
|
37
49
|
}
|
|
38
50
|
async post(path, params, config) {
|
|
39
|
-
|
|
51
|
+
// 兼容老的写法
|
|
52
|
+
if (params.userId) {
|
|
53
|
+
this.setUserId(String(params.userId));
|
|
54
|
+
}
|
|
40
55
|
const uri = this.resolveUri(path);
|
|
41
|
-
const response = await axios_1.default.post(this.origin + uri,
|
|
56
|
+
const response = await axios_1.default.post(this.origin + uri, params, {
|
|
42
57
|
...config,
|
|
43
|
-
headers: this.resolveHeaders('POST', uri
|
|
58
|
+
headers: this.resolveHeaders('POST', uri),
|
|
44
59
|
});
|
|
45
60
|
return this.format(response);
|
|
46
61
|
}
|
|
47
62
|
async del(path, params) {
|
|
48
|
-
|
|
49
|
-
|
|
63
|
+
// 兼容老的写法
|
|
64
|
+
if (params.userId) {
|
|
65
|
+
this.setUserId(String(params.userId));
|
|
66
|
+
}
|
|
67
|
+
const uri = this.resolveUri(path, params);
|
|
50
68
|
const response = await axios_1.default.delete(this.origin + uri, {
|
|
51
|
-
headers: this.resolveHeaders('DELETE', uri
|
|
69
|
+
headers: this.resolveHeaders('DELETE', uri),
|
|
52
70
|
});
|
|
53
71
|
return this.format(response);
|
|
54
72
|
}
|
|
@@ -62,20 +80,30 @@ class MarmotUtil {
|
|
|
62
80
|
}
|
|
63
81
|
return `${path_1.default.normalize(path)}?${querystring_1.default.stringify(params)}`;
|
|
64
82
|
}
|
|
65
|
-
resolveHeaders(method, uri
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
83
|
+
resolveHeaders(method, uri) {
|
|
84
|
+
const headers = {};
|
|
85
|
+
if (this.userId) {
|
|
86
|
+
headers['x-kauth-user-id'] = this.userId;
|
|
87
|
+
}
|
|
88
|
+
if (this.traceId) {
|
|
89
|
+
headers['tracelog-traceid'] = this.traceId;
|
|
90
|
+
}
|
|
91
|
+
if (this.spanId) {
|
|
92
|
+
headers['tracelog-spanid'] = this.spanId;
|
|
93
|
+
}
|
|
94
|
+
if (!this.proxyMatched) {
|
|
95
|
+
// 如果直连转发,需要传入 x-gw-tenant-id
|
|
96
|
+
if (this.tenantId) {
|
|
97
|
+
headers['x-gw-tenant-id'] = this.tenantId;
|
|
98
|
+
}
|
|
99
|
+
return headers;
|
|
74
100
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
101
|
+
// tenantId 包含在了 access-key-id 中
|
|
102
|
+
const signature = this.sign(method.toUpperCase(), uri, this.accessKeySecret);
|
|
103
|
+
headers['x-service-name'] = this.serviceName;
|
|
104
|
+
headers['x-service-port'] = this.servicePort;
|
|
105
|
+
headers.authorization = `Marmot ${this.accessKeyId}:${signature}`;
|
|
106
|
+
return headers;
|
|
79
107
|
}
|
|
80
108
|
sign(method, path, accessKeySecret) {
|
|
81
109
|
const str = `method:${method.toUpperCase()},url:${path}`;
|