Skip to content

Commit

Permalink
Rename auth transaction classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Widcket committed Nov 2, 2021
1 parent 5a99246 commit 1e764fa
Show file tree
Hide file tree
Showing 15 changed files with 79 additions and 184 deletions.
8 changes: 4 additions & 4 deletions Auth0.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ web_auth_files = [
'Auth0/ObjectiveC/A0SHA.h',
'Auth0/ObjectiveC/A0SHA.m',
'Auth0/Array+Encode.swift',
'Auth0/ASCallbackTransaction.swift',
'Auth0/ASTransaction.swift',
'Auth0/AuthCancelable.swift',
'Auth0/AuthProvider.swift',
'Auth0/AuthSession.swift',
'Auth0/AuthTransaction.swift',
'Auth0/AuthenticationServicesSession.swift',
'Auth0/AuthenticationServicesSessionCallback.swift',
'Auth0/Auth0WebAuth.swift',
'Auth0/BaseCallbackTransaction.swift',
'Auth0/BaseTransaction.swift',
'Auth0/BioAuthentication.swift',
'Auth0/ClaimValidators.swift',
'Auth0/IDTokenSignatureValidator.swift',
Expand All @@ -25,8 +27,6 @@ web_auth_files = [
'Auth0/NSURLComponents+OAuth2.swift',
'Auth0/OAuth2Grant.swift',
'Auth0/ResponseType.swift',
'Auth0/SessionCallbackTransaction.swift',
'Auth0/SessionTransaction.swift',
'Auth0/TransactionStore.swift',
'Auth0/WebAuth.swift',
'Auth0/WebAuthError.swift',
Expand Down
66 changes: 30 additions & 36 deletions Auth0.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// AuthenticationServicesSessionCallback.swift
// ASCallbackTransaction.swift
//
// Copyright (c) 2020 Auth0 (http://auth0.com)
//
Expand All @@ -23,7 +23,7 @@
#if WEB_AUTH_PLATFORM
import AuthenticationServices

final class AuthenticationServicesSessionCallback: SessionCallbackTransaction {
final class ASCallbackTransaction: BaseCallbackTransaction {

init(url: URL, schemeURL: URL, callback: @escaping (Bool) -> Void) {
super.init(callback: callback)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// AuthenticationServicesSession.swift
// ASTransaction.swift
//
// Copyright (c) 2020 Auth0 (http://auth0.com)
//
Expand All @@ -23,7 +23,7 @@
#if WEB_AUTH_PLATFORM
import AuthenticationServices

final class AuthenticationServicesSession: SessionTransaction {
final class ASTransaction: BaseTransaction {

init(authorizeURL: URL,
redirectURL: URL,
Expand Down
4 changes: 2 additions & 2 deletions Auth0/Auth0WebAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ final class Auth0WebAuth: WebAuth {
state: state,
organization: organization,
invitation: invitation)
let session = AuthenticationServicesSession(authorizeURL: authorizeURL,
let session = ASTransaction(authorizeURL: authorizeURL,
redirectURL: redirectURL,
state: state,
handler: handler,
Expand All @@ -207,7 +207,7 @@ final class Auth0WebAuth: WebAuth {
return callback(false)
}

let session = AuthenticationServicesSessionCallback(url: logoutURL,
let session = ASCallbackTransaction(url: logoutURL,
schemeURL: redirectURL,
callback: callback)
self.storage.store(session)
Expand Down
35 changes: 0 additions & 35 deletions Auth0/AuthCancelable.swift

This file was deleted.

15 changes: 14 additions & 1 deletion Auth0/AuthTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,23 @@ When that happens the OS will call a method in your `AppDelegate` and that is wh

- important: Only one AuthTransaction can be active at a given time for Auth0.swift, if you start a new one before finishing the current one it will be cancelled.
*/
public protocol AuthTransaction: AuthResumable, AuthCancelable {
public protocol AuthTransaction {

/// value of the OAuth 2.0 state parameter. It must be a cryptographically secure random string used to protect the app with request forgery.
var state: String? { get }

/**
Resumes the transaction when the third party application notifies the application using a url with a custom scheme.

- parameter url: the url sent by the third party application that contains the result of the Auth
- returns: if the url was expected and properly formatted otherwise it will return `false`.
*/
func resume(_ url: URL) -> Bool

/**
Terminates the operation and reports back that it was cancelled.
*/
func cancel()

}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#if WEB_AUTH_PLATFORM
import Foundation

class SessionCallbackTransaction: NSObject, AuthTransaction {
class BaseCallbackTransaction: NSObject, AuthTransaction {

var authSession: AuthSession?
var state: String?
Expand All @@ -37,7 +37,7 @@ class SessionCallbackTransaction: NSObject, AuthTransaction {
self.callback(false)
}

func handleUrl(_ url: URL) -> Bool {
func resume(_ url: URL) -> Bool {
self.callback(true)
return true
}
Expand Down
10 changes: 5 additions & 5 deletions Auth0/SessionTransaction.swift → Auth0/BaseTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
#if WEB_AUTH_PLATFORM
import Foundation

class SessionTransaction: NSObject, AuthTransaction {
class BaseTransaction: NSObject, AuthTransaction {

typealias FinishTransaction = (Result<Credentials>) -> Void

var authSession: AuthSession?
let state: String?
let redirectURL: URL
Expand All @@ -53,17 +53,17 @@ class SessionTransaction: NSObject, AuthTransaction {
authSession = nil
}

func handleUrl(_ url: URL) -> Bool {
func resume(_ url: URL) -> Bool {
self.logger?.trace(url: url, source: "iOS Safari")
if self.resume(url) {
if self.handleURL(url) {
authSession?.cancel()
authSession = nil
return true
}
return false
}

private func resume(_ url: URL) -> Bool {
private func handleURL(_ url: URL) -> Bool {
guard url.absoluteString.lowercased().hasPrefix(self.redirectURL.absoluteString.lowercased()) else { return false }
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else {
self.callback(.failure(AuthenticationError(string: url.absoluteString, statusCode: 200)))
Expand Down
28 changes: 2 additions & 26 deletions Auth0/DesktopWebAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,40 +52,16 @@ public extension _ObjectiveOAuth2 {

}

public protocol AuthResumable {

/**
Resumes the transaction when the third party application notifies the application using a url with a custom scheme.
This method should be called from the Application's `AppDelegate` or by using the `public func resumeAuth(_ urls: [URL])` method.

- parameter url: the url sent by the third party application that contains the result of the Auth

- returns: if the url was expected and properly formatted otherwise it will return `false`.
- warning: deprecated as the SDK will not support macOS versions older than Catalina
*/
@available(*, deprecated, message: "the SDK will not support macOS versions older than Catalina")
func resume(_ url: URL) -> Bool

}

extension AuthTransaction where Self: SessionCallbackTransaction {

func resume(_ url: URL) -> Bool {
return self.handleUrl(url)
}

}

#if swift(>=5.1)
extension AuthenticationServicesSession: ASWebAuthenticationPresentationContextProviding {
extension ASTransaction: ASWebAuthenticationPresentationContextProviding {

func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
return NSApplication.shared()?.windows.filter({ $0.isKeyWindow }).last ?? ASPresentationAnchor()
}

}

extension AuthenticationServicesSessionCallback: ASWebAuthenticationPresentationContextProviding {
extension ASCallbackTransaction: ASWebAuthenticationPresentationContextProviding {

func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
return NSApplication.shared()?.windows.filter({ $0.isKeyWindow }).last ?? ASPresentationAnchor()
Expand Down
45 changes: 3 additions & 42 deletions Auth0/MobileWebAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,53 +56,14 @@ public extension _ObjectiveOAuth2 {
*/
@objc(resumeAuthWithURL:options:)
static func resume(_ url: URL, options: [A0URLOptionsKey: Any]) -> Bool {
return resumeAuth(url, options: options)
}

}

public protocol AuthResumable {

/**
Resumes the transaction when the third party application notifies the application using a url with a custom scheme.
This method should be called from the Application's `AppDelegate` or using `public func resumeAuth(_ url: URL, options: [UIApplicationOpenURLOptionsKey: Any]) -> Bool` method.

- parameter url: the url send by the third party application that contains the result of the Auth
- parameter options: options received in the openUrl method of the `AppDelegate`

- returns: if the url was expected and properly formatted otherwise it will return `false`.
*/
func resume(_ url: URL, options: [A0URLOptionsKey: Any]) -> Bool

}

public extension AuthResumable {

/**
Tries to resume (and complete) the OAuth2 session from the received URL

- parameter url: url received in application's AppDelegate
- parameter options: a dictionary of launch options received from application's AppDelegate

- returns: `true` if the url completed (successfully or not) this session, `false` otherwise
*/
func resume(_ url: URL, options: [A0URLOptionsKey: Any] = [:]) -> Bool {
return self.resume(url, options: options)
}

}

extension AuthTransaction where Self: SessionCallbackTransaction {

func resume(_ url: URL, options: [A0URLOptionsKey: Any]) -> Bool {
return self.handleUrl(url)
return resumeAuth(url)
}

}

#if swift(>=5.1)
@available(iOS 13.0, *)
extension AuthenticationServicesSession: ASWebAuthenticationPresentationContextProviding {
extension ASTransaction: ASWebAuthenticationPresentationContextProviding {

func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
return UIApplication.shared()?.windows.filter({ $0.isKeyWindow }).last ?? ASPresentationAnchor()
Expand All @@ -111,7 +72,7 @@ extension AuthenticationServicesSession: ASWebAuthenticationPresentationContextP
}

@available(iOS 13.0, *)
extension AuthenticationServicesSessionCallback: ASWebAuthenticationPresentationContextProviding {
extension ASCallbackTransaction: ASWebAuthenticationPresentationContextProviding {

func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
return UIApplication.shared()?.windows.filter({ $0.isKeyWindow }).last ?? ASPresentationAnchor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ private let Issuer = "\(Domain.absoluteString)/"
private let Leeway = 60 * 1000
private let RedirectURL = URL(string: "https://samples.auth0.com/callback")!

class SessionTransactionSpec: QuickSpec {
class BaseTransactionSpec: QuickSpec {

override func spec() {
var transaction: SessionTransaction!
var transaction: BaseTransaction!
var result: Result<Credentials>? = nil
let callback: (Result<Credentials>) -> () = { result = $0 }
let authentication = Auth0Authentication(clientId: ClientId, url: Domain)
Expand All @@ -55,11 +55,11 @@ class SessionTransactionSpec: QuickSpec {
let code = "123456"

beforeEach {
transaction = SessionTransaction(redirectURL: RedirectURL,
state: "state",
handler: handler,
logger: nil,
callback: callback)
transaction = BaseTransaction(redirectURL: RedirectURL,
state: "state",
handler: handler,
logger: nil,
callback: callback)
result = nil
stub(condition: isToken(Domain.host!) && hasAtLeast(["code": code,
"code_verifier": generator.verifier,
Expand All @@ -78,19 +78,19 @@ class SessionTransactionSpec: QuickSpec {
context("handle url") {
it("should handle url") {
let url = URL(string: "https://samples.auth0.com/callback?code=\(code)&state=state")!
expect(transaction.handleUrl(url)) == true
expect(transaction.resume(url)) == true
expect(result).toEventually(haveCredentials())
}

it("should handle url with error") {
let url = URL(string: "https://samples.auth0.com/callback?error=error&error_description=description&state=state")!
expect(transaction.handleUrl(url)) == true
expect(transaction.resume(url)) == true
expect(result).toEventually(haveAuthenticationError(code: "error", description: "description"))
}

it("should fail to handle url without state") {
let url = URL(string: "https://samples.auth0.com/callback?code=\(code)")!
expect(transaction.handleUrl(url)) == false
expect(transaction.resume(url)) == false
}
}

Expand Down
8 changes: 0 additions & 8 deletions Auth0Tests/NativeAuthSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,11 @@ class MockNativeAuthTransaction: NativeAuthTransaction {
self.delayed = { _ in }
}

#if os(iOS)
func resume(_ url: URL, options: [A0URLOptionsKey : Any]) -> Bool {
self.delayed(self.onNativeAuth())
self.delayed = { _ in }
return true
}
#else
func resume(_ url: URL) -> Bool {
self.delayed(self.onNativeAuth())
self.delayed = { _ in }
return true
}
#endif

/// Test Hooks
var onNativeAuth: () -> Result<NativeAuthCredentials> = {
Expand Down
6 changes: 0 additions & 6 deletions Auth0Tests/TransactionStoreSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,8 @@ class MockSession: AuthTransaction {
self.cancelled = true
}

#if os(iOS)
func resume(_ url: URL, options: [A0URLOptionsKey : Any]) -> Bool {
return self.resumeResult
}
#else
func resume(_ url: URL) -> Bool {
return self.resumeResult
}
#endif

}
Loading

0 comments on commit 1e764fa

Please sign in to comment.