Skip to content

Commit

Permalink
[iOS][Android] Add LocalAnthentication#supportedAuntehnticationTypes …
Browse files Browse the repository at this point in the history
…method (#2450)
  • Loading branch information
bbarthec authored and tsapeta committed Oct 23, 2018
1 parent 129d6fc commit 16cda43
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
19 changes: 19 additions & 0 deletions apps/native-component-list/screens/LocalAuthenticationScreen.js
Expand Up @@ -39,6 +39,21 @@ export default class LocalAuthenticationScreen extends React.Component {
}
};

checkAuthenticationsTypes = async () => {
const result = await LocalAuthentication.supportedAuthenticationTypesAsync();
const stringResult = result.map(type => {
switch (type) {
case LocalAuthentication.AuthenticationType.FINGERPRINT:
return 'FINGERPRINT';
case LocalAuthentication.AuthenticationType.FACIAL_RECOGNITION:
return 'FACIAL_RECOGNITION';
default:
throw new Error(`Unrecognised authentication type returned: '${type}'`);
}
}).join(', ');
alert(stringResult ? `Available types: ${stringResult}` : 'No available authentication types!');
};

render() {
const { hasHardware, isEnrolled } = this.state;

Expand All @@ -58,6 +73,10 @@ export default class LocalAuthenticationScreen extends React.Component {
onPress={this.authenticate}
title={this.state.waiting ? 'Waiting for authentication... ' : 'Authenticate'}
/>
<Button
onPress={this.checkAuthenticationsTypes}
title="Check aunthentications types available on the device"
/>
</View>
);
}
Expand Down
Expand Up @@ -8,6 +8,9 @@
import android.support.v4.hardware.fingerprint.FingerprintManagerCompat;
import android.support.v4.os.CancellationSignal;

import java.util.ArrayList;
import java.util.List;

import expo.core.ExportedModule;
import expo.core.ModuleRegistry;
import expo.core.Promise;
Expand All @@ -22,6 +25,8 @@ public class LocalAuthenticationModule extends ExportedModule implements ModuleR
private boolean mIsAuthenticating = false;
private UIManager mUIManager;

private static final int AUTHENTICATION_TYPE_FINGERPRINT = 1;

private final FingerprintManagerCompat.AuthenticationCallback mAuthenticationCallback =
new FingerprintManagerCompat.AuthenticationCallback() {
@Override
Expand Down Expand Up @@ -84,6 +89,16 @@ public void setModuleRegistry(ModuleRegistry moduleRegistry) {
mUIManager = moduleRegistry.getModule(UIManager.class);
}

@ExpoMethod
public void supportedAuthenticationTypesAsync(final Promise promise) {
boolean hasHardware = mFingerprintManager.isHardwareDetected();
List<Integer> results = new ArrayList<>();
if (hasHardware) {
results.add(AUTHENTICATION_TYPE_FINGERPRINT);
}
promise.resolve(results);
}

@ExpoMethod
public void hasHardwareAsync(final Promise promise) {
boolean hasHardware = mFingerprintManager.isHardwareDetected();
Expand Down
Expand Up @@ -6,6 +6,11 @@
#import <EXConstantsInterface/EXConstantsInterface.h>
#import <EXLocalAuthentication/EXLocalAuthentication.h>

typedef NS_ENUM(NSInteger, EXAuthenticationType) {
EXAuthenticationTypeFingerprint = 1,
EXAuthenticationTypeFacialRecognition = 2,
};

@interface EXLocalAuthentication ()

@property (nonatomic, weak) EXModuleRegistry *moduleRegistry;
Expand All @@ -21,6 +26,21 @@ - (void)setModuleRegistry:(EXModuleRegistry *)moduleRegistry
_moduleRegistry = moduleRegistry;
}


EX_EXPORT_METHOD_AS(supportedAuthenticationTypesAsync,
supportedAuthenticationTypesAsync:(EXPromiseResolveBlock)resolve
reject:(EXPromiseRejectBlock)reject)
{
NSMutableArray *results = [NSMutableArray array];
if (EXIsTouchIDDevice()) {
[results addObject:@(EXAuthenticationTypeFingerprint)];
}
if (EXIsFaceIDDevice()) {
[results addObject:@(EXAuthenticationTypeFacialRecognition)];
}
resolve(results);
}

EX_EXPORT_METHOD_AS(hasHardwareAsync,
hasHardwareAsync:(EXPromiseResolveBlock)resolve
reject:(EXPromiseRejectBlock)reject)
Expand Down Expand Up @@ -137,4 +157,18 @@ static BOOL EXIsFaceIDDevice()
return isFaceIDDevice;
}

static BOOL EXIsTouchIDDevice()
{
static BOOL isTouchIDDevice = NO;
static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{
LAContext *context = [LAContext new];
[context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil];
isTouchIDDevice = context.biometryType == LABiometryTypeTouchID;
});

return isTouchIDDevice;
}

@end
11 changes: 11 additions & 0 deletions packages/expo-local-authentication/src/LocalAuthentication.js
Expand Up @@ -8,10 +8,21 @@ const { ExpoLocalAuthentication: LocalAuthentication } = NativeModulesProxy;

type LocalAuthenticationResult = { success: true } | { success: false, error: string };

export const AuthenticationType = {
FINGERPRINT: 1,
FACIAL_RECOGNITION: 2,
};

type AuthenticationTypeType = $Keys<typeof AuthenticationType>;

export async function hasHardwareAsync(): Promise<boolean> {
return LocalAuthentication.hasHardwareAsync();
}

export async function supportedAuthenticationTypesAsync(): Promise<Array<AuthenticationTypeType>> {
return await LocalAuthentication.supportedAuthenticationTypesAsync();
}

export async function isEnrolledAsync(): Promise<boolean> {
return LocalAuthentication.isEnrolledAsync();
}
Expand Down

0 comments on commit 16cda43

Please sign in to comment.