Skip to content

Commit 413ce62

Browse files
committed
Improved error handling and error messages
1 parent 1a51838 commit 413ce62

File tree

1 file changed

+129
-92
lines changed

1 file changed

+129
-92
lines changed

src/Oauth.ts

Lines changed: 129 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -49,66 +49,86 @@ export async function computeAuthorizationUrl(config: AuthenticationConfig): Pro
4949
throw new Error('No domain provided!');
5050
}
5151

52-
const codeVerifier: string = getRandomString(CODE_VERIFIER_LENGTH);
53-
const codeChallenge: string = await computeChallengeCode(codeVerifier);
54-
const sessionId: string = await initializeOauthSession(config.domain);
55-
56-
return {
57-
authorizationUrl: `https://${normalizeDomain(config.domain)}/api/oauth/authorize?${toUrlParameter(
58-
{
59-
response_type: AUTH_URL_RESPONSE_TYPE,
60-
client_id: config.clientId,
61-
scope: config.scopes.join('+'),
62-
code_challenge: codeChallenge,
63-
code_challenge_method: AUTH_URL_CODE_CHALLENGE_METHOD,
64-
redirect_uri: AUTH_DEFAULT_REDIRECT_URL,
65-
session_id: sessionId,
66-
},
67-
)}`,
68-
codeVerifier,
69-
sessionId,
70-
};
52+
try {
53+
const codeVerifier: string = getRandomString(CODE_VERIFIER_LENGTH);
54+
const codeChallenge: string = await computeChallengeCode(codeVerifier);
55+
const sessionId: string = await initializeOauthSession(config.domain);
56+
57+
return {
58+
authorizationUrl: `https://${normalizeDomain(config.domain)}/api/oauth/authorize?${toUrlParameter(
59+
{
60+
response_type: AUTH_URL_RESPONSE_TYPE,
61+
client_id: config.clientId,
62+
scope: config.scopes.join('+'),
63+
code_challenge: codeChallenge,
64+
code_challenge_method: AUTH_URL_CODE_CHALLENGE_METHOD,
65+
redirect_uri: AUTH_DEFAULT_REDIRECT_URL,
66+
session_id: sessionId,
67+
},
68+
)}`,
69+
codeVerifier,
70+
sessionId,
71+
};
72+
} catch (error) {
73+
logMessage('error', {
74+
code: 'ERR_COMPUTE_AUTH_URL',
75+
message: 'Error computing authorization url.',
76+
});
77+
throw new Error(error);
78+
}
7179
}
7280

7381
export async function initializeOauthSession(domain: string): Promise<string> {
7482
try {
7583
const session = await httpCall<{ data: { key: string } }>(
7684
`https://${normalizeDomain(domain)}/api/oauth/create/session`,
7785
{ method: 'POST' },
78-
);
86+
).catch((error) => {
87+
logMessage('error', {
88+
code: 'ERR_SESSION',
89+
message: 'Error generating session.',
90+
});
91+
throw new Error(error);
92+
});
7993

8094
return session.data.key;
81-
8295
} catch (error) {
8396
logMessage('error', {
8497
code: 'ERR_SESSION',
8598
message: 'Error generating session.',
8699
});
87-
throw new Error('Error generating session.');
100+
throw new Error(error);
88101
}
89102
}
90103

