Skip to content

Commit

Permalink
fix: authenticationPrompt for SetOptions (#683)
Browse files Browse the repository at this point in the history
* fix: authenticationPrompt for SetOptions

* fix: revert change
  • Loading branch information
DorianMazur authored Nov 12, 2024
1 parent dabc2f9 commit e8b6dfd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
4 changes: 2 additions & 2 deletions android/src/main/java/com/oblador/keychain/KeychainModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ class KeychainModule(reactContext: ReactApplicationContext) :
useBiometry && (isFingerprintAuthAvailable || isFaceAuthAvailable || isIrisAuthAvailable)
var foundCipher: CipherStorage? = null
for (variant in cipherStorageMap.values) {
Log.d(KEYCHAIN_MODULE, "Probe cipher storage: " + variant.javaClass.simpleName)
Log.d(KEYCHAIN_MODULE, "Probe cipher storage: " + variant.getCipherStorageName())

// Is the cipherStorage supported on the current API level?
val minApiLevel = variant.getMinSupportedApiLevel()
Expand All @@ -616,7 +616,7 @@ class KeychainModule(reactContext: ReactApplicationContext) :
if (foundCipher == null) {
throw CryptoFailedException("Unsupported Android SDK " + Build.VERSION.SDK_INT)
}
Log.d(KEYCHAIN_MODULE, "Selected storage: " + foundCipher.javaClass.simpleName)
Log.d(KEYCHAIN_MODULE, "Selected storage: " + foundCipher.getCipherStorageName())
return foundCipher
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import javax.crypto.SecretKeyFactory
import javax.crypto.spec.GCMParameterSpec

@TargetApi(Build.VERSION_CODES.M)
class CipherStorageKeystoreAesGcm(reactContext: ReactApplicationContext, val requiresBiometricAuth: Boolean) :
class CipherStorageKeystoreAesGcm(reactContext: ReactApplicationContext, private val requiresBiometricAuth: Boolean) :
CipherStorageBase(reactContext) {

// region Constants
Expand Down
7 changes: 2 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
normalizeOptions,
normalizeServerOption,
normalizeServiceOption,
normalizeStorageOptions,
} from './normalizeOptions';

const { RNKeychainManager } = NativeModules;
Expand All @@ -46,9 +45,7 @@ export function setGenericPassword(
password: string,
serviceOrOptions?: string | SetOptions
): Promise<false | Result> {
const options = normalizeStorageOptions(
normalizeServiceOption(serviceOrOptions)
);
const options = normalizeOptions(serviceOrOptions);
return RNKeychainManager.setGenericPasswordForOptions(
options,
username,
Expand Down Expand Up @@ -180,7 +177,7 @@ export function setInternetCredentials(
server,
username,
password,
options ? normalizeStorageOptions(options) : {}
options ? normalizeOptions(options) : {}
);
}

Expand Down
22 changes: 15 additions & 7 deletions src/normalizeOptions.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { STORAGE_TYPE } from './enums';
import type { AuthenticationPrompt, BaseOptions, SetOptions } from './types';
import type {
AuthenticationPrompt,
BaseOptions,
GetOptions,
SetOptions,
} from './types';

// Default authentication prompt options
export const AUTH_PROMPT_DEFAULTS: AuthenticationPrompt = {
title: 'Authenticate to retrieve secret',
cancel: 'Cancel',
};

export function normalizeStorageOptions(options: SetOptions): SetOptions {
if (options.storage && options.storage === STORAGE_TYPE.AES) {
export function normalizeStorageOptions(
options: SetOptions | GetOptions
): SetOptions {
if ('storage' in options && options.storage === STORAGE_TYPE.AES) {
console.warn(
`You passed 'AES' as a storage option to one of the react-native-keychain functions.
This way of passing storage is deprecated and will be removed in a future major.`
Expand Down Expand Up @@ -51,12 +58,13 @@ export function normalizeServerOption(
}

export function normalizeOptions(
serviceOrOptions?: string | SetOptions
): SetOptions {
const options = {
serviceOrOptions?: string | SetOptions | GetOptions
): SetOptions | GetOptions {
const options = normalizeStorageOptions({
authenticationPrompt: AUTH_PROMPT_DEFAULTS,
...normalizeServiceOption(serviceOrOptions),
};
});

const { authenticationPrompt } = options;

if (typeof authenticationPrompt === 'string') {
Expand Down
11 changes: 11 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ export type SetOptions = {
* @default 'Best available storage'
*/
storage?: STORAGE_TYPE;
/** Authentication prompt details or a title string.
* @default
* ```json
* {
* "title": "Authenticate to retrieve secret",
* "cancel": "Cancel"
* }
* ```
*
*/
authenticationPrompt?: string | AuthenticationPrompt;
} & BaseOptions &
AccessControlOption;

Expand Down

0 comments on commit e8b6dfd

Please sign in to comment.