Add shared web credentials support (#40)
This commit is contained in:
parent
7280e924ef
commit
e6cc3aa9f4
14
README.md
14
README.md
|
@ -43,10 +43,10 @@ pod 'RNKeychain', :path => 'node_modules/react-native-keychain'
|
|||
See `KeychainExample` for fully working project example.
|
||||
|
||||
```js
|
||||
var Keychain = require('react-native-keychain');
|
||||
import * as Keychain from 'react-native-keychain';
|
||||
|
||||
var username = 'zuck';
|
||||
var password = 'poniesRgr8';
|
||||
let username = 'zuck';
|
||||
let password = 'poniesRgr8';
|
||||
|
||||
// Generic Password, service argument optional
|
||||
Keychain
|
||||
|
@ -91,6 +91,14 @@ Keychain
|
|||
console.log('Credentials successfully deleted');
|
||||
});
|
||||
|
||||
Keychain
|
||||
.requestSharedWebCredentials()
|
||||
.then(function(credentials) {
|
||||
if (credentials) {
|
||||
console.log('Shared web credentials successfully loaded for user ' + credentials.username);
|
||||
}
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
### Android
|
||||
|
|
|
@ -215,4 +215,48 @@ RCT_EXPORT_METHOD(resetInternetCredentialsForServer:(NSString*)server callback:(
|
|||
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(requestSharedWebCredentials:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
|
||||
{
|
||||
SecRequestSharedWebCredential(NULL, NULL, ^(CFArrayRef credentials, CFErrorRef error) {
|
||||
if (error != NULL) {
|
||||
NSError *nsError = (__bridge NSError *)error;
|
||||
return reject([NSString stringWithFormat:@"%li", (long)nsError.code], nsError.description, nil);
|
||||
}
|
||||
|
||||
if (CFArrayGetCount(credentials) > 0) {
|
||||
|
||||
CFDictionaryRef credentialDict = CFArrayGetValueAtIndex(credentials, 0);
|
||||
NSString *server = (__bridge NSString *)CFDictionaryGetValue(credentialDict, kSecAttrServer);
|
||||
NSString *username = (__bridge NSString *)CFDictionaryGetValue(credentialDict, kSecAttrAccount);
|
||||
NSString *password = (__bridge NSString *)CFDictionaryGetValue(credentialDict, kSecSharedPassword);
|
||||
|
||||
return resolve(@{
|
||||
@"server": server,
|
||||
@"username": username,
|
||||
@"password": password
|
||||
});
|
||||
}
|
||||
return resolve(@(NO));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
RCT_EXPORT_METHOD(setSharedWebCredentialsForServer:(NSString*)server withUsername:(NSString*)username withPassword:(NSString*)password resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
|
||||
{
|
||||
SecAddSharedWebCredential(
|
||||
(__bridge CFStringRef)server,
|
||||
(__bridge CFStringRef)username,
|
||||
(__bridge CFStringRef)password,
|
||||
^(CFErrorRef error)
|
||||
{
|
||||
|
||||
if (error != NULL) {
|
||||
NSError *nsError = (__bridge NSError *)error;
|
||||
return reject([NSString stringWithFormat:@"%li", (long)nsError.code], nsError.description, nil);
|
||||
}
|
||||
|
||||
resolve(@(YES));
|
||||
});
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
27
index.js
27
index.js
|
@ -155,3 +155,30 @@ export function resetGenericPassword(
|
|||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Asks the user for a shared web credential, resolves to `{ server, username, password }` if approved
|
||||
* `false` if denied and throws an error if not supported on platform or there's no shared credentials.
|
||||
* Returns a `Promise` object.
|
||||
*/
|
||||
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.
|
||||
* Returns a `Promise` object.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue