@clxmedia/credstore-client 2.0.0 → 2.0.1
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/dist/index.d.ts +21 -1
- package/dist/index.js +66 -1
- package/package.json +2 -3
- package/dist/client.d.ts +0 -21
- package/dist/client.js +0 -66
package/dist/index.d.ts
CHANGED
|
@@ -1 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
import { CredStoreBundle, CredStoreConfig, CredentialProvider, CredstoreCredential } from '@clxmedia/types/core/credstore';
|
|
2
|
+
export declare const XperienceEntityKeyValidator: (entityId: string | number, entitySecret: string, availableEntityKeys: string[]) => boolean;
|
|
3
|
+
export declare const XperienceEntityKeyGenerator: (entityId: string | number, xperienceEntityKey: string) => string;
|
|
4
|
+
export type OptionsBase = {
|
|
5
|
+
guidSecret?: string;
|
|
6
|
+
};
|
|
7
|
+
export type FetchOptions = OptionsBase & {
|
|
8
|
+
skipCache?: boolean;
|
|
9
|
+
};
|
|
10
|
+
export type UpdateOptions = OptionsBase;
|
|
11
|
+
export declare class CredStoreClient {
|
|
12
|
+
private apiServer;
|
|
13
|
+
private apiKey?;
|
|
14
|
+
constructor(config: CredStoreConfig);
|
|
15
|
+
buildHeaders(guidSecret?: string, skipCache?: boolean): {
|
|
16
|
+
[x: string]: string;
|
|
17
|
+
};
|
|
18
|
+
fetchCredential(guid: string, fetchOptions?: FetchOptions): Promise<CredStoreBundle>;
|
|
19
|
+
createCredential(provider: CredentialProvider, credData: CredstoreCredential): Promise<any>;
|
|
20
|
+
updateCredential(guid: string, data: any, updateOptions?: UpdateOptions): Promise<any>;
|
|
21
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -1 +1,66 @@
|
|
|
1
|
-
|
|
1
|
+
import { DateTime } from 'luxon';
|
|
2
|
+
import { createHash } from 'crypto';
|
|
3
|
+
import axios from 'axios';
|
|
4
|
+
export const XperienceEntityKeyValidator = (entityId, entitySecret, availableEntityKeys) => {
|
|
5
|
+
const now = DateTime.local().startOf('minute').toSeconds();
|
|
6
|
+
const timeRange = Array.from(Array(9).keys()).map((i) => now + (i - 4) * 60);
|
|
7
|
+
return timeRange.some((timeSalt) => {
|
|
8
|
+
return availableEntityKeys.some((entityKey) => {
|
|
9
|
+
return entitySecret === sha256Hash(`${entityId}:${entityKey}`, timeSalt.toString());
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
export const XperienceEntityKeyGenerator = (entityId, xperienceEntityKey) => {
|
|
14
|
+
const timeSalt = DateTime.local().startOf('minute').toSeconds();
|
|
15
|
+
return sha256Hash(`${entityId}:${xperienceEntityKey}`, timeSalt.toString());
|
|
16
|
+
};
|
|
17
|
+
const sha256Hash = (input, salt) => {
|
|
18
|
+
return createHash('sha256')
|
|
19
|
+
.update(input)
|
|
20
|
+
.update(createHash('sha256').update(salt, 'utf8').digest('hex'))
|
|
21
|
+
.digest('hex');
|
|
22
|
+
};
|
|
23
|
+
export class CredStoreClient {
|
|
24
|
+
constructor(config) {
|
|
25
|
+
this.apiServer = config.api_server;
|
|
26
|
+
this.apiKey = config.api_key;
|
|
27
|
+
}
|
|
28
|
+
buildHeaders(guidSecret, skipCache) {
|
|
29
|
+
const headers = {
|
|
30
|
+
'x-credstore-skip-cache': `${skipCache}`,
|
|
31
|
+
};
|
|
32
|
+
if (this.apiKey) {
|
|
33
|
+
headers['x-credstore-admin-key'] = this.apiKey;
|
|
34
|
+
}
|
|
35
|
+
if (guidSecret) {
|
|
36
|
+
headers['x-credstore-secret'] = guidSecret;
|
|
37
|
+
}
|
|
38
|
+
return headers;
|
|
39
|
+
}
|
|
40
|
+
async fetchCredential(guid, fetchOptions) {
|
|
41
|
+
if (!fetchOptions) {
|
|
42
|
+
throw new Error('fetchOptions is required');
|
|
43
|
+
}
|
|
44
|
+
const { guidSecret, skipCache } = fetchOptions;
|
|
45
|
+
const response = await axios.get(`${this.apiServer}/credstore/credentials/${guid}`, {
|
|
46
|
+
headers: this.buildHeaders(guidSecret, skipCache),
|
|
47
|
+
});
|
|
48
|
+
return response.data;
|
|
49
|
+
}
|
|
50
|
+
async createCredential(provider, credData) {
|
|
51
|
+
const response = await axios.post(`${this.apiServer}/credstore/credentials/${provider}`, credData, {
|
|
52
|
+
headers: this.buildHeaders(),
|
|
53
|
+
});
|
|
54
|
+
return response.data;
|
|
55
|
+
}
|
|
56
|
+
async updateCredential(guid, data, updateOptions) {
|
|
57
|
+
if (!updateOptions) {
|
|
58
|
+
throw new Error('updateOptions is required');
|
|
59
|
+
}
|
|
60
|
+
const { guidSecret } = updateOptions;
|
|
61
|
+
const response = await axios.patch(`${this.apiServer}/credstore/credentials/${guid}`, data, {
|
|
62
|
+
headers: this.buildHeaders(guidSecret),
|
|
63
|
+
});
|
|
64
|
+
return response.data;
|
|
65
|
+
}
|
|
66
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clxmedia/credstore-client",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "CLXperience Credstore Client",
|
|
5
5
|
"author": "Brandon Thompson <brandont@clxmedia.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -15,8 +15,7 @@
|
|
|
15
15
|
".": {
|
|
16
16
|
"import": "./dist/index.js",
|
|
17
17
|
"require": "./dist/index.js"
|
|
18
|
-
}
|
|
19
|
-
"./client": "./dist/client.js"
|
|
18
|
+
}
|
|
20
19
|
},
|
|
21
20
|
"scripts": {
|
|
22
21
|
"start:dev": "tsc -w",
|
package/dist/client.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { CredStoreBundle, CredStoreConfig, CredentialProvider, CredstoreCredential } from '@clxmedia/types/core/credstore';
|
|
2
|
-
export declare const XperienceEntityKeyValidator: (entityId: string | number, entitySecret: string, availableEntityKeys: string[]) => boolean;
|
|
3
|
-
export declare const XperienceEntityKeyGenerator: (entityId: string | number, xperienceEntityKey: string) => string;
|
|
4
|
-
export type OptionsBase = {
|
|
5
|
-
guidSecret?: string;
|
|
6
|
-
};
|
|
7
|
-
export type FetchOptions = OptionsBase & {
|
|
8
|
-
skipCache?: boolean;
|
|
9
|
-
};
|
|
10
|
-
export type UpdateOptions = OptionsBase;
|
|
11
|
-
export declare class CredStoreClient {
|
|
12
|
-
private apiServer;
|
|
13
|
-
private apiKey?;
|
|
14
|
-
constructor(config: CredStoreConfig);
|
|
15
|
-
buildHeaders(guidSecret?: string, skipCache?: boolean): {
|
|
16
|
-
[x: string]: string;
|
|
17
|
-
};
|
|
18
|
-
fetchCredential(guid: string, fetchOptions?: FetchOptions): Promise<CredStoreBundle>;
|
|
19
|
-
createCredential(provider: CredentialProvider, credData: CredstoreCredential): Promise<any>;
|
|
20
|
-
updateCredential(guid: string, data: any, updateOptions?: UpdateOptions): Promise<any>;
|
|
21
|
-
}
|
package/dist/client.js
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { DateTime } from 'luxon';
|
|
2
|
-
import { createHash } from 'crypto';
|
|
3
|
-
import axios from 'axios';
|
|
4
|
-
export const XperienceEntityKeyValidator = (entityId, entitySecret, availableEntityKeys) => {
|
|
5
|
-
const now = DateTime.local().startOf('minute').toSeconds();
|
|
6
|
-
const timeRange = Array.from(Array(9).keys()).map((i) => now + (i - 4) * 60);
|
|
7
|
-
return timeRange.some((timeSalt) => {
|
|
8
|
-
return availableEntityKeys.some((entityKey) => {
|
|
9
|
-
return entitySecret === sha256Hash(`${entityId}:${entityKey}`, timeSalt.toString());
|
|
10
|
-
});
|
|
11
|
-
});
|
|
12
|
-
};
|
|
13
|
-
export const XperienceEntityKeyGenerator = (entityId, xperienceEntityKey) => {
|
|
14
|
-
const timeSalt = DateTime.local().startOf('minute').toSeconds();
|
|
15
|
-
return sha256Hash(`${entityId}:${xperienceEntityKey}`, timeSalt.toString());
|
|
16
|
-
};
|
|
17
|
-
const sha256Hash = (input, salt) => {
|
|
18
|
-
return createHash('sha256')
|
|
19
|
-
.update(input)
|
|
20
|
-
.update(createHash('sha256').update(salt, 'utf8').digest('hex'))
|
|
21
|
-
.digest('hex');
|
|
22
|
-
};
|
|
23
|
-
export class CredStoreClient {
|
|
24
|
-
constructor(config) {
|
|
25
|
-
this.apiServer = config.api_server;
|
|
26
|
-
this.apiKey = config.api_key;
|
|
27
|
-
}
|
|
28
|
-
buildHeaders(guidSecret, skipCache) {
|
|
29
|
-
const headers = {
|
|
30
|
-
'x-credstore-skip-cache': `${skipCache}`,
|
|
31
|
-
};
|
|
32
|
-
if (this.apiKey) {
|
|
33
|
-
headers['x-credstore-admin-key'] = this.apiKey;
|
|
34
|
-
}
|
|
35
|
-
if (guidSecret) {
|
|
36
|
-
headers['x-credstore-secret'] = guidSecret;
|
|
37
|
-
}
|
|
38
|
-
return headers;
|
|
39
|
-
}
|
|
40
|
-
async fetchCredential(guid, fetchOptions) {
|
|
41
|
-
if (!fetchOptions) {
|
|
42
|
-
throw new Error('fetchOptions is required');
|
|
43
|
-
}
|
|
44
|
-
const { guidSecret, skipCache } = fetchOptions;
|
|
45
|
-
const response = await axios.get(`${this.apiServer}/credstore/credentials/${guid}`, {
|
|
46
|
-
headers: this.buildHeaders(guidSecret, skipCache),
|
|
47
|
-
});
|
|
48
|
-
return response.data;
|
|
49
|
-
}
|
|
50
|
-
async createCredential(provider, credData) {
|
|
51
|
-
const response = await axios.post(`${this.apiServer}/credstore/credentials/${provider}`, credData, {
|
|
52
|
-
headers: this.buildHeaders(),
|
|
53
|
-
});
|
|
54
|
-
return response.data;
|
|
55
|
-
}
|
|
56
|
-
async updateCredential(guid, data, updateOptions) {
|
|
57
|
-
if (!updateOptions) {
|
|
58
|
-
throw new Error('updateOptions is required');
|
|
59
|
-
}
|
|
60
|
-
const { guidSecret } = updateOptions;
|
|
61
|
-
const response = await axios.patch(`${this.apiServer}/credstore/credentials/${guid}`, data, {
|
|
62
|
-
headers: this.buildHeaders(guidSecret),
|
|
63
|
-
});
|
|
64
|
-
return response.data;
|
|
65
|
-
}
|
|
66
|
-
}
|