宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-04-06 a1ae6802080a22e6e6ce6d0935e95facb1daca5c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import Foundation
 
// MARK: - AccessTokenAuthorizable
 
/// A protocol for controlling the behavior of `AccessTokenPlugin`.
public protocol AccessTokenAuthorizable {
 
    /// Represents the authorization header to use for requests.
    var authorizationType: AuthorizationType? { get }
}
 
// MARK: - AuthorizationType
 
/// An enum representing the header to use with an `AccessTokenPlugin`
public enum AuthorizationType {
 
    /// The `"Basic"` header.
    case basic
 
    /// The `"Bearer"` header.
    case bearer
 
    /// Custom header implementation.
    case custom(String)
 
    public var value: String {
        switch self {
        case .basic: return "Basic"
        case .bearer: return "Bearer"
        case .custom(let customValue): return customValue
        }
    }
}
 
extension AuthorizationType: Equatable {
    public static func == (lhs: AuthorizationType, rhs: AuthorizationType) -> Bool {
        switch (lhs, rhs) {
        case (.basic, .basic),
             (.bearer, .bearer):
            return true
 
        case let (.custom(value1), .custom(value2)):
            return value1 == value2
 
        default:
            return false
        }
    }
}
 
// MARK: - AccessTokenPlugin
 
/**
 A plugin for adding basic or bearer-type authorization headers to requests. Example:
 
 ```
 Authorization: Basic <token>
 Authorization: Bearer <token>
 Authorization: <Сustom> <token>
 ```
 
 */
public struct AccessTokenPlugin: PluginType {
 
    public typealias TokenClosure = (AuthorizationType) -> String
 
    /// A closure returning the access token to be applied in the header.
    public let tokenClosure: TokenClosure
 
    /**
     Initialize a new `AccessTokenPlugin`.
 
     - parameters:
     - tokenClosure: A closure returning the token to be applied in the pattern `Authorization: <AuthorizationType> <token>`
     */
    public init(tokenClosure: @escaping TokenClosure) {
        self.tokenClosure = tokenClosure
    }
 
    /**
     Prepare a request by adding an authorization header if necessary.
 
     - parameters:
     - request: The request to modify.
     - target: The target of the request.
     - returns: The modified `URLRequest`.
     */
    public func prepare(_ request: URLRequest, target: TargetType) -> URLRequest {
 
        guard let authorizable = target as? AccessTokenAuthorizable,
            let authorizationType = authorizable.authorizationType
            else { return request }
 
        var request = request
 
        let authValue = authorizationType.value + " " + tokenClosure(authorizationType)
        request.addValue(authValue, forHTTPHeaderField: "Authorization")
 
        return request
    }
}