From 68d0768f90f7ef8fbe32902967b88ed431525061 Mon Sep 17 00:00:00 2001 From: James Wolfe <56296015+wolfej94@users.noreply.github.com> Date: Fri, 12 Nov 2021 11:28:14 +0000 Subject: [PATCH] Remove Alamofire dependency on auth request --- PassportKit.podspec | 2 +- Sources/Network/PassportAuthAPI.swift | 50 ++++------ Sources/Network/PassportAuthService.swift | 107 +++++++++++++++------- 3 files changed, 91 insertions(+), 68 deletions(-) diff --git a/PassportKit.podspec b/PassportKit.podspec index c7df788..dddfcf5 100644 --- a/PassportKit.podspec +++ b/PassportKit.podspec @@ -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" diff --git a/Sources/Network/PassportAuthAPI.swift b/Sources/Network/PassportAuthAPI.swift index f73a3ad..a30f302 100644 --- a/Sources/Network/PassportAuthAPI.swift +++ b/Sources/Network/PassportAuthAPI.swift @@ -8,7 +8,6 @@ import Foundation -import Alamofire @@ -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 - } - } diff --git a/Sources/Network/PassportAuthService.swift b/Sources/Network/PassportAuthService.swift index fb6b160..66fdd8e 100644 --- a/Sources/Network/PassportAuthService.swift +++ b/Sources/Network/PassportAuthService.swift @@ -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..) -> PassportKitNetworkError? { - return nil - } +// private func error(from response: DataResponse) -> PassportKitNetworkError? { +// return nil +// } }