Skip to content

Commit 3423ff6

Browse files
Merge pull request #15 from AppcentMobile/feature/auth-header_improvements
🎷 [FEATURE] Auth header improvements.
2 parents a6dc495 + 5a2942c commit 3423ff6

File tree

4 files changed

+72
-6
lines changed

4 files changed

+72
-6
lines changed

Sources/ACMNetworking/Library/Extensions/ACMNetworking+Extensions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extension ACMNetworking {
2525
ACMBaseLogger.info(info)
2626
}
2727

28-
if let authHeader = endpoint.authHeader, authHeader.count > 0 {
28+
if let authHeader = endpoint.authHeader {
2929
let info = ACMStringUtils.shared.merge(list: [
3030
ACMNetworkConstants.httpAuthHeadersMessage,
3131
"\(authHeader)",

Sources/ACMNetworking/Library/Manager/ACMBaseEndpoint.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public struct ACMBaseEndpoint {
5757

5858
// MARK: Auth header
5959

60-
var authHeader: String?
60+
var authHeader: ACMAuthModel?
6161

6262
// MARK: Retry count
6363

@@ -79,7 +79,7 @@ public struct ACMBaseEndpoint {
7979
urlRequest.httpMethod = method.rawValue
8080

8181
if let header = authHeader {
82-
urlRequest.setValue(header, forHTTPHeaderField: ACMNetworkConstants.headerAuthorization)
82+
urlRequest.setValue(header.rawHeader, forHTTPHeaderField: ACMNetworkConstants.headerAuthorization)
8383
}
8484

8585
if let basicHeaders = headers {
@@ -110,7 +110,7 @@ public struct ACMBaseEndpoint {
110110
return URLSession(configuration: configuration, delegate: delegate, delegateQueue: OperationQueue.main)
111111
}
112112

113-
init(config: ACMPlistModel? = nil, configOverride: Bool, host: String? = nil, scheme: ACMBaseScheme, path: String = "", queryItems: [URLQueryItem]? = nil, params: [String: Any?]? = nil, headers: NSMutableDictionary? = nil, method: ACMBaseMethod, authHeader: String? = nil, mediaData: NSMutableData? = nil, retryCount: Int? = nil) {
113+
init(config: ACMPlistModel? = nil, configOverride: Bool, host: String? = nil, scheme: ACMBaseScheme, path: String = "", queryItems: [URLQueryItem]? = nil, params: [String: Any?]? = nil, headers: NSMutableDictionary? = nil, method: ACMBaseMethod, authHeader: ACMAuthModel? = nil, mediaData: NSMutableData? = nil, retryCount: Int? = nil) {
114114
if let config = config {
115115
self.config = config
116116
} else {

Sources/ACMNetworking/Library/Manager/ACMEndpoint.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public final class ACMEndpoint {
2525
var headers: NSMutableDictionary?
2626
var headersModel: [ACMHeaderModel]?
2727
var method: ACMBaseMethod = .get
28-
var authHeader: String?
28+
var authHeader: ACMAuthModel?
2929
var mediaData: NSMutableData?
3030
var retryCount: Int?
3131

@@ -154,7 +154,7 @@ public final class ACMEndpoint {
154154
///
155155
/// - Returns
156156
/// - Self
157-
public func add(authHeader: String) -> Self {
157+
public func add(authHeader: ACMAuthModel) -> Self {
158158
self.authHeader = authHeader
159159
return self
160160
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//
2+
// ACMAuthModel.swift
3+
//
4+
//
5+
// Created by burak on 1.04.2023.
6+
//
7+
8+
/// ACMAuthModel
9+
///
10+
/// A struct that holds the body payload
11+
public struct ACMAuthModel {
12+
var type: ACMAuthType
13+
var value: Any
14+
15+
var rawHeader: String {
16+
switch type {
17+
case .basic:
18+
guard let model = value as? ACMBasicAuthModel else { return "" }
19+
let header = String(format: "%@:%@", model.user, model.password)
20+
guard let data = header.data(using: String.Encoding.utf8) else { return "" }
21+
return String(format: "Basic %@", data.base64EncodedString())
22+
case .bearer:
23+
guard let header = value as? String else { return "" }
24+
return String(format: "Bearer %@", header)
25+
default:
26+
guard let header = value as? String else { return "" }
27+
return header
28+
}
29+
}
30+
31+
/// Public Init function
32+
/// For creating auth header
33+
/// - Parameters:
34+
/// - key: Body payload key
35+
/// - value: Body payload value
36+
/// - Returns
37+
/// - Void
38+
public init(type: ACMAuthType, value: Any) {
39+
self.type = type
40+
self.value = value
41+
}
42+
}
43+
44+
/// ACMAuthType
45+
///
46+
/// A struct that holds the authorization types
47+
public enum ACMAuthType {
48+
case basic
49+
case bearer
50+
case raw
51+
}
52+
53+
/// ACMBasicAuthModel
54+
///
55+
/// A struct that holds the authorization payload
56+
public struct ACMBasicAuthModel {
57+
var user: String
58+
var password: String
59+
}
60+
61+
/// ACMBearerAuthModel
62+
///
63+
/// A struct that holds the authorization payload
64+
public struct ACMBearerAuthModel {
65+
var bearer: String
66+
}

0 commit comments

Comments
 (0)