Code Monkey home page Code Monkey logo

localauthentication's Introduction

Local Authentication using biometric:

Using Face ID and Touch ID, the iOS app can authorize your identity and send the log in information to the back-end without any user interaction with the app. This is one of the most secure way to identify the user. Let's see how to implement it.

In the world of computer security, authentication falls into 3 categories:

(1). Something you know like user_id and password, PIN number etc. which are considered less secure.

(2). Something you have like it generates OTP (one time password), phone call to your personal mobile device etc.

(3). Something you are, this is most secure, which refers to a physical attribute of your body which is unique to the user. Like biometric information of retina scan, voice recognition, facial recognition, finger-print etc.

Architecture of local authentication:

local_auth_architecture

The local authentication framework:

Biometric authentication for ios app is implemented using local authentication framework: LAContext

Flow 1: User enters the user_name and password first time

Flow 2: On success of log-in, the app will save the password to key-chain and user-name into user-defaults to use it later

Flow 3: While next time onwards, the user comes to LogIn page, the app retrieves user-name from user-defaults and password from key-chain and check whether the app can evaluate the authentication using biometric.

Flow 4: If the device supports Touch ID and Face ID, it will call logIn Service with stored user_name and password after successfully validating the user biometric.

Face ID:

face_id

Touch ID:

touch_id

authenticate:

  func authenticateUser(successBlock: (() -> ())?, failureBlock: (()->())?) {
    
    // retrieve the user name
    guard let lastAccessedUserName = UserDefaults.standard.object(forKey: "lastAccessedUserName") as? String else { return }
    
    let context = LAContext()
    var error: NSError?
    
    if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
        // device can be used for biometric authentication
        // evalutate the policy
        context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Access requires authentication") { (success, err) in
            DispatchQueue.main.async { [weak self] in
                if success {
                    switch context.biometryType {
                    case .faceID, .touchID:
                        print("device support faceId or touchId authentication")
                        self?.loadPasswordFromKeychainAndAuthenticateUser(lastAccessedUserName, successBlock: successBlock, failureBlock: failureBlock)
                    default: break
                    }
                }
            }
        }
    } else { // device cannot be used for biometric authentication
        if let error = error {
            switch error.code {
            case LAError.Code.biometryNotEnrolled.rawValue: print("biometry is not enrolled")
            case LAError.Code.biometryLockout.rawValue: print("the phone is not passcode protected")
            case LAError.Code.biometryNotAvailable.rawValue: print("biometry is not availabled")
            default: break
            }
        }
    }
}

How to simulate Face ID in simulator:

Target should be iPhone X and upper version which supports Face ID.

1. add privacy key into info.plist for face id:

screen shot 2018-11-14 at 9 41 39 am

2. Select simulator, hardware and choose face id and select enroll:

screen shot 2018-11-14 at 6 52 31 pm

3. While Face id view comes in simulator front, use "matching face":

screen shot 2018-11-14 at 6 53 57 pm

How to simulate Touch ID in simulator:

Target should support Touch ID like iPhone 8, 8 plus etc.

1. selector simulator, hardware and choose Touch ID and select enroll:

screen shot 2018-11-14 at 6 58 04 pm

2. select matching touch while Touch ID view will appear.

simulator screen shot - iphone 8 - 2018-11-14 at 19 00 04

Helping documents:

https://www.techotopia.com/index.php/Implementing_TouchID_Authentication_in_iOS_8_Apps

https://developer.apple.com/documentation/security/keychain_services

localauthentication's People

Contributors

ashislaha avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.