2016-11-04 10:42:12 +00:00
|
|
|
import { NativeModules, Platform } from 'react-native';
|
|
|
|
const { RNKeychainManager } = NativeModules;
|
2015-05-20 16:23:04 +00:00
|
|
|
|
2018-12-19 08:11:10 +00:00
|
|
|
export const SECURITY_LEVEL = {
|
|
|
|
ANY: 'ANY',
|
|
|
|
SECURE_SOFTWARE: 'SECURE_SOFTWARE',
|
|
|
|
SECURE_HARDWARE: 'SECURE_HARDWARE',
|
|
|
|
};
|
|
|
|
|
2018-02-25 16:29:39 +00:00
|
|
|
export const ACCESSIBLE = {
|
|
|
|
WHEN_UNLOCKED: 'AccessibleWhenUnlocked',
|
|
|
|
AFTER_FIRST_UNLOCK: 'AccessibleAfterFirstUnlock',
|
|
|
|
ALWAYS: 'AccessibleAlways',
|
|
|
|
WHEN_PASSCODE_SET_THIS_DEVICE_ONLY: 'AccessibleWhenPasscodeSetThisDeviceOnly',
|
|
|
|
WHEN_UNLOCKED_THIS_DEVICE_ONLY: 'AccessibleWhenUnlockedThisDeviceOnly',
|
|
|
|
AFTER_FIRST_UNLOCK_THIS_DEVICE_ONLY:
|
|
|
|
'AccessibleAfterFirstUnlockThisDeviceOnly',
|
|
|
|
ALWAYS_THIS_DEVICE_ONLY: 'AccessibleAlwaysThisDeviceOnly',
|
|
|
|
};
|
|
|
|
|
|
|
|
export const ACCESS_CONTROL = {
|
|
|
|
USER_PRESENCE: 'UserPresence',
|
2018-02-25 23:14:48 +00:00
|
|
|
BIOMETRY_ANY: 'BiometryAny',
|
|
|
|
BIOMETRY_CURRENT_SET: 'BiometryCurrentSet',
|
2018-02-25 16:29:39 +00:00
|
|
|
DEVICE_PASSCODE: 'DevicePasscode',
|
2018-02-26 09:49:33 +00:00
|
|
|
APPLICATION_PASSWORD: 'ApplicationPassword',
|
2018-02-25 23:14:48 +00:00
|
|
|
BIOMETRY_ANY_OR_DEVICE_PASSCODE: 'BiometryAnyOrDevicePasscode',
|
|
|
|
BIOMETRY_CURRENT_SET_OR_DEVICE_PASSCODE: 'BiometryCurrentSetOrDevicePasscode',
|
2018-02-25 16:29:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const AUTHENTICATION_TYPE = {
|
|
|
|
DEVICE_PASSCODE_OR_BIOMETRICS: 'AuthenticationWithBiometricsDevicePasscode',
|
|
|
|
BIOMETRICS: 'AuthenticationWithBiometrics',
|
|
|
|
};
|
|
|
|
|
2018-02-25 16:05:33 +00:00
|
|
|
export const BIOMETRY_TYPE = {
|
|
|
|
TOUCH_ID: 'TouchID',
|
|
|
|
FACE_ID: 'FaceID',
|
2018-02-27 15:20:37 +00:00
|
|
|
FINGERPRINT: 'Fingerprint',
|
2018-02-25 16:05:33 +00:00
|
|
|
};
|
|
|
|
|
2018-12-19 08:11:10 +00:00
|
|
|
type SecMinimumLevel =
|
|
|
|
| 'ANY'
|
|
|
|
| 'SECURE_SOFTWARE'
|
|
|
|
| 'SECURE_HARDWARE' ;
|
|
|
|
|
2017-02-10 08:34:58 +00:00
|
|
|
type SecAccessible =
|
|
|
|
| 'AccessibleWhenUnlocked'
|
|
|
|
| 'AccessibleAfterFirstUnlock'
|
|
|
|
| 'AccessibleAlways'
|
|
|
|
| 'AccessibleWhenPasscodeSetThisDeviceOnly'
|
|
|
|
| 'AccessibleWhenUnlockedThisDeviceOnly'
|
|
|
|
| 'AccessibleAfterFirstUnlockThisDeviceOnly'
|
2018-02-25 15:14:17 +00:00
|
|
|
| 'AccessibleAlwaysThisDeviceOnly';
|
2017-02-10 08:34:58 +00:00
|
|
|
|
2018-02-25 14:55:17 +00:00
|
|
|
type SecAccessControl =
|
|
|
|
| 'UserPresence'
|
2018-02-25 23:14:48 +00:00
|
|
|
| 'BiometryAny'
|
|
|
|
| 'BiometryCurrentSet'
|
2018-02-25 14:55:17 +00:00
|
|
|
| 'DevicePasscode'
|
2018-02-26 09:49:33 +00:00
|
|
|
| 'ApplicationPassword'
|
2018-02-25 23:14:48 +00:00
|
|
|
| 'BiometryAnyOrDevicePasscode'
|
|
|
|
| 'BiometryCurrentSetOrDevicePasscode';
|
2018-02-25 14:55:17 +00:00
|
|
|
|
2018-02-25 15:14:17 +00:00
|
|
|
type LAPolicy = 'Authentication' | 'AuthenticationWithBiometrics';
|
2018-02-25 14:55:17 +00:00
|
|
|
|
2018-02-26 11:31:06 +00:00
|
|
|
type Options = {
|
2018-02-25 22:53:42 +00:00
|
|
|
accessControl?: SecAccessControl,
|
|
|
|
accessGroup?: string,
|
2018-02-26 11:31:06 +00:00
|
|
|
accessible?: SecAccessible,
|
2018-02-25 22:42:15 +00:00
|
|
|
authenticationPrompt?: string,
|
2018-02-25 15:14:17 +00:00
|
|
|
authenticationType?: LAPolicy,
|
2018-02-25 22:53:42 +00:00
|
|
|
service?: string,
|
2018-02-25 14:55:17 +00:00
|
|
|
};
|
|
|
|
|
2018-12-19 08:11:10 +00:00
|
|
|
/**
|
|
|
|
* (Android only) Returns guaranteed security level supported by this library
|
|
|
|
* on the current device.
|
|
|
|
* @return {Promise} Resolves to `SECURITY_LEVEL` when supported, otherwise `null`.
|
|
|
|
*/
|
|
|
|
export function getSecurityLevel(): Promise {
|
|
|
|
if (!RNKeychainManager.getSecurityLevel){
|
|
|
|
return Promise.resolve(null);
|
|
|
|
}
|
|
|
|
return RNKeychainManager.getSecurityLevel();
|
|
|
|
}
|
|
|
|
|
2018-02-25 14:55:17 +00:00
|
|
|
/**
|
|
|
|
* Inquire if the type of local authentication policy (LAPolicy) is supported
|
|
|
|
* on this device with the device settings the user chose.
|
|
|
|
* @param {object} options LAPolicy option, iOS only
|
2018-02-25 16:07:18 +00:00
|
|
|
* @return {Promise} Resolves to `true` when supported, otherwise `false`
|
2018-02-25 14:55:17 +00:00
|
|
|
*/
|
2018-02-26 11:31:06 +00:00
|
|
|
export function canImplyAuthentication(options?: Options): Promise {
|
2018-02-27 10:51:13 +00:00
|
|
|
if (!RNKeychainManager.canCheckAuthentication) {
|
|
|
|
return Promise.resolve(false);
|
2018-02-25 15:29:33 +00:00
|
|
|
}
|
2018-02-25 14:55:17 +00:00
|
|
|
return RNKeychainManager.canCheckAuthentication(options);
|
|
|
|
}
|
|
|
|
|
2018-02-25 16:05:33 +00:00
|
|
|
/**
|
2018-02-27 12:58:37 +00:00
|
|
|
* Get what type of hardware biometry support the device has.
|
2018-02-25 16:05:33 +00:00
|
|
|
* @return {Promise} Resolves to a `BIOMETRY_TYPE` when supported, otherwise `null`
|
|
|
|
*/
|
|
|
|
export function getSupportedBiometryType(): Promise {
|
2018-02-25 19:21:01 +00:00
|
|
|
if (!RNKeychainManager.getSupportedBiometryType) {
|
2018-02-25 18:23:36 +00:00
|
|
|
return Promise.resolve(null);
|
2018-02-25 16:05:33 +00:00
|
|
|
}
|
|
|
|
return RNKeychainManager.getSupportedBiometryType();
|
|
|
|
}
|
|
|
|
|
2016-11-04 10:42:12 +00:00
|
|
|
/**
|
2017-01-10 22:00:51 +00:00
|
|
|
* Saves the `username` and `password` combination for `server`.
|
|
|
|
* @param {string} server URL to server.
|
|
|
|
* @param {string} username Associated username or e-mail to be saved.
|
|
|
|
* @param {string} password Associated password to be saved.
|
2018-12-19 08:11:10 +00:00
|
|
|
* @param {string} minimumSecurityLevel `SECURITY_LEVEL` defines which security
|
|
|
|
* level is minimally acceptable for this password.
|
2017-02-10 09:58:39 +00:00
|
|
|
* @param {object} options Keychain options, iOS only
|
2017-01-10 22:00:51 +00:00
|
|
|
* @return {Promise} Resolves to `true` when successful
|
2016-11-04 10:42:12 +00:00
|
|
|
*/
|
|
|
|
export function setInternetCredentials(
|
|
|
|
server: string,
|
|
|
|
username: string,
|
2017-02-10 08:34:58 +00:00
|
|
|
password: string,
|
2018-12-19 08:11:10 +00:00
|
|
|
minimumSecurityLevel?: SecMinimumLevel,
|
2017-02-10 09:58:39 +00:00
|
|
|
options?: Options
|
2016-11-04 10:42:12 +00:00
|
|
|
): Promise {
|
2018-02-25 15:14:17 +00:00
|
|
|
return RNKeychainManager.setInternetCredentialsForServer(
|
|
|
|
server,
|
|
|
|
username,
|
|
|
|
password,
|
2018-12-19 08:11:10 +00:00
|
|
|
getMinimumSecurityLevel(minimumSecurityLevel),
|
2018-02-25 15:14:17 +00:00
|
|
|
options
|
|
|
|
);
|
2016-11-04 10:42:12 +00:00
|
|
|
}
|
2015-05-20 18:39:52 +00:00
|
|
|
|
2016-11-04 10:42:12 +00:00
|
|
|
/**
|
2017-01-10 22:00:51 +00:00
|
|
|
* Fetches login combination for `server`.
|
|
|
|
* @param {string} server URL to server.
|
2017-02-10 09:58:39 +00:00
|
|
|
* @param {object} options Keychain options, iOS only
|
2017-01-10 22:00:51 +00:00
|
|
|
* @return {Promise} Resolves to `{ server, username, password }` when successful
|
2016-11-04 10:42:12 +00:00
|
|
|
*/
|
|
|
|
export function getInternetCredentials(
|
2017-02-10 09:58:39 +00:00
|
|
|
server: string,
|
|
|
|
options?: Options
|
2016-11-04 10:42:12 +00:00
|
|
|
): Promise {
|
2017-02-10 09:58:39 +00:00
|
|
|
return RNKeychainManager.getInternetCredentialsForServer(server, options);
|
2016-11-04 10:42:12 +00:00
|
|
|
}
|
2015-05-20 18:39:52 +00:00
|
|
|
|
2016-11-04 10:42:12 +00:00
|
|
|
/**
|
2017-01-10 22:00:51 +00:00
|
|
|
* Deletes all internet password keychain entries for `server`.
|
|
|
|
* @param {string} server URL to server.
|
2017-02-10 09:58:39 +00:00
|
|
|
* @param {object} options Keychain options, iOS only
|
2017-01-10 22:00:51 +00:00
|
|
|
* @return {Promise} Resolves to `true` when successful
|
2016-11-04 10:42:12 +00:00
|
|
|
*/
|
|
|
|
export function resetInternetCredentials(
|
2017-02-10 09:58:39 +00:00
|
|
|
server: string,
|
|
|
|
options?: Options
|
2016-11-04 10:42:12 +00:00
|
|
|
): Promise {
|
2017-02-10 09:58:39 +00:00
|
|
|
return RNKeychainManager.resetInternetCredentialsForServer(server, options);
|
|
|
|
}
|
|
|
|
|
2018-02-26 11:31:06 +00:00
|
|
|
function getOptionsArgument(serviceOrOptions?: string | Options) {
|
2017-02-10 09:58:39 +00:00
|
|
|
if (Platform.OS !== 'ios') {
|
2018-02-25 15:14:17 +00:00
|
|
|
return typeof serviceOrOptions === 'object'
|
|
|
|
? serviceOrOptions.service
|
|
|
|
: serviceOrOptions;
|
2017-02-10 09:58:39 +00:00
|
|
|
}
|
2018-02-25 15:14:17 +00:00
|
|
|
return typeof serviceOrOptions === 'string'
|
|
|
|
? { service: serviceOrOptions }
|
|
|
|
: serviceOrOptions;
|
2016-11-04 10:42:12 +00:00
|
|
|
}
|
2015-05-20 16:23:04 +00:00
|
|
|
|
2018-12-19 08:11:10 +00:00
|
|
|
function getMinimumSecurityLevel(minimumSecurityLevel?: SecMinimumLevel) {
|
|
|
|
if (minimumSecurityLevel === undefined) {
|
|
|
|
return SECURITY_LEVEL.ANY;
|
|
|
|
} else {
|
|
|
|
return minimumSecurityLevel
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-04 10:42:12 +00:00
|
|
|
/**
|
2017-01-10 22:00:51 +00:00
|
|
|
* Saves the `username` and `password` combination for `service`.
|
|
|
|
* @param {string} username Associated username or e-mail to be saved.
|
|
|
|
* @param {string} password Associated password to be saved.
|
2018-12-19 08:11:10 +00:00
|
|
|
* @param {string} minimumSecurityLevel `SECURITY_LEVEL` defines which security
|
|
|
|
* level is minimally acceptable for this password.
|
2017-02-10 09:58:39 +00:00
|
|
|
* @param {string|object} serviceOrOptions Reverse domain name qualifier for the service, defaults to `bundleId` or an options object.
|
2017-01-10 22:00:51 +00:00
|
|
|
* @return {Promise} Resolves to `true` when successful
|
2016-11-04 10:42:12 +00:00
|
|
|
*/
|
|
|
|
export function setGenericPassword(
|
|
|
|
username: string,
|
|
|
|
password: string,
|
2018-12-19 08:11:10 +00:00
|
|
|
minimumSecurityLevel?: SecMinimumLevel,
|
2018-02-26 11:31:06 +00:00
|
|
|
serviceOrOptions?: string | Options
|
2016-11-04 10:42:12 +00:00
|
|
|
): Promise {
|
2018-02-25 15:14:17 +00:00
|
|
|
return RNKeychainManager.setGenericPasswordForOptions(
|
|
|
|
getOptionsArgument(serviceOrOptions),
|
|
|
|
username,
|
2018-12-19 08:11:10 +00:00
|
|
|
password,
|
|
|
|
getMinimumSecurityLevel(minimumSecurityLevel)
|
2018-06-30 09:21:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-15 12:22:05 +00:00
|
|
|
/**
|
|
|
|
* Saves the `username` for further use on get requests.
|
|
|
|
* @param {string} username Associated username or e-mail to be saved.
|
|
|
|
* @return {Promise} Resolves to `true` when successful
|
|
|
|
*/
|
|
|
|
export function setUsername(
|
|
|
|
username: string
|
|
|
|
): Promise {
|
|
|
|
return RNKeychainManager.setUsername(
|
|
|
|
username
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-04 10:42:12 +00:00
|
|
|
/**
|
2017-01-10 22:00:51 +00:00
|
|
|
* Fetches login combination for `service`.
|
2017-02-10 09:58:39 +00:00
|
|
|
* @param {string|object} serviceOrOptions Reverse domain name qualifier for the service, defaults to `bundleId` or an options object.
|
2017-01-10 22:00:51 +00:00
|
|
|
* @return {Promise} Resolves to `{ service, username, password }` when successful
|
2016-11-04 10:42:12 +00:00
|
|
|
*/
|
|
|
|
export function getGenericPassword(
|
2018-02-26 11:31:06 +00:00
|
|
|
serviceOrOptions?: string | Options
|
2016-11-04 10:42:12 +00:00
|
|
|
): Promise {
|
2018-02-25 15:14:17 +00:00
|
|
|
return RNKeychainManager.getGenericPasswordForOptions(
|
|
|
|
getOptionsArgument(serviceOrOptions)
|
|
|
|
);
|
2016-11-04 10:42:12 +00:00
|
|
|
}
|
2015-05-29 16:25:56 +00:00
|
|
|
|
2016-11-04 10:42:12 +00:00
|
|
|
/**
|
2017-01-10 22:00:51 +00:00
|
|
|
* Deletes all generic password keychain entries for `service`.
|
2017-02-10 09:58:39 +00:00
|
|
|
* @param {string|object} serviceOrOptions Reverse domain name qualifier for the service, defaults to `bundleId` or an options object.
|
2017-01-10 22:00:51 +00:00
|
|
|
* @return {Promise} Resolves to `true` when successful
|
2016-11-04 10:42:12 +00:00
|
|
|
*/
|
|
|
|
export function resetGenericPassword(
|
2018-02-26 11:31:06 +00:00
|
|
|
serviceOrOptions?: string | Options
|
2016-11-04 10:42:12 +00:00
|
|
|
): Promise {
|
2018-02-25 15:14:17 +00:00
|
|
|
return RNKeychainManager.resetGenericPasswordForOptions(
|
|
|
|
getOptionsArgument(serviceOrOptions)
|
|
|
|
);
|
2015-05-20 18:39:52 +00:00
|
|
|
}
|
2017-01-10 21:25:04 +00:00
|
|
|
|
|
|
|
/**
|
2017-01-10 22:00:51 +00:00
|
|
|
* Asks the user for a shared web credential.
|
|
|
|
* @return {Promise} Resolves to `{ server, username, password }` if approved and
|
|
|
|
* `false` if denied and throws an error if not supported on platform or there's no shared credentials
|
2017-01-10 21:25:04 +00:00
|
|
|
*/
|
2018-02-25 15:14:17 +00:00
|
|
|
export function requestSharedWebCredentials(): Promise {
|
2017-01-10 21:25:04 +00:00
|
|
|
if (Platform.OS !== 'ios') {
|
2018-02-25 15:14:17 +00:00
|
|
|
return Promise.reject(
|
|
|
|
new Error(
|
|
|
|
`requestSharedWebCredentials() is not supported on ${Platform.OS} yet`
|
|
|
|
)
|
|
|
|
);
|
2017-01-10 21:25:04 +00:00
|
|
|
}
|
|
|
|
return RNKeychainManager.requestSharedWebCredentials();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a shared web credential.
|
2017-01-10 22:00:51 +00:00
|
|
|
* @param {string} server URL to server.
|
|
|
|
* @param {string} username Associated username or e-mail to be saved.
|
|
|
|
* @param {string} password Associated password to be saved.
|
|
|
|
* @return {Promise} Resolves to `true` when successful
|
2017-01-10 21:25:04 +00:00
|
|
|
*/
|
|
|
|
export function setSharedWebCredentials(
|
|
|
|
server: string,
|
|
|
|
username: string,
|
|
|
|
password: string
|
2018-02-25 15:14:17 +00:00
|
|
|
): Promise {
|
2017-01-10 21:25:04 +00:00
|
|
|
if (Platform.OS !== 'ios') {
|
2018-02-25 15:14:17 +00:00
|
|
|
return Promise.reject(
|
|
|
|
new Error(
|
|
|
|
`setSharedWebCredentials() is not supported on ${Platform.OS} yet`
|
|
|
|
)
|
|
|
|
);
|
2017-01-10 21:25:04 +00:00
|
|
|
}
|
2018-02-25 15:14:17 +00:00
|
|
|
return RNKeychainManager.setSharedWebCredentialsForServer(
|
|
|
|
server,
|
|
|
|
username,
|
|
|
|
password
|
|
|
|
);
|
2017-01-10 21:25:04 +00:00
|
|
|
}
|