Skip to content

Commit

Permalink
Add updateUser and getChallenge methods (#38)
Browse files Browse the repository at this point in the history
* Add updateUser and getChallenge methods

* wip
  • Loading branch information
chrisfisher authored Jul 26, 2024
1 parent 64127fa commit 38182a2
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 33 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@authsignal/node",
"version": "1.0.6",
"version": "1.1.0",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "dist/index.d.ts",
Expand All @@ -18,7 +18,7 @@
"build": "rollup -c rollup.config.js"
},
"dependencies": {
"axios": "^1.6.8"
"axios": "^1.7.2"
},
"devDependencies": {
"@rollup/plugin-typescript": "^8.3.2",
Expand Down
29 changes: 29 additions & 0 deletions src/authsignal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import axios from "axios";

import {
AuthsignalConstructor,
ChallengeRequest,
ChallengeResponse,
EnrollVerifiedAuthenticatorRequest,
EnrollVerifiedAuthenticatorResponse,
GetActionRequest,
GetActionResponse,
TrackRequest,
TrackResponse,
UpdateUserRequest,
UserRequest,
UserResponse,
ValidateChallengeRequest,
Expand Down Expand Up @@ -39,6 +42,32 @@ export class Authsignal {
return response.data;
}

public async updateUser(request: UpdateUserRequest): Promise<UserResponse> {
const {userId, ...data} = request;

const url = `${this.apiBaseUrl}/users/${userId}`;

const config = this.getBasicAuthConfig();

const response = await axios.post<UserResponse>(url, data, config);

return response.data;
}

public async getChallenge(request: ChallengeRequest): Promise<ChallengeResponse> {
const {userId, action} = request;

const urlParams = action ? `?action=${action}` : undefined;

const url = `${this.apiBaseUrl}/users/${userId}/challenge${urlParams}`;

const config = this.getBasicAuthConfig();

const response = await axios.get<ChallengeResponse>(url, config);

return response.data;
}

public async track(request: TrackRequest): Promise<TrackResponse> {
const {userId, action, redirectUrl = this.redirectUrl, ...rest} = request;

Expand Down
3 changes: 2 additions & 1 deletion src/integrations/auth0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface ExecutePostLoginOptions {
userId?: string;
action?: string;
redirectUrl?: string;
custom?: object;
custom?: {[key: string]: string};
apiBaseUrl?: string;
forceEnrollment?: boolean;
}
Expand Down Expand Up @@ -85,6 +85,7 @@ export async function handleAuth0ContinuePostLogin(event: any, api: any, options

const result = await authsignal.validateChallenge({
token: event.request.query?.["token"],
action,
userId,
});

Expand Down
57 changes: 31 additions & 26 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,28 @@ export interface UserResponse {
email?: string;
phoneNumber?: string;
username?: string;
displayName?: string;
enrolledVerificationMethods?: VerificationMethod[];
allowedVerificationMethods?: VerificationMethod[];
custom?: CustomData;
}

export interface UpdateUserRequest {
userId: string;
email?: string | null;
phoneNumber?: string | null;
username?: string | null;
displayName?: string | null;
custom?: CustomData;
}

export interface ChallengeRequest {
userId: string;
action?: string;
}

export interface ChallengeResponse {
challengeId?: string;
}

export interface TrackRequest {
Expand All @@ -30,7 +50,7 @@ export interface TrackRequest {
email?: string;
phoneNumber?: string;
username?: string;
custom?: object;
custom?: CustomData;
challengeId?: string;
}

Expand All @@ -57,7 +77,7 @@ export interface GetActionResponse {

export interface EnrollVerifiedAuthenticatorRequest {
userId: string;
oobChannel: string;
verificationMethod: VerificationMethod;
phoneNumber?: string;
email?: string;
isDefault?: boolean;
Expand All @@ -70,6 +90,7 @@ export interface EnrollVerifiedAuthenticatorResponse {

export interface ValidateChallengeRequest {
token: string;
action?: string;
userId?: string;
}

Expand All @@ -81,6 +102,7 @@ export interface ValidateChallengeResponse {
action?: string;
idempotencyKey?: string;
verificationMethod?: VerificationMethod;
error?: string;
}

export enum UserActionState {
Expand All @@ -94,41 +116,24 @@ export enum UserActionState {
export interface UserAuthenticator {
userId: string;
userAuthenticatorId: string;
authenticatorType: AuthenticatorType;
verificationMethod: VerificationMethod;
createdAt: string;
isDefault: boolean;
verifiedAt?: string;
isActive?: boolean;
oobChannel?: OobChannel;
phoneNumber?: string;
email?: string;
}

export enum AuthenticatorType {
OOB = "OOB",
OTP = "OTP",
}

export interface OtpBinding {
secret: string;
uri: string;
}

export enum OobChannel {
SMS = "SMS",
EMAIL_MAGIC_LINK = "EMAIL_MAGIC_LINK",
EMAIL_OTP = "EMAIL_OTP",
}

export enum VerificationMethod {
SMS = "SMS",
AUTHENTICATOR_APP = "AUTHENTICATOR_APP",
RECOVERY_CODE = "RECOVERY_CODE",
EMAIL_OTP = "EMAIL_OTP",
EMAIL_MAGIC_LINK = "EMAIL_MAGIC_LINK",
AUTHENTICATOR_APP = "AUTHENTICATOR_APP",
PASSKEY = "PASSKEY",
PUSH = "PUSH",
SECURITY_KEY = "SECURITY_KEY",
PUSH = "PUSH",
VERIFF = "VERIFF",
IPROOV = "IPROOV",
EMAIL_OTP = "EMAIL_OTP",
RECOVERY_CODE = "RECOVERY_CODE",
}

type CustomData = {[key: string]: string};
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ asynckit@^0.4.0:
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==

axios@^1.6.8:
version "1.6.8"
resolved "https://registry.npmjs.org/axios/-/axios-1.6.8.tgz#66d294951f5d988a00e87a0ffb955316a619ea66"
integrity sha512-v/ZHtJDU39mDpyBoFVkETcd/uNdxrWRrg3bKpOKzXFA6Bvqopts6ALSMU3y6ijYxbw2B+wPrIv46egTzJXCLGQ==
axios@^1.7.2:
version "1.7.2"
resolved "https://registry.npmjs.org/axios/-/axios-1.7.2.tgz#b625db8a7051fbea61c35a3cbb3a1daa7b9c7621"
integrity sha512-2A8QhOMrbomlDuiLeK9XibIBzuHeRcqqNOHp0Cyp5EoJ1IFDh+XZH3A6BkXtv0K4gFGCI0Y4BM7B1wOEi0Rmgw==
dependencies:
follow-redirects "^1.15.6"
form-data "^4.0.0"
Expand Down

0 comments on commit 38182a2

Please sign in to comment.