-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93e678e
commit 2b28649
Showing
8 changed files
with
162 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import retry from 'async-retry'; | ||
import { createUnauthorizedApi } from './api'; | ||
import { DefenderApiResponseError } from './api-error'; | ||
|
||
export type AuthType = 'admin' | 'relay'; | ||
|
||
export type AuthCredentials = { | ||
apiKey: string; | ||
secretKey: string; | ||
type: AuthType; | ||
}; | ||
|
||
export type RefreshCredentials = { | ||
apiKey: string; | ||
secretKey: string; | ||
refreshToken: string; | ||
type: AuthType; | ||
}; | ||
|
||
export type AuthResponse = { | ||
accessToken: string; | ||
refreshToken: string; | ||
}; | ||
|
||
export async function authenticateV2(credentials: AuthCredentials, apiUrl: string): Promise<AuthResponse> { | ||
const api = createUnauthorizedApi(apiUrl); | ||
try { | ||
return await retry(() => api.post('/auth/login', credentials), { retries: 3 }); | ||
} catch (err) { | ||
const errorMessage = (err as DefenderApiResponseError).response.statusText || err; | ||
throw new Error(`Failed to get a token for the API key ${credentials.apiKey}: ${errorMessage}`); | ||
} | ||
} | ||
|
||
export async function refreshSessionV2(credentials: RefreshCredentials, apiUrl: string): Promise<AuthResponse> { | ||
const api = createUnauthorizedApi(apiUrl); | ||
try { | ||
return await retry(() => api.post('/auth/refresh-token', credentials), { retries: 3 }); | ||
} catch (err) { | ||
const errorMessage = (err as DefenderApiResponseError).response.statusText || err; | ||
throw new Error(`Failed to refresh token for the API key ${credentials.apiKey}: ${errorMessage}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters