Skip to content

Commit 48984e9

Browse files
committed
Rename refreshToken method; Linting fixes; Formatting
1 parent c9e27bf commit 48984e9

File tree

5 files changed

+115
-113
lines changed

5 files changed

+115
-113
lines changed

src/Oauth.ts

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { encodeUrlToBase64, getRandomString, toUrlParameter, httpCall, normalizeDomain } from './Utils';
22
import { logMessage } from './Logger';
33

4-
const CODE_VERIFIER_LENGTH: number = 64;
5-
const AUTH_URL_RESPONSE_TYPE: string = 'code';
6-
const AUTH_URL_CODE_CHALLENGE_METHOD: string = 'S256';
7-
const AUTH_DEFAULT_REDIRECT_URL: string = '/connection/authenticator';
8-
const AUTH_CODE_GRANT_TYPE: string = 'authorization_code';
9-
const REFRESH_TOKEN_GRANT_TYPE: string = 'refresh_token';
10-
const HASH_ALGORITHM: string = 'SHA-256';
11-
const BEARER_TOKEN_TYPE: string = 'Bearer';
4+
const CODE_VERIFIER_LENGTH = 64;
5+
const AUTH_URL_RESPONSE_TYPE = 'code';
6+
const AUTH_URL_CODE_CHALLENGE_METHOD = 'S256';
7+
const AUTH_DEFAULT_REDIRECT_URL = '/connection/authenticator';
8+
const AUTH_CODE_GRANT_TYPE = 'authorization_code';
9+
const REFRESH_TOKEN_GRANT_TYPE = 'refresh_token';
10+
const HASH_ALGORITHM = 'SHA-256';
11+
const BEARER_TOKEN_TYPE = 'Bearer';
1212

