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
|
|
|
|
2017-02-10 08:34:58 +00:00
|
|
|
type SecAccessible =
|
|
|
|
| 'AccessibleWhenUnlocked'
|
|
|
|
| 'AccessibleAfterFirstUnlock'
|
|
|
|
| 'AccessibleAlways'
|
|
|
|
| 'AccessibleWhenPasscodeSetThisDeviceOnly'
|
|
|
|
| 'AccessibleWhenUnlockedThisDeviceOnly'
|
|
|
|
| 'AccessibleAfterFirstUnlockThisDeviceOnly'
|
|
|
|
| 'AccessibleAlwaysThisDeviceOnly'
|
|
|
|
|
2017-02-10 09:58:39 +00:00
|
|
|
type Options = {
|
|
|
|
accessible?: SecAccessible;
|
|
|
|
service?: string;
|
|
|
|
};
|
|
|
|
|
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.
|
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,
|
2017-02-10 09:58:39 +00:00
|
|
|
options?: Options
|
2016-11-04 10:42:12 +00:00
|
|
|
): Promise {
|
2017-02-10 09:58:39 +00:00
|
|
|
return RNKeychainManager.setInternetCredentialsForServer(server, username, password, 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getOptionsArgument(serviceOrOptions?: string | KeychainOptions) {
|
|
|
|
if (Platform.OS !== 'ios') {
|
|
|
|
return typeof serviceOrOptions === 'object' ? serviceOrOptions.service : serviceOrOptions;
|
|
|
|
}
|
|
|
|
return typeof serviceOrOptions === 'string' ? { service: serviceOrOptions } : serviceOrOptions;
|
2016-11-04 10:42:12 +00:00
|
|
|
}
|
2015-05-20 16:23:04 +00:00
|
|
|
|
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.
|
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,
|
2017-02-10 09:58:39 +00:00
|
|
|
serviceOrOptions?: string | KeychainOptions
|
2016-11-04 10:42:12 +00:00
|
|
|
): Promise {
|
2017-02-10 09:58:39 +00:00
|
|
|
return RNKeychainManager.setGenericPasswordForOptions(getOptionsArgument(serviceOrOptions), username, password);
|
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
|
|
|
* 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(
|
2017-02-10 09:58:39 +00:00
|
|
|
serviceOrOptions?: string | KeychainOptions
|
2016-11-04 10:42:12 +00:00
|
|
|
): Promise {
|
2017-02-10 09:58:39 +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(
|
2017-02-10 09:58:39 +00:00
|
|
|
serviceOrOptions?: string | KeychainOptions
|
2016-11-04 10:42:12 +00:00
|
|
|
): Promise {
|
2017-02-10 09:58:39 +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
|
|
|
*/
|
|
|
|
export function requestSharedWebCredentials() : Promise {
|
|
|
|
if (Platform.OS !== 'ios') {
|
|
|
|
return Promise.reject(new Error(`requestSharedWebCredentials() is not supported on ${Platform.OS} yet`));
|
|
|
|
}
|
|
|
|
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
|
|
|
|
) : Promise {
|
|
|
|
if (Platform.OS !== 'ios') {
|
|
|
|
return Promise.reject(new Error(`setSharedWebCredentials() is not supported on ${Platform.OS} yet`));
|
|
|
|
}
|
|
|
|
return RNKeychainManager.setSharedWebCredentialsForServer(server, username, password);
|
|
|
|
}
|