import { BiometricPrivateKeyOptions, BiometricOptions } from "./types"; import { _wrapError } from "./error"; import AuthgearReactNative from "./NativeAuthgearReactNative"; // In ios objc code, reading a BOOL from a NSDictionary always got true // So encode the boolean as string before passing to native type BooleanString = "false" | "true"; async function _wrapPromise(p: Promise): Promise { try { const val = await p; return val; } catch (e: unknown) { throw _wrapError(e); } } // Objective-C cannot return a correct boolean. Use string to represent a boolean. function parseBooleanString(str: unknown): boolean { return str === "true"; } export async function storageGetItem(key: string): Promise { return _wrapPromise(AuthgearReactNative.storageGetItem(key)); } export async function storageSetItem( key: string, value: string ): Promise { return _wrapPromise(AuthgearReactNative.storageSetItem(key, value)); } export async function storageDeleteItem(key: string): Promise { return _wrapPromise(AuthgearReactNative.storageDeleteItem(key)); } export async function getDeviceInfo(): Promise { return _wrapPromise(AuthgearReactNative.getDeviceInfo()); } export async function randomBytes(length: number): Promise { return _wrapPromise(AuthgearReactNative.randomBytes(length)); } export async function sha256String(input: string): Promise { return _wrapPromise(AuthgearReactNative.sha256String(input)); } export async function generateUUID(): Promise { return _wrapPromise(AuthgearReactNative.generateUUID()); } export async function openAuthorizeURL( url: string, callbackURL: string, prefersEphemeralWebBrowserSession: boolean ): Promise { const redirectURI: string = await _wrapPromise( AuthgearReactNative.openAuthorizeURL( url, callbackURL, prefersEphemeralWebBrowserSession ) ); await dismiss(); return redirectURI; } export async function openAuthorizeURLWithWebView(options: { invocationID: string; url: string; redirectURI: string; navigationBarBackgroundColor?: string; navigationBarButtonTintColor?: string; modalPresentationStyle?: string; iosIsInspectable?: BooleanString; actionBarBackgroundColor?: string; actionBarButtonTintColor?: string; iosWechatRedirectURI?: string; androidWechatRedirectURI?: string; }): Promise { const redirectURIWithQuery: string = await _wrapPromise( AuthgearReactNative.openAuthorizeURLWithWebView(options) ); await dismiss(); return redirectURIWithQuery; } export async function dismiss(): Promise { return _wrapPromise(AuthgearReactNative.dismiss()); } export async function getAnonymousKey( kid: string | null ): Promise<{ kid: string; alg: string; jwk?: any }> { return _wrapPromise(AuthgearReactNative.getAnonymousKey(kid)); } export async function signAnonymousToken( kid: string, tokenData: string ): Promise { return _wrapPromise(AuthgearReactNative.signAnonymousToken(kid, tokenData)); } export async function createBiometricPrivateKey( options: BiometricPrivateKeyOptions ): Promise { return _wrapPromise(AuthgearReactNative.createBiometricPrivateKey(options)); } export async function signWithBiometricPrivateKey( options: BiometricPrivateKeyOptions ): Promise { return _wrapPromise(AuthgearReactNative.signWithBiometricPrivateKey(options)); } export async function removeBiometricPrivateKey(kid: string): Promise { return _wrapPromise(AuthgearReactNative.removeBiometricPrivateKey(kid)); } export async function checkBiometricSupported( options: BiometricOptions ): Promise { return _wrapPromise(AuthgearReactNative.checkBiometricSupported(options)); } export async function checkDPoPSupported(): Promise { // Call with empty object, // because it seems there is some problem in objc if we call a method without arguments. const result = await _wrapPromise(AuthgearReactNative.checkDPoPSupported({})); return parseBooleanString(result); } export async function createDPoPPrivateKey(kid: string): Promise { return _wrapPromise(AuthgearReactNative.createDPoPPrivateKey({ kid })); } export async function signWithDPoPPrivateKey( kid: string, payload: Record ): Promise { return _wrapPromise( AuthgearReactNative.signWithDPoPPrivateKey({ kid, payload }) ); } export async function checkDPoPPrivateKey(kid: string): Promise { const result = await _wrapPromise( AuthgearReactNative.checkDPoPPrivateKey({ kid }) ); return parseBooleanString(result); } export async function computeDPoPJKT(kid: string): Promise { return _wrapPromise(AuthgearReactNative.computeDPoPJKT({ kid })); }