1313
export type AuthenticationConfig = {
1414
domain?: string;
@@ -42,7 +42,6 @@ async function computeChallengeCode(codeVerifier: string): Promise<string> {
4242
}
4343

4444
export async function computeAuthorizationUrl(config: AuthenticationConfig): Promise<AuthorizationUrl> {
45-
4645
if (!config.domain) {
4746
throw new Error('No domain provided!');
4847
}
@@ -53,22 +52,20 @@ export async function computeAuthorizationUrl(config: AuthenticationConfig): Pro
5352
const sessionId: string = await initializeOauthSession(config.domain);
5453

5554
return {
56-
authorizationUrl: `https://${normalizeDomain(config.domain)}/api/oauth/authorize?${toUrlParameter(
57-
{
58-
response_type: AUTH_URL_RESPONSE_TYPE,
59-
client_id: config.clientId,
60-
scope: config.scopes.join('+'),
61-
code_challenge: codeChallenge,
62-
code_challenge_method: AUTH_URL_CODE_CHALLENGE_METHOD,
63-
redirect_uri: AUTH_DEFAULT_REDIRECT_URL,
64-
session_id: sessionId,
65-
},
66-
)}`,
55+
authorizationUrl: `https://${normalizeDomain(config.domain)}/api/oauth/authorize?${toUrlParameter({
56+
response_type: AUTH_URL_RESPONSE_TYPE,
57+
client_id: config.clientId,
58+
scope: config.scopes.join('+'),
59+
code_challenge: codeChallenge,
60+
code_challenge_method: AUTH_URL_CODE_CHALLENGE_METHOD,
61+
redirect_uri: AUTH_DEFAULT_REDIRECT_URL,
62+
session_id: sessionId,
63+
})}`,
6764
codeVerifier,
6865
sessionId,
6966
};
7067
} catch (error) {
71-
const errorMessage: string = 'Error computing authorization url.';
68+
const errorMessage = 'Error computing authorization url.';
7269
logMessage('error', {
7370
code: 'ERR_COMPUTE_AUTH_URL',
7471
message: errorMessage,
@@ -92,7 +89,7 @@ export async function initializeOauthSession(domain: string): Promise<string> {
9289

9390
return session.data.key;
9491
} catch (error) {
95-
const errorMessage: string = 'Error generating session.';
92+
const errorMessage = 'Error generating session.';
9693
logMessage('error', {
9794
code: 'ERR_SESSION',
9895
message: errorMessage,
@@ -102,7 +99,6 @@ export async function initializeOauthSession(domain: string): Promise<string> {
10299
}
103100

104101
export async function pollOauthSession(config: AuthenticationConfig, sessionId: string): Promise<string> {
105-
106102
if (!config.domain) {
107103
throw new Error('No domain provided!');
108104
}
@@ -123,7 +119,7 @@ export async function pollOauthSession(config: AuthenticationConfig, sessionId:
123119

124120
return response.data.payload.code;
125121
} catch (error) {
126-
const errorMessage: string = 'Error polling session.';
122+
const errorMessage = 'Error polling session.';
127123
logMessage('error', {
128124
code: 'ERR_POLL_SESSION',
129125
message: 'Error polling session.',
@@ -137,7 +133,6 @@ export async function retrieveAccessToken(
137133
code: string,
138134
codeVerifier: string,
139135
): Promise<Token> {
140-
141136
if (!config.domain) {
142137
throw new Error('No domain provided!');
143138
}
@@ -173,7 +168,7 @@ export async function retrieveAccessToken(
173168
scopes: config.scopes,
174169
};
175170
} catch (error) {
176-
const errorMessage: string = 'Error retrieving token.';
171+
const errorMessage = 'Error retrieving token.';
177172
logMessage('error', {
178173
code: 'ERR_ACCESS_TOKEN',
179174
message: 'errorMessage',
@@ -182,13 +177,12 @@ export async function retrieveAccessToken(
182177
}
183178
}
184179

185-
export async function refreshToken(
180+
export async function getRefreshToken(
186181
domain: string,
187182
refreshToken: string,
188183
clientId: string,
189184
scopes: string[],
190185
): Promise<Token> {
191-
192186
try {
193187
const normalizedDomain = normalizeDomain(domain);
194188
const response = await httpCall<{ access_token: string; expires_in: number; refresh_token: string }>(
@@ -219,7 +213,7 @@ export async function refreshToken(
219213
scopes,
220214
};
221215
} catch (error) {
222-
const errorMessage: string = 'Error refreshing token.';
216+
const errorMessage = 'Error refreshing token.';
223217
logMessage('error', {
224218
code: 'ERR_REFRESH_TOKEN',
225219
message: errorMessage,
@@ -228,10 +222,7 @@ export async function refreshToken(
228222
}
229223
}
230224

231-
export async function revokeToken(
232-
domain: string,
233-
accessToken: string,
234-
): Promise<void> {
225+
export async function revokeToken(domain: string, accessToken: string): Promise<void> {
235226
try {
236227
await httpCall(`https://${normalizeDomain(domain)}/api/oauth/revoke`, {
237228
method: 'POST',
@@ -241,7 +232,7 @@ export async function revokeToken(
241232
body: JSON.stringify({ token: accessToken }),
242233
});
243234
} catch (error) {
244-
const errorMessage: string = 'Error revoking token.';
235+
const errorMessage = 'Error revoking token.';
245236
logMessage('error', {
246237
code: 'ERR_TOKEN_REVOKE',
247238
message: errorMessage,

src/Popup.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ export class Popup {
3131
public readonly popUp: Window | null;
3232
private readonly interval: NodeJS.Timer;
3333
private readonly unregisterEventListener: () => void;
34-
private static EVENT_NAME_CANCELLED: string = 'frontify-oauth-authorize-cancelled';
35-
private static EVENT_NAME_SUCCESS: string = 'frontify-oauth-authorize-success';
36-
private static EVENT_METHOD_CANCELLED: string = 'cancelled';
37-
private static EVENT_METHOD_SUCCESS: string = 'success';
38-
private static EVENT_METHOD_DOMAIN: string = 'domain';
39-
private static EVENT_METHOD_ABORTED: string = 'aborted';
34+
private static EVENT_NAME_CANCELLED = 'frontify-oauth-authorize-cancelled';
35+
private static EVENT_NAME_SUCCESS = 'frontify-oauth-authorize-success';
36+
private static EVENT_METHOD_CANCELLED = 'cancelled';
37+
private static EVENT_METHOD_SUCCESS = 'success';
38+
private static EVENT_METHOD_DOMAIN = 'domain';
39+
private static EVENT_METHOD_ABORTED = 'aborted';
4040
public listeners: { [name: string]: () => void } = {};
41-
private domain: string = "";
41+
private domain = '';
4242

4343
public constructor(userConfiguration: PopupConfiguration) {
4444
const configuration = { ...DEFAULT_POPUP_CONFIG, ...userConfiguration };

src/Utils.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,24 @@ export function toUrlParameter(dict: { [name: string]: string }): string {
2020
}
2121

2222
export function normalizeDomain(domain: string): string {
23-
let normalizedDomain = domain.replace(/^(http(?:s)?:\/\/)/, '');
23+
const normalizedDomain = domain.replace(/^(http(?:s)?:\/\/)/, '');
2424
return normalizedDomain.endsWith('/') ? normalizedDomain.replace(/\/+$/, '') : normalizedDomain;
2525
}
2626

2727
export async function httpCall<JsonResponse>(url: string, init?: RequestInit): Promise<JsonResponse> {
28-
return fetch(url, init).then(async (response) => {
29-
if (response.status >= 200 && response.status <= 299) {
30-
return (await response.json()) as JsonResponse;
31-
}
32-
throw new Error(response.statusText);
33-
}).then((response) => {
34-
return response;
35-
}).catch((error) => {
36-
throw new Error(error);
37-
});
28+
return fetch(url, init)
29+
.then(async (response) => {
30+
if (response.status >= 200 && response.status <= 299) {
31+
return (await response.json()) as JsonResponse;
32+
}
33+
throw new Error(response.statusText);
34+
})
35+
.then((response) => {
36+
return response;
37+
})
38+
.catch((error) => {
39+
throw new Error(error);
40+
});
3841
}
3942

4043
export function addWindowEventListener(eventType: string, callback: any): () => void {

src/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@
6262
diam.
6363
</p>
6464
</div>
65+
<script type="module" src="../dist/index.js"></script>
66+
<script>
67+
</script>
6568
<script type="module">
6669
import {authorize, revoke, refresh} from "../dist/index.es.js";
6770
const POPUP_CONFIG = {

0 commit comments

Comments
 (0)