Skip to content

Commit

Permalink
Remove Alamofire dependency on auth request
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfej94 committed Nov 12, 2021
1 parent c583fbd commit 68d0768
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 68 deletions.
2 changes: 1 addition & 1 deletion PassportKit.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |spec|

spec.name = "PassportKit"
spec.version = "1.6.1"
spec.version = "1.6.2"
spec.license = "MIT"
spec.summary = "Swift library used for quick and easy oauth authentication."
spec.homepage = "https://github.com/appoly/PassportKit"
Expand Down
50 changes: 16 additions & 34 deletions Sources/Network/PassportAuthAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@


import Foundation
import Alamofire



Expand All @@ -28,52 +27,35 @@ enum PassportAuthAPI {
}
}

var parameters: Parameters? {
var parameters: Data? {
switch self {
case .login(let configuration, let model):
switch configuration.mode {
case .sanctum:
return [
"email": model.email!,
"password": model.password!
]
return "username=\(model.email!)&password=\(model.password!)"
.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)?
.data(using: .utf8)
case .standard(let clientID, let clientSecret):
return [
"username" : model.email!,
"password" : model.password!,
"client_id" : clientID,
"client_secret" : clientSecret,
"grant_type" : "password"
]
return "username=\(model.email!)&password=\(model.password!)&client_id=\(clientID)&client_secret=\(clientSecret)&grant_type=password"
.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)?
.data(using: .utf8)
}
case .refresh(let configuration, let token):
guard case .standard(let clientID, let clientSecret) = configuration.mode else {
return nil
}
case .refresh(let configuration, let token):
guard case .standard(let clientID, let clientSecret) = configuration.mode else {
return nil
}

return [
"grant_type": "refresh_token",
"refresh_token": token,
"client_id": clientID,
"client_secret": clientSecret,
"scope": ""
]
return "refresh_token=\(token)&client_id=\(clientID)&client_secret=\(clientSecret)&grant_type=refresh_token"
.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)?
.data(using: .utf8)
}
}

var method: HTTPMethod {
var method: String {
switch self {
case .login, .refresh:
return .post
return "POST"
}
}

var headers: HTTPHeaders? {
return nil
}

var encoding: ParameterEncoding {
return URLEncoding.default
}

}
107 changes: 74 additions & 33 deletions Sources/Network/PassportAuthService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,44 +48,85 @@ class PassportKitAuthService {


private func authRequest(configuration: PassportConfiguration, api: PassportAuthAPI, completion: @escaping (Error?) -> Void) {
Session.default.request(api.url, method: api.method, parameters: api.parameters, encoding: api.encoding, headers: api.headers)
.validate(statusCode:
PassportKitHTTPStatusCode.ok.rawValue..<PassportKitHTTPStatusCode.multipleChoices.rawValue)
.responseData { (response) in
switch response.result {
case .failure(_):
switch response.response?.statusCode {
var request = URLRequest(url: api.url)
request.httpMethod = api.method
request.httpBody = api.parameters
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let response = response as? HTTPURLResponse else { completion(PassportKitNetworkError.invalidResponse); return }
guard let data = data, error == nil else { completion(error); return }
guard (PassportKitHTTPStatusCode.ok.rawValue..<PassportKitHTTPStatusCode.multipleChoices.rawValue).contains(response.statusCode) else {
switch response.statusCode {
case 400:
completion(PassportKitNetworkError.messageError(message: NSLocalizedString("Email address or password was incorrect, please try again.", comment: "")))
default:
completion(PassportKitNetworkError.unknown)
}
return
}

let manager = PassportKitAuthenticationManager(configuration.keychainID)

switch configuration.mode {
case .sanctum:
guard let response = try? JSONDecoder().decode(SanctumAuthResponse.self, from: data) else {
completion(PassportKitNetworkError.invalidResponse)
return
}
case .success(let data):
let manager = PassportKitAuthenticationManager(configuration.keychainID)

switch configuration.mode {
case .sanctum:
guard let response = try? JSONDecoder().decode(SanctumAuthResponse.self, from: data) else {
completion(PassportKitNetworkError.invalidResponse)
return
}

manager.setAuthToken(response.token)
case .standard:
guard let response = try? JSONDecoder().decode(PassportAuthResponse.self, from: data) else {
completion(PassportKitNetworkError.invalidResponse)
return
}

manager.setAuthToken(response.accessToken)
manager.setRefreshToken(response.refreshToken)


manager.setAuthToken(response.token)
case .standard:
guard let response = try? JSONDecoder().decode(PassportAuthResponse.self, from: data) else {
completion(PassportKitNetworkError.invalidResponse)
return
}

completion(nil)
}

manager.setAuthToken(response.accessToken)
manager.setRefreshToken(response.refreshToken)

}

completion(nil)
}
task.resume()
}
// AF.request(api.url, method: api.method, parameters: api.parameters, encoding: api.encoding, headers: api.headers)
// .validate(statusCode:
// PassportKitHTTPStatusCode.ok.rawValue..<PassportKitHTTPStatusCode.multipleChoices.rawValue)
// .responseData { (response) in
// switch response.result {
// case .failure(_):
// switch response.response?.statusCode {
// case 400:
// completion(PassportKitNetworkError.messageError(message: NSLocalizedString("Email address or password was incorrect, please try again.", comment: "")))
// default:
// completion(PassportKitNetworkError.unknown)
// }
// case .success(let data):
// let manager = PassportKitAuthenticationManager(configuration.keychainID)
//
// switch configuration.mode {
// case .sanctum:
// guard let response = try? JSONDecoder().decode(SanctumAuthResponse.self, from: data) else {
// completion(PassportKitNetworkError.invalidResponse)
// return
// }
//
// manager.setAuthToken(response.token)
// case .standard:
// guard let response = try? JSONDecoder().decode(PassportAuthResponse.self, from: data) else {
// completion(PassportKitNetworkError.invalidResponse)
// return
// }
//
// manager.setAuthToken(response.accessToken)
// manager.setRefreshToken(response.refreshToken)
//
// }
//
// completion(nil)
// }
// }
// }



Expand All @@ -96,8 +137,8 @@ class PassportKitAuthService {
}


private func error(from response: DataResponse<Any, Error>) -> PassportKitNetworkError? {
return nil
}
// private func error(from response: DataResponse<Any, Error>) -> PassportKitNetworkError? {
// return nil
// }

}

0 comments on commit 68d0768

Please sign in to comment.