forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsandbox.ts
More file actions
144 lines (127 loc) · 4.85 KB
/
sandbox.ts
File metadata and controls
144 lines (127 loc) · 4.85 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import { workspace } from 'vscode';
// eslint-disable-next-line no-shadow
export enum SBAPIEndpoint {
SIGNUP = '/api/v1/signup',
VERIFICATION = '/api/v1/signup/verification'
}
export interface SBStatus {
ready: boolean;
reason: 'Provisioned' | 'PendingApproval';
verificationRequired: boolean;
}
export interface SBSignupResponse {
apiEndpoint: string;
cheDashboardURL: string;
clusterName: string;
company: string;
compliantUsername: string;
consoleURL: string;
familyName: string;
givenName: string;
status: SBStatus;
username: string;
proxyURL: string;
}
export interface SBResponseData {
status: string;
code: number;
message: string;
details: string;
}
export interface VerificationCodeResponse{
ok: boolean;
json: SBResponseData;
}
export const OAUTH_SERVER_INFO_PATH = '.well-known/oauth-authorization-server';
export interface OauthServerInfo {
issuer: string;
authorization_endpoint: string;
token_endpoint: string;
scopes_supported: string[];
response_types_supported: string[];
grant_types_supported: string[];
code_challenge_methods_supported: string[];
}
export function getSandboxAPIUrl(): string {
return workspace.getConfiguration('openshiftToolkit').get('sandboxApiHostUrl');
}
export function getSandboxAPITimeout(): number {
return workspace.getConfiguration('openshiftToolkit').get('sandboxApiTimeout');
}
export interface SandboxAPI {
getSignUpStatus(token: string): Promise<SBSignupResponse | undefined>;
signUp(token: string): Promise<boolean>;
requestVerificationCode(token: string, areaCode: string, phoneNumber: string): Promise<VerificationCodeResponse>;
validateVerificationCode(token: string, code: string): Promise<boolean>;
getOauthServerInfo(apiEndpointUrl: string): Promise<OauthServerInfo>;
}
export async function getSignUpStatus(token: string): Promise<SBSignupResponse | undefined> {
const signupResponse = await fetch(`${getSandboxAPIUrl()}${SBAPIEndpoint.SIGNUP}`, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`
},
cache: 'no-cache',
signal: AbortSignal.timeout(getSandboxAPITimeout())
});
return signupResponse.ok ? signupResponse.json() as Promise<SBSignupResponse> : undefined;
}
export async function signUp(token: string): Promise<boolean> {
const signupResponse = await fetch(`${getSandboxAPIUrl()}${SBAPIEndpoint.SIGNUP}`, {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`
},
signal: AbortSignal.timeout(getSandboxAPITimeout())
});
return signupResponse.ok;
}
export async function requestVerificationCode(token: string, countryCode: string, phoneNumber: string) : Promise<VerificationCodeResponse> {
const verificationCodeRequestResponse = await fetch(`${getSandboxAPIUrl()}${SBAPIEndpoint.VERIFICATION}`, {
method: 'PUT',
headers: {
Authorization: `Bearer ${token}`
},
signal: AbortSignal.timeout(getSandboxAPITimeout()),
body: JSON.stringify({
'country_code': countryCode,
'phone_number': phoneNumber
})
});
const responseText = await verificationCodeRequestResponse.text();
return {
ok: verificationCodeRequestResponse.ok,
json: (responseText ? JSON.parse(responseText) : {}) as SBResponseData
}
}
export async function validateVerificationCode(token: string, code: string): Promise<boolean> {
const validationRequestResponse = await fetch(`${getSandboxAPIUrl()}${SBAPIEndpoint.VERIFICATION}/${code}`, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`
},
signal: AbortSignal.timeout(getSandboxAPITimeout())
});
return validationRequestResponse.ok;
}
export async function getOauthServerInfo(apiEndpointUrl: string): Promise<OauthServerInfo> {
const oauthServerInfoResponse = await fetch(`${apiEndpointUrl}/${OAUTH_SERVER_INFO_PATH}`, {
method: 'GET',
signal: AbortSignal.timeout(getSandboxAPITimeout())
});
const oauthInfoText = await oauthServerInfoResponse.text();
return (oauthInfoText ? JSON.parse(oauthInfoText) : {}) as OauthServerInfo;
}
export function createSandboxAPI(): SandboxAPI {
return {
getSignUpStatus,
signUp,
requestVerificationCode,
validateVerificationCode,
getOauthServerInfo
};
}