-
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 SDK methods for Email OTP/TOTP/SMS (#65)
* totp * fix import * rest * Replace token after verification * Bump version
- Loading branch information
Showing
16 changed files
with
383 additions
and
26 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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import {ApiClientOptions, ChallengeResponse, EnrollResponse, VerifyResponse} from "./types/shared"; | ||
|
||
export class EmailApiClient { | ||
tenantId: string; | ||
baseUrl: string; | ||
|
||
constructor({baseUrl, tenantId}: ApiClientOptions) { | ||
this.tenantId = tenantId; | ||
this.baseUrl = baseUrl; | ||
} | ||
|
||
async enroll({token, email}: {token: string; email: string}): Promise<EnrollResponse> { | ||
const body = {email}; | ||
|
||
const response = fetch(`${this.baseUrl}/client/user-authenticators/email-otp`, { | ||
method: "POST", | ||
headers: this.buildHeaders(token), | ||
body: JSON.stringify(body), | ||
}); | ||
|
||
return (await response).json(); | ||
} | ||
|
||
async challenge({token}: {token: string}): Promise<ChallengeResponse> { | ||
const response = fetch(`${this.baseUrl}/client/challenge/email-otp`, { | ||
method: "POST", | ||
headers: this.buildHeaders(token), | ||
}); | ||
|
||
return (await response).json(); | ||
} | ||
|
||
async verify({token, code}: {token: string; code: string}): Promise<VerifyResponse> { | ||
const body = {code}; | ||
|
||
const response = fetch(`${this.baseUrl}/client/verify/email-otp`, { | ||
method: "POST", | ||
headers: this.buildHeaders(token), | ||
body: JSON.stringify(body), | ||
}); | ||
|
||
return (await response).json(); | ||
} | ||
|
||
private buildHeaders(token?: string) { | ||
const authorizationHeader = token ? `Bearer ${token}` : `Basic ${window.btoa(encodeURIComponent(this.tenantId))}`; | ||
|
||
return { | ||
"Content-Type": "application/json", | ||
Authorization: authorizationHeader, | ||
}; | ||
} | ||
} |
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,53 @@ | ||
import {ApiClientOptions, ChallengeResponse, EnrollResponse, VerifyResponse} from "./types/shared"; | ||
|
||
export class SmsApiClient { | ||
tenantId: string; | ||
baseUrl: string; | ||
|
||
constructor({baseUrl, tenantId}: ApiClientOptions) { | ||
this.tenantId = tenantId; | ||
this.baseUrl = baseUrl; | ||
} | ||
|
||
async enroll({token, phoneNumber}: {token: string; phoneNumber: string}): Promise<EnrollResponse> { | ||
const body = {phoneNumber}; | ||
|
||
const response = fetch(`${this.baseUrl}/client/user-authenticators/sms`, { | ||
method: "POST", | ||
headers: this.buildHeaders(token), | ||
body: JSON.stringify(body), | ||
}); | ||
|
||
return (await response).json(); | ||
} | ||
|
||
async challenge({token}: {token: string}): Promise<ChallengeResponse> { | ||
const response = fetch(`${this.baseUrl}/client/challenge/sms`, { | ||
method: "POST", | ||
headers: this.buildHeaders(token), | ||
}); | ||
|
||
return (await response).json(); | ||
} | ||
|
||
async verify({token, code}: {token: string; code: string}): Promise<VerifyResponse> { | ||
const body = {code}; | ||
|
||
const response = fetch(`${this.baseUrl}/client/verify/sms`, { | ||
method: "POST", | ||
headers: this.buildHeaders(token), | ||
body: JSON.stringify(body), | ||
}); | ||
|
||
return (await response).json(); | ||
} | ||
|
||
private buildHeaders(token?: string) { | ||
const authorizationHeader = token ? `Bearer ${token}` : `Basic ${window.btoa(encodeURIComponent(this.tenantId))}`; | ||
|
||
return { | ||
"Content-Type": "application/json", | ||
Authorization: authorizationHeader, | ||
}; | ||
} | ||
} |
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,42 @@ | ||
import {ApiClientOptions, VerifyResponse} from "./types/shared"; | ||
import {EnrollResponse} from "./types/totp"; | ||
|
||
export class TotpApiClient { | ||
tenantId: string; | ||
baseUrl: string; | ||
|
||
constructor({baseUrl, tenantId}: ApiClientOptions) { | ||
this.tenantId = tenantId; | ||
this.baseUrl = baseUrl; | ||
} | ||
|
||
async enroll({token}: {token: string}): Promise<EnrollResponse> { | ||
const response = fetch(`${this.baseUrl}/client/user-authenticators/totp`, { | ||
method: "POST", | ||
headers: this.buildHeaders(token), | ||
}); | ||
|
||
return (await response).json(); | ||
} | ||
|
||
async verify({token, code}: {token: string; code: string}): Promise<VerifyResponse> { | ||
const body = {code}; | ||
|
||
const response = fetch(`${this.baseUrl}/client/verify/totp`, { | ||
method: "POST", | ||
headers: this.buildHeaders(token), | ||
body: JSON.stringify(body), | ||
}); | ||
|
||
return (await response).json(); | ||
} | ||
|
||
private buildHeaders(token?: string) { | ||
const authorizationHeader = token ? `Bearer ${token}` : `Basic ${window.btoa(encodeURIComponent(this.tenantId))}`; | ||
|
||
return { | ||
"Content-Type": "application/json", | ||
Authorization: authorizationHeader, | ||
}; | ||
} | ||
} |
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,25 @@ | ||
export type ApiClientOptions = { | ||
baseUrl: string; | ||
tenantId: string; | ||
}; | ||
|
||
export type EnrollResponse = { | ||
userAuthenticatorId: string; | ||
}; | ||
|
||
export type ChallengeResponse = { | ||
challengeId: string; | ||
}; | ||
|
||
export type VerifyResponse = { | ||
isVerified: boolean; | ||
token?: string; | ||
failureReason?: string; | ||
}; | ||
|
||
export type ErrorResponse = { | ||
error: string; | ||
errorDescription?: string; | ||
}; | ||
|
||
export type AuthsignalResponse<T> = T | ErrorResponse; |
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,5 @@ | ||
export type EnrollResponse = { | ||
userAuthenticatorID: string; | ||
uri: string; | ||
secret: string; | ||
}; |
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,55 @@ | ||
import {EmailApiClient} from "./api/email-api-client"; | ||
import {AuthsignalResponse, ChallengeResponse, EnrollResponse, VerifyResponse} from "./api/types/shared"; | ||
import {TokenCache} from "./token-cache"; | ||
|
||
type EmailOptions = { | ||
baseUrl: string; | ||
tenantId: string; | ||
}; | ||
|
||
type EnrollParams = { | ||
email: string; | ||
}; | ||
|
||
type VerifyParams = { | ||
code: string; | ||
}; | ||
|
||
export class Email { | ||
private api: EmailApiClient; | ||
private cache = TokenCache.shared; | ||
|
||
constructor({baseUrl, tenantId}: EmailOptions) { | ||
this.api = new EmailApiClient({baseUrl, tenantId}); | ||
} | ||
|
||
async enroll({email}: EnrollParams): Promise<AuthsignalResponse<EnrollResponse>> { | ||
if (!this.cache.token) { | ||
return this.cache.handleTokenNotSetError(); | ||
} | ||
|
||
return this.api.enroll({token: this.cache.token, email}); | ||
} | ||
|
||
async challenge(): Promise<AuthsignalResponse<ChallengeResponse>> { | ||
if (!this.cache.token) { | ||
return this.cache.handleTokenNotSetError(); | ||
} | ||
|
||
return this.api.challenge({token: this.cache.token}); | ||
} | ||
|
||
async verify({code}: VerifyParams): Promise<AuthsignalResponse<VerifyResponse>> { | ||
if (!this.cache.token) { | ||
return this.cache.handleTokenNotSetError(); | ||
} | ||
|
||
const verifyResponse = await this.api.verify({token: this.cache.token, code}); | ||
|
||
if (verifyResponse.token) { | ||
this.cache.token = verifyResponse.token; | ||
} | ||
|
||
return verifyResponse; | ||
} | ||
} |
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
Oops, something went wrong.