-
Notifications
You must be signed in to change notification settings - Fork 0
/
accessToken.ts
70 lines (63 loc) · 4.78 KB
/
accessToken.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* Example of how to get an access token for B2B (and B2B2C)
*/
import { SnapBI } from "../src/core/SnapBI";
import { APISecurity } from "../src/services/requestor/APISecurity"
/**
* This is a valid Private and Public Key pair for testing purposes.
*/
const config = {
baseUrl: 'http://localhost:3000',
clientId: 'c58bab96-9a67-41ba-aeda-61a99e59a26c',
clientSecret: '0e896ade-591c-4e21-bcd3-cf2d0ebf316a',
privateKey: 'LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCk1JSUV2Z0lCQURBTkJna3Foa2lHOXcwQkFRRUZBQVNDQktnd2dnU2tBZ0VBQW9JQkFRRFl6MjY4eXhQTnRudWEKWUlMekhnZDQxdnA4eDM1Q0R4UUIwYVR0akxvV1NzMEdSbjdKdkswQ2FRRHUya0RJSkZDVC9ldWIydkFHcDhqNApzQVIxdTEwWEtsVnpLZWlPRm9Ua085WXM5eFJhOEs3NmszbkpyOFYva3pTcm1PSEJRTnczVVFFSmx6UGl2YllFCmV2WW85U0FhTWxjaXVYOUx6ZGx0MFBJWC9XaXQwU3RhMS9NMU1YdWs5ZEhadDZUV29MeXFlekpva1VOd0YrSm0KWkdqQzlRL3ZSaGxQQW5qUXJjQTdiczF0K3VsdkZIZHBoOHRIZWlyQUpsSERiOXU1b1J3THdtRFNjYzNIV2JvZgpyeWNBaEdRNWY4RFI1dkp6b3JzWjgwM1BWblVrUmlwWWRvWU82RkE5eDQ5TzJOL3l0dTNjSHFVNlI5aE1TVUVPClFqeUJLdGFGQWdNQkFBRUNnZ0VBTFljUmwrK0ZEeE9jelFRck8yWTQ1M1V0VUhxTDFzOVh3aklG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hOeDFtNkg2OG5BSVJrCk9YL0EwZWJ5YzZLN0dmTk56MVoxSkVZcVdIYUdEdWhRUGNlUFR0amY4cmJ0M0I2bE9rZllURWxCRGtJOGdTclcKaFFJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0tCg==',
partnerServiceId: '123',
withMilliseconds: false
}
/**
* Using pre-configured APISecurity service to get the access token for B2B.
*/
const apiSecurity = new APISecurity()
apiSecurity.setConfig(config)
/**
* Retrieves the access token for B2B authentication.
* @returns {Promise<any>} A promise that resolves with the access token.
*/
function b2b(): Promise<any> {
return new Promise(async (resolve, reject) => {
try {
const request = await apiSecurity.withAsymmetricSignature().accessTokenB2B()
return resolve(await request.json())
} catch (error) {
return reject(error)
}
});
}
/**
* B2B2C is not implemented yet, so we need to use the SnapBI service.
*/
const snapBI = new SnapBI(config)
snapBI.registerService({
'access-token.b2b2c': '/v1.0/access-token/b2b2c'
})
/**
* Retrieves the access token for the b2b2c service.
* @returns {Promise<any>} A promise that resolves with the access token.
*/
function b2b2c(): Promise<any> {
return new Promise(async (resolve, reject) => {
try {
const request = await snapBI.withHeaders({
'X-CLIENT-KEY': snapBI.config.clientId
}).http.post({
url: snapBI.config.baseUrl + snapBI.services['access-token.b2b2c']
})
return resolve(await request.json())
} catch (error) {
return reject(error)
}
});
}
b2b().then(console.log).catch(console.error)
b2b2c().then(console.log).catch(console.error)