11import { encodeUrlToBase64 , getRandomString , toUrlParameter , httpCall , normalizeDomain } from './Utils' ;
22import { 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
1313export type AuthenticationConfig = {
1414 domain ?: string ;
@@ -42,7 +42,6 @@ async function computeChallengeCode(codeVerifier: string): Promise<string> {
4242}
4343
4444export 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
104101export 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 ,
0 commit comments