Cotter's iOS SDK helps you easily add a Biometric prompt or PIN fallback to your app. This is useful for protecting transactions or sensitive information like medical records.
Step 2. Set allowed Authentication Methods in the Developer Dashboard
You need to set allowed methods for authentication your users. To allow PIN and BIOMETRIC, go to https://dev.cotter.app/rules
Remember to select the correct project in the dropdown list
Step 3. Initializing Cotter
You will have to do import Cotter on the file that will use Cotter. Then do initialization as follows
importCotter...let cotter = Cotter( apiSecretKey:<your-api-secret-key>, apiKeyID:<your-api-key-id>, cotterURL:"https://www.cotter.app/api/v0", userID:<your-user-id>, // user’s id that will be created later configuration:<your-cotter-config>)
/* https://www.cotter.app/api/v0/user/create */CotterAPIService.shared.registerUser( userID:<your-user-id>, cb: { response in// handle Result (Swift 5 enum) callback here })
Response:
{"ID":"9449e9e9-00e0-4d6b-a4b6-28c5b22b0b0f","created_at":"2020-01-21T12:40:21.200727668Z","update_at":"2020-01-21T12:40:21.200727668Z","deleted_at": null,"issuer":"<your key ID>","client_user_id":"<Your User Identifier (string)>","enrolled": [],"default_method": null}
Example:
// registerUserCb is a simple callback that handles the cases for // the API CallfuncregisterUserCb(_response: CotterResult<CotterUser>){switch response{case .success(let user):print("successfully registered the \(user)")case .failure(let err):// you can put exhaustive error handling hereswitch err{case CotterAPIError.decoding:print("this is decoding error on registering user")breakcase CotterAPIError.network:print("this is network error on registering user")breakcase CotterAPIError.status:print("this is not successful error")breakdefault:print("error registering user: \(err)") } }}// call the API using our clientCotterAPIService.shared.registerUser( userID:"hello@example.com", cb: registerUserCb)
2. Get a User
/* https://www.cotter.app/api/v0/user/:your_user_id */CotterAPIService.shared.getUser( userID:<your-user-id>, cb: { response in// handle Result (Swift 5 enum) callback here })
Response:
{"ID":"9449e9e9-00e0-4d6b-a4b6-28c5b22b0b0f","created_at":"2020-01-21T12:40:21.200727668Z","update_at":"2020-01-21T12:40:21.200727668Z","deleted_at": null,"issuer":"<your key ID>","client_user_id":"<Your User Identifier (string)>","enrolled": ["PIN", "BIOMETRIC"],"default_method":"BIOMETRIC"}
Example:
funcenrollCb(response: CotterResult<CotterUser>) {switch response {case .success(let resp): self.yourLabel.text= resp.enrolled.joined(separator:", ")case .failure(let err):// we can handle multiple error results hereswitch err {case CotterAPIError.status(code:500):print("internal server error")case CotterAPIError.status(code:404):print("user not found")default:print(err.localizedDescription) } }}CotterAPIService.shared.getUser(userID:self.userID, cb:enrollCb)
Step 5. Enroll PIN or Biometric
For starting authentication flow, you need to have the following:
The UIViewController you want to attach Cotter to
The UIViewController MUST have a NavigationController set (i.e. self.navigationController cannot be nil)
The verification flow will automatically prompt for Biometric Verification if the user's device has an enrolled biometric. Otherwise, it will fallback to entering PIN. Starting the verification flow is exactly the same as starting the Enrollment Flow on step 5.
// initializationlet cotter =Cotter(...)cotter.PinEnrollment.startTransaction( vc:<your-vc>, animated:true, cb:<your-callback>, hideClose:false, // hideClose param gives you the choice to show/hide the close button configuration: [:]);
HTTP Callbacks are the ones that you pass in to the start flow functions (startEnrollment, startTransaction, startUpdateProfile).
The callback form is as such:
// FinalCallbackAuth is the general callback function declarationpublictypealiasFinalAuthCallback= (_ token: String, _ error: Error?) ->Void
It takes in a token String and an optional Error object. Currently token String is not used in PIN or Biometric authentication. We're working on making the token useable.
Error types
For the FinalAuthCallback function, there are 3 types of CotterError that exist in the SDK. These include:
1. CotterError.biometricEnrollment
This error is produced when you fail to enroll your biometrics during the Enrollment flow in startEnrollment.
2. CotterError.biometricVerification
This error is produced when you fail to verify your biometrics during the Transaction flow in startTransaction.
3. CotterError.keychainError
This error is produced in all flows when the user's device is unable to attain its corresponding public/private keys.
However, you will almost always only encounter CotterError.biometricEnrollment and CotterError.biometricVerification errors in the resulting FinalAuthCallback function.
2. Authentication Callbacks
Authentication Callbacks are the ones that is passed in through the CotterAPIService. It uses the latest Result enum in Swift 5. The Authentication callback works similarly as the HTTP Callback, it can handle success and error cases as shown on the previous example.
What this definition says is that it takes a Result of a type Value, which Value can be any of the pre defined Cotter classes (such as CotterUser, CotterEvent, etc), and returns nothing.
Error types
When using the CotterAPIService class to call Cotter's API endpoints, there are 5 types of Cotter errors that you might encounter:
1. CotterAPIError.encoding
An 'encoding' error refers to an error encoding the HTTP Request.
2. CotterAPIError.decoding
A 'decoding' error refers to an error decoding the HTTP Response.
3. CotterAPIError.status(code: Int)
A 'status' error refers to the response having an invalid HTTP Status Code - e.g. Status 400 (Bad Request Error)
4. CotterAPIError.server(message: String)
A 'server' error means that our API server has responded to the request with an error message.
5. CotterAPIError.network
A 'network' error means that network conditions are poor, which can be due to bad internet connection.