Add getSupportedBiometryType

This commit is contained in:
Joel Arvidsson 2018-02-25 17:05:33 +01:00
parent 1497383434
commit aac2d3d8c4
3 changed files with 55 additions and 0 deletions

View File

@ -16,8 +16,15 @@ export default class KeychainExample extends Component {
username: '', username: '',
password: '', password: '',
status: '', status: '',
biometryType: null,
}; };
componentDidMount() {
Keychain.getSupportedBiometryType().then(biometryType => {
this.setState({ biometryType });
});
}
save() { save() {
Keychain.setGenericPassword(this.state.username, this.state.password) Keychain.setGenericPassword(this.state.username, this.state.password)
.then(() => { .then(() => {
@ -91,6 +98,11 @@ export default class KeychainExample extends Component {
{!!this.state.status && ( {!!this.state.status && (
<Text style={styles.status}>{this.state.status}</Text> <Text style={styles.status}>{this.state.status}</Text>
)} )}
{!!this.state.biometryType && (
<Text style={styles.biometryType}>
Supported biometry: {this.state.biometryType}
</Text>
)}
<View style={styles.buttons}> <View style={styles.buttons}>
<TouchableHighlight <TouchableHighlight
onPress={() => this.save()} onPress={() => this.save()}
@ -160,6 +172,11 @@ const styles = StyleSheet.create({
fontSize: 12, fontSize: 12,
marginTop: 15, marginTop: 15,
}, },
biometryType: {
color: '#333',
fontSize: 12,
marginTop: 15,
},
buttons: { buttons: {
flexDirection: 'row', flexDirection: 'row',
justifyContent: 'space-between', justifyContent: 'space-between',

View File

@ -178,6 +178,23 @@ RCT_EXPORT_METHOD(canCheckAuthentication:(NSDictionary *)options resolver:(RCTPr
} }
} }
RCT_EXPORT_METHOD(getSupportedBiometryType:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
NSError *aerr = nil;
LAContext *context = [LAContext new];
BOOL canBeProtected = [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&aerr];
if (!aerr && canBeProtected) {
if (@available(iOS 11, *)) {
if (context.biometryType == LABiometryTypeFaceID) {
return resolve(@"FaceID");
}
}
return resolve(@"TouchID");
}
return resolve([NSNull null]);
}
- (BOOL) canCheckAuthentication:(LAPolicy)policyToEvaluate error:(NSError **)err { - (BOOL) canCheckAuthentication:(LAPolicy)policyToEvaluate error:(NSError **)err {
return [[[ LAContext alloc] init ] canEvaluatePolicy:policyToEvaluate error:err ]; return [[[ LAContext alloc] init ] canEvaluatePolicy:policyToEvaluate error:err ];
} }

View File

@ -1,6 +1,11 @@
import { NativeModules, Platform } from 'react-native'; import { NativeModules, Platform } from 'react-native';
const { RNKeychainManager } = NativeModules; const { RNKeychainManager } = NativeModules;
export const BIOMETRY_TYPE = {
TOUCH_ID: 'TouchID',
FACE_ID: 'FaceID',
};
type SecAccessible = type SecAccessible =
| 'AccessibleWhenUnlocked' | 'AccessibleWhenUnlocked'
| 'AccessibleAfterFirstUnlock' | 'AccessibleAfterFirstUnlock'
@ -49,6 +54,22 @@ export function canImplyAuthentication(options?: SecureOptions): Promise {
return RNKeychainManager.canCheckAuthentication(options); return RNKeychainManager.canCheckAuthentication(options);
} }
/**
* Get what type of local authentication policy (LAPolicy) is supported
* on this device with the device settings the user chose.
* @return {Promise} Resolves to a `BIOMETRY_TYPE` when supported, otherwise `null`
*/
export function getSupportedBiometryType(): Promise {
if (Platform.OS !== 'ios') {
return Promise.reject(
new Error(
`getSupportedBiometryType() is not supported on ${Platform.OS} yet`
)
);
}
return RNKeychainManager.getSupportedBiometryType();
}
/** /**
* Saves the `username` and `password` combination for `service` securely - needs authentication to retrieve it. * Saves the `username` and `password` combination for `service` securely - needs authentication to retrieve it.
* @param {string} service Associated service. * @param {string} service Associated service.