Skip to content

Commit

Permalink
Release 9.1.0 (#673)
Browse files Browse the repository at this point in the history
  • Loading branch information
DorianMazur authored Oct 24, 2024
1 parent 495248a commit 7eaf30e
Show file tree
Hide file tree
Showing 43 changed files with 1,670 additions and 64 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,6 @@ lib

# Docosaurus
/build
website/docs/api
.docusaurus
.cache-loader
2 changes: 0 additions & 2 deletions KeychainExample/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,13 @@ export default function App() {
accessControl,
securityLevel,
storage,
rules,
}
);
} else {
await Keychain.setGenericPassword(username, password, {
accessControl,
securityLevel,
storage,
rules,
});
}

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-keychain",
"version": "9.0.0",
"version": "9.1.0",
"description": "Keychain Access for React Native",
"main": "./lib/commonjs/index.js",
"module": "./lib/module/index",
Expand Down
58 changes: 34 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ import {
BIOMETRY_TYPE,
} from './enums';
import type {
Options,
Result,
UserCredentials,
SharedWebCredentials,
GetOptions,
BaseOptions,
SetOptions,
AuthenticationTypeOption,
AccessControlOption,
} from './types';
import { normalizeOptions, normalizeServerOption } from './normalizeOptions';
import {
normalizeOptions,
normalizeServerOption,
normalizeServiceOption,
} from './normalizeOptions';

const { RNKeychainManager } = NativeModules;

Expand All @@ -23,7 +31,7 @@ const { RNKeychainManager } = NativeModules;
*
* @param {string} username - The username or e-mail to be saved.
* @param {string} password - The password to be saved.
* @param {Options | string} [serviceOrOptions] - A keychain options object or a service name string. Passing a service name as a string is deprecated.
* @param {SetOptions | string} [serviceOrOptions] - A keychain options object or a service name string. Passing a service name as a string is deprecated.
*
* @returns {Promise<false | Result>} Resolves to an object containing `service` and `storage` when successful, or `false` on failure.
*
Expand All @@ -35,9 +43,9 @@ const { RNKeychainManager } = NativeModules;
export function setGenericPassword(
username: string,
password: string,
serviceOrOptions?: string | Options
serviceOrOptions?: string | SetOptions
): Promise<false | Result> {
const options = normalizeOptions(serviceOrOptions);
const options = normalizeServiceOption(serviceOrOptions);
return RNKeychainManager.setGenericPasswordForOptions(
options,
username,
Expand All @@ -48,7 +56,7 @@ export function setGenericPassword(
/**
* Fetches the `username` and `password` combination for the given service.
*
* @param {Options | string} [serviceOrOptions] - A keychain options object or a service name string.
* @param {GetOptions | string} [serviceOrOptions] - A keychain options object or a service name string.
*
* @returns {Promise<false | UserCredentials>} Resolves to an object containing `service`, `username`, `password`, and `storage` when successful, or `false` on failure.
*
Expand All @@ -63,7 +71,7 @@ export function setGenericPassword(
* ```
*/
export function getGenericPassword(
serviceOrOptions?: string | Options
serviceOrOptions?: string | GetOptions
): Promise<false | UserCredentials> {
const options = normalizeOptions(serviceOrOptions);
return RNKeychainManager.getGenericPasswordForOptions(options);
Expand All @@ -72,7 +80,7 @@ export function getGenericPassword(
/**
* Checks if generic password exists for the given service.
*
* @param {Options | string} [serviceOrOptions] - A keychain options object or a service name string.
* @param {BaseOptions | string} [serviceOrOptions] - A keychain options object or a service name string.
*
* @returns {Promise<boolean>} Resolves to `true` if a password exists, otherwise `false`.
*
Expand All @@ -83,16 +91,16 @@ export function getGenericPassword(
* ```
*/
export function hasGenericPassword(
serviceOrOptions?: string | Options
serviceOrOptions?: string | BaseOptions
): Promise<boolean> {
const options = normalizeOptions(serviceOrOptions);
const options = normalizeServiceOption(serviceOrOptions);
return RNKeychainManager.hasGenericPasswordForOptions(options);
}

/**
* Deletes all generic password keychain entries for the given service.
*
* @param {Options | string} [serviceOrOptions] - A keychain options object or a service name string.
* @param {BaseOptions | string} [serviceOrOptions] - A keychain options object or a service name string.
*
* @returns {Promise<boolean>} Resolves to `true` when successful, otherwise `false`.
*
Expand All @@ -103,9 +111,9 @@ export function hasGenericPassword(
* ```
*/
export function resetGenericPassword(
serviceOrOptions?: string | Options
serviceOrOptions?: string | BaseOptions
): Promise<boolean> {
const options = normalizeOptions(serviceOrOptions);
const options = normalizeServiceOption(serviceOrOptions);
return RNKeychainManager.resetGenericPasswordForOptions(options);
}

Expand Down Expand Up @@ -138,7 +146,7 @@ export function getAllGenericPasswordServices(): Promise<string[]> {
* ```
*/
export function hasInternetCredentials(
serverOrOptions: string | Options
serverOrOptions: string | BaseOptions
): Promise<boolean> {
const options = normalizeServerOption(serverOrOptions);
return RNKeychainManager.hasInternetCredentialsForOptions(options);
Expand All @@ -150,7 +158,7 @@ export function hasInternetCredentials(
* @param {string} server - The server URL.
* @param {string} username - The username or e-mail to be saved.
* @param {string} password - The password to be saved.
* @param {Options} [options] - A keychain options object.
* @param {SetOptions} [options] - A keychain options object.
*
* @returns {Promise<false | Result>} Resolves to an object containing `service` and `storage` when successful, or `false` on failure.
*
Expand All @@ -163,7 +171,7 @@ export function setInternetCredentials(
server: string,
username: string,
password: string,
options?: Options
options?: SetOptions
): Promise<false | Result> {
return RNKeychainManager.setInternetCredentialsForServer(
server,
Expand All @@ -177,7 +185,7 @@ export function setInternetCredentials(
* Fetches the internet credentials for the given server.
*
* @param {string} server - The server URL.
* @param {Options} [options] - A keychain options object.
* @param {GetOptions} [options] - A keychain options object.
*
* @returns {Promise<false | UserCredentials>} Resolves to an object containing `server`, `username`, `password`, and `storage` when successful, or `false` on failure.
*
Expand All @@ -193,7 +201,7 @@ export function setInternetCredentials(
*/
export function getInternetCredentials(
server: string,
options?: Options
options?: GetOptions
): Promise<false | UserCredentials> {
return RNKeychainManager.getInternetCredentialsForServer(
server,
Expand All @@ -204,7 +212,7 @@ export function getInternetCredentials(
/**
* Deletes all internet password keychain entries for the given server.
*
* @param {string} serverOrOptions - A keychain options object or a server name string.
* @param {BaseOptions | string} [serviceOrOptions] - A keychain options object or a service name string.
*
* @returns {Promise<void>} Resolves when the operation is completed.
*
Expand All @@ -215,7 +223,7 @@ export function getInternetCredentials(
* ```
*/
export function resetInternetCredentials(
serverOrOptions: string | Options
serverOrOptions: string | BaseOptions
): Promise<void> {
const options = normalizeServerOption(serverOrOptions);
return RNKeychainManager.resetInternetCredentialsForOptions(options);
Expand Down Expand Up @@ -311,7 +319,7 @@ export function setSharedWebCredentials(
*
* @platform iOS
*
* @param {Options} [options] - A keychain options object.
* @param {AuthenticationTypeOption} [options] - A keychain options object.
*
* @returns {Promise<boolean>} Resolves to `true` when supported, otherwise `false`.
*
Expand All @@ -321,7 +329,9 @@ export function setSharedWebCredentials(
* console.log('Can imply authentication:', canAuthenticate);
* ```
*/
export function canImplyAuthentication(options?: Options): Promise<boolean> {
export function canImplyAuthentication(
options?: AuthenticationTypeOption
): Promise<boolean> {
if (!RNKeychainManager.canCheckAuthentication) {
return Promise.resolve(false);
}
Expand All @@ -333,7 +343,7 @@ export function canImplyAuthentication(options?: Options): Promise<boolean> {
*
* @platform Android
*
* @param {Options} [options] - A keychain options object.
* @param {AccessControlOption} [options] - A keychain options object.
*
* @returns {Promise<null | SECURITY_LEVEL>} Resolves to a `SECURITY_LEVEL` when supported, otherwise `null`.
*
Expand All @@ -344,7 +354,7 @@ export function canImplyAuthentication(options?: Options): Promise<boolean> {
* ```
*/
export function getSecurityLevel(
options?: Options
options?: AccessControlOption
): Promise<null | SECURITY_LEVEL> {
if (!RNKeychainManager.getSecurityLevel) {
return Promise.resolve(null);
Expand Down
17 changes: 10 additions & 7 deletions src/normalizeOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AuthenticationPrompt, Options } from './types';
import type { AuthenticationPrompt, BaseOptions, SetOptions } from './types';

// Default authentication prompt options
export const AUTH_PROMPT_DEFAULTS: AuthenticationPrompt = {
Expand All @@ -7,8 +7,8 @@ export const AUTH_PROMPT_DEFAULTS: AuthenticationPrompt = {
};

export function normalizeServiceOption(
serviceOrOptions?: string | Options
): Options {
serviceOrOptions?: string | BaseOptions
): BaseOptions {
if (typeof serviceOrOptions === 'string') {
console.warn(
`You passed a service string as an argument to one of the react-native-keychain functions.
Expand All @@ -23,8 +23,8 @@ export function normalizeServiceOption(
}

export function normalizeServerOption(
serverOrOptions?: string | Options
): Options {
serverOrOptions?: string | BaseOptions
): BaseOptions {
if (typeof serverOrOptions === 'string') {
console.warn(
`You passed a server string as an argument to one of the react-native-keychain functions.
Expand All @@ -38,10 +38,13 @@ export function normalizeServerOption(
return serverOrOptions || {};
}

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

if (typeof authenticationPrompt === 'string') {
Expand Down
53 changes: 35 additions & 18 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,21 @@ export type AuthenticationPrompt = {
cancel?: string;
};

export type BaseOptions = {
/** The service name to associate with the keychain item.
* @default 'App bundle ID'
*/
service?: string;
/** The server name to associate with the keychain item. */
server?: string;
/** Whether to synchronize the keychain item to iCloud.
* @platform iOS
*/
cloudSync?: boolean;
};

/** Base options for keychain functions. */
export type Options = {
/** The access control policy to use for the keychain item. */
accessControl?: ACCESS_CONTROL;
export type SetOptions = {
/** The access group to share keychain items between apps.
* @platform iOS, visionOS
*/
Expand All @@ -40,17 +51,6 @@ export type Options = {
* @default ACCESSIBLE.AFTER_FIRST_UNLOCK
*/
accessible?: ACCESSIBLE;
/** Authentication type for retrieving keychain item.
* @platform iOS, visionOS
* @default AUTHENTICATION_TYPE.DEVICE_PASSCODE_OR_BIOMETRICS
*/
authenticationType?: AUTHENTICATION_TYPE;
/** The service name to associate with the keychain item.
* @default 'App bundle ID'
*/
service?: string;
/** The server name to associate with the keychain item. */
server?: string;
/** The desired security level of the keychain item.
* @platform Android
*/
Expand All @@ -60,15 +60,18 @@ export type Options = {
* @default 'Best available storage'
*/
storage?: STORAGE_TYPE;
} & BaseOptions &
AccessControlOption;

/** Base options for keychain functions. */
export type GetOptions = {
/** The access control policy to use for the keychain item. */
accessControl?: ACCESS_CONTROL;
/** The security rules to apply when storing the keychain item.
* @platform Android
* @default SECURITY_RULES.AUTOMATIC_UPGRADE
*/
rules?: SECURITY_RULES;
/** Whether to synchronize the keychain item to iCloud.
* @platform iOS
*/
cloudSync?: boolean;
/** Authentication prompt details or a title string.
* @default
* ```json
Expand All @@ -80,6 +83,20 @@ export type Options = {
*
*/
authenticationPrompt?: string | AuthenticationPrompt;
} & BaseOptions &
AccessControlOption;

export type AccessControlOption = {
/** The access control policy to use for the keychain item. */
accessControl?: ACCESS_CONTROL;
};

export type AuthenticationTypeOption = {
/** Authentication type for retrieving keychain item.
* @platform iOS, visionOS
* @default AUTHENTICATION_TYPE.DEVICE_PASSCODE_OR_BIOMETRICS
*/
authenticationType?: AUTHENTICATION_TYPE;
};

/**
Expand Down
5 changes: 2 additions & 3 deletions website/docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ const config: Config = {
{
docs: {
path: './docs/',
sidebarPath: './sidebars.ts',
sidebarPath: require.resolve('./sidebars.ts'),
sidebarCollapsible: false,
editUrl: `${REPO_URL}/edit/master/website/docs/`,
lastVersion: 'current',
versions: {
Expand Down Expand Up @@ -70,8 +71,6 @@ const config: Config = {
type: 'docsVersionDropdown',
position: 'left',
dropdownActiveClassDisabled: true,
dropdownItemsBefore: [],
dropdownItemsAfter: [],
},
{
type: 'doc',
Expand Down
Loading

0 comments on commit 7eaf30e

Please sign in to comment.