Skip to content

Commit

Permalink
Add push challenge response fields (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisfisher authored Aug 21, 2024
1 parent 3c2ef80 commit 7d1cdfa
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 26 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ dependencies {

implementation "androidx.browser:browser:1.2.0"

implementation("com.authsignal:authsignal-android:1.0.4")
implementation("com.authsignal:authsignal-android:1.0.5")
}

if (isNewArchitectureEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import androidx.annotation.NonNull;

import com.authsignal.push.AuthsignalPush;
import com.authsignal.push.models.PushChallenge;
import com.authsignal.push.models.PushCredential;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
Expand Down Expand Up @@ -56,11 +57,11 @@ public void getCredential(Promise promise) {
.getCredentialAsync()
.thenAcceptAsync((response) -> {
if (response.getError() != null) {
promise.reject("getCredential error", response.getError());
promise.reject("getCredentialError", response.getError());
} else {
PushCredential credential = response.getData();
WritableMap map = Arguments.createMap();
map.putString("credentialID", credential.getCredentialID());
map.putString("credentialId", credential.getCredentialId());
map.putString("createdAt", credential.getCreatedAt());
map.putString("lastAuthenticatedAt", credential.getLastAuthenticatedAt());
promise.resolve(map);
Expand All @@ -82,7 +83,7 @@ public void addCredential(String token, Promise promise) {
if (response.getErrorType() != null && response.getErrorType().equals("TYPE_TOKEN_NOT_SET")) {
promise.reject("tokenNotSetError", "TOKEN_NOT_SET");
} else if (response.getError() != null) {
promise.reject("addCredential error", response.getError());
promise.reject("addCredentialError", response.getError());
} else {
promise.resolve(response.getData());
}
Expand All @@ -101,7 +102,7 @@ public void removeCredential(Promise promise) {
.removeCredentialAsync()
.thenAcceptAsync(response -> {
if (response.getError() != null) {
promise.reject("removeCredential error", response.getError());
promise.reject("removeCredentialError", response.getError());
} else {
promise.resolve(response.getData());
}
Expand All @@ -120,9 +121,22 @@ public void getChallenge(Promise promise) {
.getChallengeAsync()
.thenAcceptAsync(response -> {
if (response.getError() != null) {
promise.reject("getChallenge error", response.getError());
promise.reject("getChallengeError", response.getError());
} else {
promise.resolve(response.getData());
PushChallenge challenge = response.getData();

if (challenge == null) {
promise.resolve(null);
} else {
WritableMap map = Arguments.createMap();
map.putString("challengeId", challenge.getChallengeId());
map.putString("actionCode", challenge.getActionCode());
map.putString("idempotencyKey", challenge.getIdempotencyKey());
map.putString("ipAddress", challenge.getIpAddress());
map.putString("deviceId", challenge.getDeviceId());
map.putString("userAgent", challenge.getUserAgent());
promise.resolve(map);
}
}
});
} else {
Expand All @@ -144,7 +158,7 @@ public void updateChallenge(
.updateChallengeAsync(challengeId, approved, verificationCode)
.thenAcceptAsync(response -> {
if (response.getError() != null) {
promise.reject("updateChallenge error", response.getError());
promise.reject("updateChallengeError", response.getError());
} else {
promise.resolve(response.getData());
}
Expand Down
27 changes: 19 additions & 8 deletions ios/AuthsignalPushModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ class AuthsignalPushModule: NSObject {
reject("getCredential error", error, nil)
} else if let data = response.data {
let credential: [String: String?] = [
"credentialId": response.data!.credentialId,
"createdAt": response.data!.createdAt,
"lastAuthenticatedAt": response.data!.lastAuthenticatedAt,
"credentialId": data.credentialId,
"createdAt": data.createdAt,
"lastAuthenticatedAt": data.lastAuthenticatedAt,
]

resolve(credential)
Expand Down Expand Up @@ -67,7 +67,7 @@ class AuthsignalPushModule: NSObject {
if (response.errorCode == "TOKEN_NOT_SET") {
reject("tokenNotSetError", "TOKEN_NOT_SET", nil)
} else if let error = response.error {
reject("addCredential error", error, nil)
reject("addCredentialError", error, nil)
} else {
resolve(response.data)
}
Expand All @@ -87,7 +87,7 @@ class AuthsignalPushModule: NSObject {
let response = await authsignal.removeCredential()

if let error = response.error {
reject("removeCredential error", error, nil)
reject("removeCredentialError", error, nil)
} else {
resolve(response.data)
}
Expand All @@ -107,9 +107,20 @@ class AuthsignalPushModule: NSObject {
let response = await authsignal.getChallenge()

if let error = response.error {
reject("getChallenge error", error, nil)
reject("getChallengeError", error, nil)
} else if let data = response.data as? PushChallenge {
let challenge: [String: String?] = [
"challengeId": data.challengeId,
"actionCode": data.actionCode,
"idempotencyKey": data.idempotencyKey,
"userAgent": data.userAgent,
"deviceId": data.deviceId,
"ipAddress": data.ipAddress,
]

resolve(challenge)
} else {
resolve(response.data)
resolve(nil)
}
}
}
Expand Down Expand Up @@ -138,7 +149,7 @@ class AuthsignalPushModule: NSObject {
)

if let error = response.error {
reject("updateChallenge error", error, nil)
reject("updateChallengeError", error, nil)
} else {
resolve(response.data)
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-authsignal",
"version": "1.0.1",
"version": "1.0.2",
"description": "The official Authsignal React Native library.",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down
2 changes: 1 addition & 1 deletion react-native-authsignal.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Pod::Spec.new do |s|
s.source_files = "ios/**/*.{h,m,mm,swift}"

s.dependency "React-Core"
s.dependency 'Authsignal', '1.0.3'
s.dependency 'Authsignal', '1.0.4'

# Don't install the dependencies when we run `pod install` in the old architecture.
if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then
Expand Down
14 changes: 6 additions & 8 deletions src/push.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { NativeModules } from 'react-native';
import { LINKING_ERROR } from './error';
import type { AuthsignalResponse } from './types';
import type {
AuthsignalResponse,
PushChallenge,
PushCredential,
} from './types';

interface ConstructorArgs {
tenantID: string;
baseURL: string;
enableLogging: boolean;
}

interface PushCredential {
credentialId: string;
createdAt: string;
lastAuthenticatedAt?: string;
}

let initialized = false;

const AuthsignalPushModule = NativeModules.AuthsignalPushModule
Expand Down Expand Up @@ -97,7 +95,7 @@ export class AuthsignalPush {
}
}

async getChallenge(): Promise<AuthsignalResponse<string>> {
async getChallenge(): Promise<AuthsignalResponse<PushChallenge | undefined>> {
await this.ensureModuleIsInitialized();

try {
Expand Down
15 changes: 15 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,18 @@ export interface VerifyResponse {
token?: string;
failureReason?: string;
}

export interface PushCredential {
credentialId: string;
createdAt: string;
lastAuthenticatedAt?: string;
}

export interface PushChallenge {
challengeId: string;
actionCode?: string;
idempotencyKey?: string;
userAgent?: string;
deviceId?: string;
ipAddress?: string;
}

0 comments on commit 7d1cdfa

Please sign in to comment.