91104
export async function pollOauthSession(config: AuthenticationConfig, sessionId: string): Promise<string> {
105+
92106
if (!config.domain) {
93107
throw new Error('No domain provided!');
94108
}
95109

96-
const response = await httpCall<{ data: { payload: { code: string } } }>(
97-
`https://${normalizeDomain(config.domain)}/api/oauth/poll`,
98-
{
99-
method: 'POST',
100-
headers: {
101-
'content-type': 'application/json',
110+
try {
111+
const response = await httpCall<{ data: { payload: { code: string } } }>(
112+
`https://${normalizeDomain(config.domain)}/api/oauth/poll`,
113+
{
114+
method: 'POST',
115+
headers: {
116+
'content-type': 'application/json',
117+
},
118+
body: JSON.stringify({
119+
session_id: sessionId,
120+
}),
102121
},
103-
body: JSON.stringify({
104-
session_id: sessionId,
105-
}),
106-
},
107-
);
108-
109-
// @TODO handle response.data, response.data.payload and response.data.payload.code
122+
);
110123

111-
return response.data.payload.code;
124+
return response.data.payload.code;
125+
} catch (error) {
126+
logMessage('error', {
127+
code: 'ERR_POLL_SESSION',
128+
message: 'Error polling session.',
129+
});
130+
throw new Error(error);
131+
}
112132
}
113133

114134
export async function retrieveAccessToken(
@@ -121,35 +141,43 @@ export async function retrieveAccessToken(
121141
throw new Error('No domain provided!');
122142
}
123143

124-
const normalizedDomain = normalizeDomain(config.domain);
125-
const response = await httpCall<{ access_token: string; expires_in: number; refresh_token: string }>(
126-
`https://${normalizedDomain}/api/oauth/accesstoken`,
127-
{
128-
method: 'POST',
129-
headers: {
130-
'content-type': 'application/json',
144+
try {
145+
const normalizedDomain = normalizeDomain(config.domain);
146+
const response = await httpCall<{ access_token: string; expires_in: number; refresh_token: string }>(
147+
`https://${normalizedDomain}/api/oauth/accesstoken`,
148+
{
149+
method: 'POST',
150+
headers: {
151+
'content-type': 'application/json',
152+
},
153+
body: JSON.stringify({
154+
grant_type: AUTH_CODE_GRANT_TYPE,
155+
code,
156+
code_verifier: codeVerifier,
157+
client_id: config.clientId,
158+
redirect_uri: AUTH_DEFAULT_REDIRECT_URL,
159+
}),
160+
},
161+
);
162+
163+
return {
164+
bearerToken: {
165+
tokenType: BEARER_TOKEN_TYPE,
166+
expiresIn: response.expires_in,
167+
accessToken: response.access_token,
168+
refreshToken: response.refresh_token,
169+
domain: normalizedDomain,
131170
},
132-
body: JSON.stringify({
133-
grant_type: AUTH_CODE_GRANT_TYPE,
134-
code,
135-
code_verifier: codeVerifier,
136-
client_id: config.clientId,
137-
redirect_uri: AUTH_DEFAULT_REDIRECT_URL,
138-
}),
139-
},
140-
);
141-
142-
return {
143-
bearerToken: {
144-
tokenType: BEARER_TOKEN_TYPE,
145-
expiresIn: response.expires_in,
146-
accessToken: response.access_token,
147-
refreshToken: response.refresh_token,
148-
domain: normalizedDomain,
149-
},
150-
clientId: config.clientId,
151-
scopes: config.scopes,
152-
};
171+
clientId: config.clientId,
172+
scopes: config.scopes,
173+
};
174+
} catch (error) {
175+
logMessage('error', {
176+
code: 'ERR_ACCESS_TOKEN',
177+
message: 'Error retrieving token!',
178+
});
179+
throw new Error(error);
180+
}
153181
}
154182

155183
export async function refreshToken(
@@ -158,34 +186,43 @@ export async function refreshToken(
158186
clientId: string,
159187
scopes: string[],
160188
): Promise<Token> {
161-
const normalizedDomain = normalizeDomain(domain);
162-
const response = await httpCall<{ access_token: string; expires_in: number; refresh_token: string }>(
163-
`https://${normalizedDomain}/api/oauth/refresh`,
164-
{
165-
method: 'POST',
166-
headers: {
167-
'content-type': 'application/json',
189+
190+
try {
191+
const normalizedDomain = normalizeDomain(domain);
192+
const response = await httpCall<{ access_token: string; expires_in: number; refresh_token: string }>(
193+
`https://${normalizedDomain}/api/oauth/refresh`,
194+
{
195+
method: 'POST',
196+
headers: {
197+
'content-type': 'application/json',
198+
},
199+
body: JSON.stringify({
200+
grant_type: REFRESH_TOKEN_GRANT_TYPE,
201+
refresh_token: refreshToken,
202+
client_id: clientId,
203+
scope: scopes.join('+'),
204+
}),
168205
},
169-
body: JSON.stringify({
170-
grant_type: REFRESH_TOKEN_GRANT_TYPE,
171-
refresh_token: refreshToken,
172-
client_id: clientId,
173-
scope: scopes.join('+'),
174-
}),
175-
},
176-
);
177-
178-
return {
179-
bearerToken: {
180-
tokenType: BEARER_TOKEN_TYPE,
181-
expiresIn: response.expires_in,
182-
accessToken: response.access_token,
183-
refreshToken: response.refresh_token,
184-
domain: normalizedDomain,
185-
},
186-
clientId,
187-
scopes,
188-
};
206+
);
207+
208+
return {
209+
bearerToken: {
210+
tokenType: BEARER_TOKEN_TYPE,
211+
expiresIn: response.expires_in,
212+
accessToken: response.access_token,
213+
refreshToken: response.refresh_token,
214+
domain: normalizedDomain,
215+
},
216+
clientId,
217+
scopes,
218+
};
219+
} catch (error) {
220+
logMessage('error', {
221+
code: 'ERR_REFRESH_TOKEN',
222+
message: 'Error refreshing token!',
223+
});
224+
throw new Error(error);
225+
}
189226
}
190227

191228
export async function revokeToken(
@@ -203,7 +240,7 @@ export async function revokeToken(
203240
} catch (error) {
204241
logMessage('error', {
205242
code: 'ERR_TOKEN_REVOKE',
206-
message: 'Access token could not be revoked!',
243+
message: 'Error revoking token!',
207244
});
208245
}
209246
}

0 commit comments

Comments
 (0)