-
Notifications
You must be signed in to change notification settings - Fork 4
/
get-revoke-account-permission-url.ts
81 lines (70 loc) · 2.85 KB
/
get-revoke-account-permission-url.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
71
72
73
74
75
76
77
78
79
80
81
/*!
* Copyright (c) 2009-2024 World Data Exchange Holdings Pty Limited (WDXH). All rights reserved.
*/
import { getRandomAlphaNumeric } from "./crypto";
import { sign } from "jsonwebtoken";
import { net, handleServerResponse } from "./net";
import { UserAccessToken } from "./types/user-access-token";
import { SDKConfiguration } from "./types/sdk-configuration";
import { ContractDetails } from "./types/common";
import { CodecAssertion, codecAssertion } from "./utils/codec-assertion";
import * as t from "io-ts";
export interface GetRevokeAccountPermissionUrlOptions {
contractDetails: ContractDetails;
userAccessToken: UserAccessToken;
accountId: string;
redirectUri: string;
}
export interface GetRevokeAccountPermissionUrlResponse {
location: string;
}
const GetRevokeAccountPermissionResponseUrlCodec = t.type({
location: t.string,
});
export const assertIsGetRevokeAccountPermissionUrlResponse: CodecAssertion<GetRevokeAccountPermissionUrlResponse> =
codecAssertion(GetRevokeAccountPermissionResponseUrlCodec);
const getRevokeAccountPermissionUrl = async (
options: GetRevokeAccountPermissionUrlOptions,
sdkConfig: SDKConfiguration
): Promise<GetRevokeAccountPermissionUrlResponse> => {
const { userAccessToken, contractDetails, accountId, redirectUri } = options;
const { contractId, privateKey } = contractDetails;
const url = `${sdkConfig.baseUrl}permission-access/revoke/h:accountId`;
try {
const response = await net.get(url, {
headers: {
accept: "application/json",
accountId,
redirectUri,
},
retry: sdkConfig.retryOptions,
responseType: "json",
hooks: {
beforeRequest: [
(options) => {
const jwt: string = sign(
{
access_token: userAccessToken.accessToken.value,
client_id: `${sdkConfig.applicationId}_${contractId}`,
nonce: getRandomAlphaNumeric(32),
timestamp: Date.now(),
},
privateKey.toString(),
{
algorithm: "PS512",
noTimestamp: true,
}
);
options.headers["Authorization"] = `Bearer ${jwt}`;
},
],
},
});
assertIsGetRevokeAccountPermissionUrlResponse(response.body);
return response.body;
} catch (error) {
handleServerResponse(error);
throw error;
}
};
export { getRevokeAccountPermissionUrl };