Improve Authentication documentation

This commit is contained in:
Daniel Saidi 2021-11-03 11:16:01 +01:00
parent 08d7761955
commit 214069fa72
7 changed files with 40 additions and 37 deletions

Binary file not shown.

View File

@ -11,9 +11,9 @@ import Foundation
/** /**
This struct represents a unique authentication. This struct represents a unique authentication.
This struct currently only has an `id` but is still used to This struct currently only has an ``id``, but is still used
be able to extend the authentication info without having to to improve the authentication info without having to change
change any authentication protocols. any authentication protocols.
*/ */
public struct Authentication { public struct Authentication {
@ -27,9 +27,8 @@ public struct Authentication {
public extension Authentication { public extension Authentication {
/** /**
This a "standard" authentication. It can be used if you This standard authentication type can be used if you do
don't have a bunch of different authentication types in not have many different authentications in your app.
your app.
*/ */
static var standard: Authentication { static var standard: Authentication {
Authentication(id: "com.swiftkit.auth.any") Authentication(id: "com.swiftkit.auth.any")

View File

@ -9,8 +9,8 @@
import Foundation import Foundation
/** /**
This protocol can be implemented by authenticating services This protocol can be implemented by any classes that can be
that can be used to authenticate the current user. used to authenticate the user.
*/ */
public protocol AuthenticationService: AnyObject { public protocol AuthenticationService: AnyObject {
@ -26,8 +26,7 @@ public protocol AuthenticationService: AnyObject {
func authenticateUser(for auth: Authentication, reason: String, completion: @escaping AuthCompletion) func authenticateUser(for auth: Authentication, reason: String, completion: @escaping AuthCompletion)
/** /**
Check if the service instance can authenticate the user Check if the service instance can authenticate the user.
for a certain authentication type.
*/ */
func canAuthenticateUser(for auth: Authentication) -> Bool func canAuthenticateUser(for auth: Authentication) -> Bool
} }

View File

@ -9,10 +9,23 @@
import Foundation import Foundation
/** /**
This enum represents possible authentication service errors. This enum represents various authentication errors that can
occur while a user is being authenticated.
*/ */
public enum AuthenticationServiceError: Error, Equatable { public enum AuthenticationServiceError: Error, Equatable {
/**
The authentication failed.
*/
case authenticationFailed case authenticationFailed
/**
The authentication failed with a certain error message.
*/
case authenticationFailedWithErrorMessage(String)
/**
The requested authentication type is not supported.
*/
case unsupportedAuthentication case unsupportedAuthentication
} }

View File

@ -11,28 +11,18 @@ import LocalAuthentication
/** /**
This authentication service uses `LocalAuthentication` such This authentication service uses `LocalAuthentication` such
as `FaceID` or `TOuchID` to authenticate the user. as `FaceID` or `TouchID` to authenticate the user.
*/ */
@available(iOS 11.0, OSX 10.12.2, *)
public class BiometricAuthenticationService: AuthenticationService { public class BiometricAuthenticationService: AuthenticationService {
// MARK: - Initialization
public init() {} public init() {}
// MARK: - Properties
private let policy = LAPolicy.deviceOwnerAuthenticationWithBiometrics private let policy = LAPolicy.deviceOwnerAuthenticationWithBiometrics
// MARK: - Public functions
/** /**
Authenticate the user for a certain authentication type, Authenticate the user for a certain authentication type.
provided that the service can authenticate the user for
the provided authentication type.
`reason` can be used to display information to the user. `reason` can be used to display information to the user.
*/ */
@ -44,8 +34,11 @@ public class BiometricAuthenticationService: AuthenticationService {
} }
/** /**
Check if the service instance can authenticate the user Check if the service instance can authenticate the user.
for a certain authentication type.
For instance, a user can disable authentication for the
app, which means that the service can no longer fulfill
it's intended use.
*/ */
public func canAuthenticateUser(for auth: Authentication) -> Bool { public func canAuthenticateUser(for auth: Authentication) -> Bool {
var error: NSError? var error: NSError?

View File

@ -9,19 +9,19 @@
import Foundation import Foundation
/** /**
This protocol can be implemented by services that can cache This protocol can be implemented by any classes that can be
an authentication result, to avoid having to perform a real used to authenticate the user and cache the result to avoid
authentication operation if a successful authentication has having to perform a real authentication if a successful one
already been performed. has already been performed.
For instance, you can reduce the number of times users have For instance, you can reduce the number of times users have
to perform biometric authentication to access critical data. to perform biometric authentication to access critical data.
Note that you can't rely on a cached authentication service Note that you can't rely on a cached authentication service
to clear its cached state. Call `resetUserAuthentications()` to clear its state. Call the ``resetUserAuthentications()``
or `resetUserAuthentication(for:)` as soon as this state is or ``resetUserAuthentication(for:)`` function as soon as an
considered to be invalid, e.g. when your app is send to the authenticated session becomes invalid, e.g. when the app is
background and a new user can open the app at a later time. sent to the background or new users log in.
*/ */
public protocol CachedAuthenticationService: AuthenticationService { public protocol CachedAuthenticationService: AuthenticationService {

View File

@ -9,8 +9,8 @@
import Foundation import Foundation
/** /**
This service wraps another authentication service and keeps This class wraps another ``AuthenticationService`` instance
authentication results in a cache. and keeps authentication results in a cache.
*/ */
public class CachedAuthenticationServiceProxy: CachedAuthenticationService { public class CachedAuthenticationServiceProxy: CachedAuthenticationService {
@ -45,8 +45,7 @@ public class CachedAuthenticationServiceProxy: CachedAuthenticationService {
} }
/** /**
Check if the service instance can authenticate the user Check if the service instance can authenticate the user.
for a certain authentication type.
*/ */
public func canAuthenticateUser(for auth: Authentication) -> Bool { public func canAuthenticateUser(for auth: Authentication) -> Bool {
baseService.canAuthenticateUser(for: auth) baseService.canAuthenticateUser(for: auth)