-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for
onTokenExpired
callback (#75)
* wip * Bump version
- Loading branch information
Showing
13 changed files
with
167 additions
and
104 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,59 @@ | ||
import {ApiClientOptions, ChallengeResponse, EnrollResponse, VerifyResponse} from "./types/shared"; | ||
import {buildHeaders, handleTokenExpired} from "./helpers"; | ||
import {ApiClientOptions, AuthsignalResponse, ChallengeResponse, EnrollResponse, VerifyResponse} from "./types/shared"; | ||
|
||
export class EmailApiClient { | ||
tenantId: string; | ||
baseUrl: string; | ||
onTokenExpired?: () => void; | ||
|
||
constructor({baseUrl, tenantId}: ApiClientOptions) { | ||
constructor({baseUrl, tenantId, onTokenExpired}: ApiClientOptions) { | ||
this.tenantId = tenantId; | ||
this.baseUrl = baseUrl; | ||
this.onTokenExpired = onTokenExpired; | ||
} | ||
|
||
async enroll({token, email}: {token: string; email: string}): Promise<EnrollResponse> { | ||
async enroll({token, email}: {token: string; email: string}) { | ||
const body = {email}; | ||
|
||
const response = fetch(`${this.baseUrl}/client/user-authenticators/email-otp`, { | ||
const response = await fetch(`${this.baseUrl}/client/user-authenticators/email-otp`, { | ||
method: "POST", | ||
headers: this.buildHeaders(token), | ||
headers: buildHeaders({token, tenantId: this.tenantId}), | ||
body: JSON.stringify(body), | ||
}); | ||
|
||
return (await response).json(); | ||
const responseJson: AuthsignalResponse<EnrollResponse> = await response.json(); | ||
|
||
handleTokenExpired({response: responseJson, onTokenExpired: this.onTokenExpired}); | ||
|
||
return responseJson; | ||
} | ||
|
||
async challenge({token}: {token: string}): Promise<ChallengeResponse> { | ||
const response = fetch(`${this.baseUrl}/client/challenge/email-otp`, { | ||
async challenge({token}: {token: string}) { | ||
const response = await fetch(`${this.baseUrl}/client/challenge/email-otp`, { | ||
method: "POST", | ||
headers: this.buildHeaders(token), | ||
headers: buildHeaders({token, tenantId: this.tenantId}), | ||
}); | ||
|
||
return (await response).json(); | ||
const responseJson: AuthsignalResponse<ChallengeResponse> = await response.json(); | ||
|
||
handleTokenExpired({response: responseJson, onTokenExpired: this.onTokenExpired}); | ||
|
||
return responseJson; | ||
} | ||
|
||
async verify({token, code}: {token: string; code: string}): Promise<VerifyResponse> { | ||
async verify({token, code}: {token: string; code: string}) { | ||
const body = {verificationCode: code}; | ||
|
||
const response = fetch(`${this.baseUrl}/client/verify/email-otp`, { | ||
const response = await fetch(`${this.baseUrl}/client/verify/email-otp`, { | ||
method: "POST", | ||
headers: this.buildHeaders(token), | ||
headers: buildHeaders({token, tenantId: this.tenantId}), | ||
body: JSON.stringify(body), | ||
}); | ||
|
||
return (await response).json(); | ||
} | ||
const responseJson: AuthsignalResponse<VerifyResponse> = await response.json(); | ||
|
||
private buildHeaders(token?: string) { | ||
const authorizationHeader = token ? `Bearer ${token}` : `Basic ${window.btoa(encodeURIComponent(this.tenantId))}`; | ||
handleTokenExpired({response: responseJson, onTokenExpired: this.onTokenExpired}); | ||
|
||
return { | ||
"Content-Type": "application/json", | ||
Authorization: authorizationHeader, | ||
}; | ||
return responseJson; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,45 +1,59 @@ | ||
import {buildHeaders} from "./helpers"; | ||
import {ApiClientOptions, ChallengeResponse, EnrollResponse, VerifyResponse} from "./types/shared"; | ||
import {buildHeaders, handleTokenExpired} from "./helpers"; | ||
import {ApiClientOptions, AuthsignalResponse, ChallengeResponse, EnrollResponse, VerifyResponse} from "./types/shared"; | ||
|
||
export class SmsApiClient { | ||
tenantId: string; | ||
baseUrl: string; | ||
onTokenExpired?: () => void; | ||
|
||
constructor({baseUrl, tenantId}: ApiClientOptions) { | ||
constructor({baseUrl, tenantId, onTokenExpired}: ApiClientOptions) { | ||
this.tenantId = tenantId; | ||
this.baseUrl = baseUrl; | ||
this.onTokenExpired = onTokenExpired; | ||
} | ||
|
||
async enroll({token, phoneNumber}: {token: string; phoneNumber: string}): Promise<EnrollResponse> { | ||
async enroll({token, phoneNumber}: {token: string; phoneNumber: string}) { | ||
const body = {phoneNumber}; | ||
|
||
const response = fetch(`${this.baseUrl}/client/user-authenticators/sms`, { | ||
const response = await fetch(`${this.baseUrl}/client/user-authenticators/sms`, { | ||
method: "POST", | ||
headers: buildHeaders({token, tenantId: this.tenantId}), | ||
body: JSON.stringify(body), | ||
}); | ||
|
||
return (await response).json(); | ||
const responseJson: AuthsignalResponse<EnrollResponse> = await response.json(); | ||
|
||
handleTokenExpired({response: responseJson, onTokenExpired: this.onTokenExpired}); | ||
|
||
return responseJson; | ||
} | ||
|
||
async challenge({token}: {token: string}): Promise<ChallengeResponse> { | ||
const response = fetch(`${this.baseUrl}/client/challenge/sms`, { | ||
async challenge({token}: {token: string}) { | ||
const response = await fetch(`${this.baseUrl}/client/challenge/sms`, { | ||
method: "POST", | ||
headers: buildHeaders({token, tenantId: this.tenantId}), | ||
}); | ||
|
||
return (await response).json(); | ||
const responseJson: AuthsignalResponse<ChallengeResponse> = await response.json(); | ||
|
||
handleTokenExpired({response: responseJson, onTokenExpired: this.onTokenExpired}); | ||
|
||
return responseJson; | ||
} | ||
|
||
async verify({token, code}: {token: string; code: string}): Promise<VerifyResponse> { | ||
async verify({token, code}: {token: string; code: string}) { | ||
const body = {verificationCode: code}; | ||
|
||
const response = fetch(`${this.baseUrl}/client/verify/sms`, { | ||
const response = await fetch(`${this.baseUrl}/client/verify/sms`, { | ||
method: "POST", | ||
headers: buildHeaders({token, tenantId: this.tenantId}), | ||
body: JSON.stringify(body), | ||
}); | ||
|
||
return (await response).json(); | ||
const responseJson: AuthsignalResponse<VerifyResponse> = await response.json(); | ||
|
||
handleTokenExpired({response: responseJson, onTokenExpired: this.onTokenExpired}); | ||
|
||
return responseJson; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,34 +1,44 @@ | ||
import {buildHeaders} from "./helpers"; | ||
import {ApiClientOptions, VerifyResponse} from "./types/shared"; | ||
import {buildHeaders, handleTokenExpired} from "./helpers"; | ||
import {ApiClientOptions, AuthsignalResponse, VerifyResponse} from "./types/shared"; | ||
import {EnrollResponse} from "./types/totp"; | ||
|
||
export class TotpApiClient { | ||
tenantId: string; | ||
baseUrl: string; | ||
onTokenExpired?: () => void; | ||
|
||
constructor({baseUrl, tenantId}: ApiClientOptions) { | ||
constructor({baseUrl, tenantId, onTokenExpired}: ApiClientOptions) { | ||
this.tenantId = tenantId; | ||
this.baseUrl = baseUrl; | ||
this.onTokenExpired = onTokenExpired; | ||
} | ||
|
||
async enroll({token}: {token: string}): Promise<EnrollResponse> { | ||
const response = fetch(`${this.baseUrl}/client/user-authenticators/totp`, { | ||
async enroll({token}: {token: string}) { | ||
const response = await fetch(`${this.baseUrl}/client/user-authenticators/totp`, { | ||
method: "POST", | ||
headers: buildHeaders({token, tenantId: this.tenantId}), | ||
}); | ||
|
||
return (await response).json(); | ||
const responseJson: AuthsignalResponse<EnrollResponse> = await response.json(); | ||
|
||
handleTokenExpired({response: responseJson, onTokenExpired: this.onTokenExpired}); | ||
|
||
return responseJson; | ||
} | ||
|
||
async verify({token, code}: {token: string; code: string}): Promise<VerifyResponse> { | ||
async verify({token, code}: {token: string; code: string}) { | ||
const body = {verificationCode: code}; | ||
|
||
const response = fetch(`${this.baseUrl}/client/verify/totp`, { | ||
const response = await fetch(`${this.baseUrl}/client/verify/totp`, { | ||
method: "POST", | ||
headers: buildHeaders({token, tenantId: this.tenantId}), | ||
body: JSON.stringify(body), | ||
}); | ||
|
||
return (await response).json(); | ||
const responseJson: AuthsignalResponse<VerifyResponse> = await response.json(); | ||
|
||
handleTokenExpired({response: responseJson, onTokenExpired: this.onTokenExpired}); | ||
|
||
return responseJson; | ||
} | ||
} |
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
Oops, something went wrong.