From 93da5aa20ed61b78f21efb96a0451de74ef37800 Mon Sep 17 00:00:00 2001 From: Martin Walsh Date: Tue, 7 Mar 2017 11:22:24 +0000 Subject: [PATCH 1/2] Added webAuth method to Authentication, spawn new WebAuth instance Added tests --- Auth0/Auth0Authentication.swift | 4 ++++ Auth0/Authentication.swift | 16 ++++++++++++++++ Auth0Tests/AuthenticationSpec.swift | 19 +++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/Auth0/Auth0Authentication.swift b/Auth0/Auth0Authentication.swift index b06bf021..f9f525ac 100644 --- a/Auth0/Auth0Authentication.swift +++ b/Auth0/Auth0Authentication.swift @@ -184,4 +184,8 @@ struct Auth0Authentication: Authentication { let delegation = URL(string: "/delegation", relativeTo: self.url)! return Request(session: session, url: delegation, method: "POST", handle: plainJson, payload: payload, logger: self.logger, telemetry: self.telemetry) } + + func webAuth(withConnection connection: String) -> WebAuth { + return SafariWebAuth(clientId: self.clientId, url: self.url, presenter: ControllerModalPresenter(), telemetry: self.telemetry).connection(connection) + } } diff --git a/Auth0/Authentication.swift b/Auth0/Authentication.swift index 3b4e898c..ebd14ea3 100644 --- a/Auth0/Authentication.swift +++ b/Auth0/Authentication.swift @@ -396,6 +396,22 @@ public protocol Authentication: Trackable, Loggable { - returns: a request that will yield the result of delegation */ func delegation(withParameters parameters: [String: Any]) -> Request<[String: Any], AuthenticationError> + + /** + Creates a new WebAuth compliant instance using authentication credentials + and telemetry. + + ``` + Auth0 + .authentication(clientId: clientId, domain: "samples.auth0.com") + .webAuth(withConnection: "facebook") + .start { print($0) } + ``` + + - parameter connection: name of the connection to use. + - returns: a newly created WebAuth compliant object. + */ + func webAuth(withConnection connection: String) -> WebAuth } /** diff --git a/Auth0Tests/AuthenticationSpec.swift b/Auth0Tests/AuthenticationSpec.swift index a6601819..6fa75e56 100644 --- a/Auth0Tests/AuthenticationSpec.swift +++ b/Auth0Tests/AuthenticationSpec.swift @@ -722,5 +722,24 @@ class AuthenticationSpec: QuickSpec { } + describe("spawn WebAuth instance") { + + it("should return a WebAuth instance with matching credentials") { + let webAuth = auth.webAuth(withConnection: "facebook") + expect(webAuth.clientId) == auth.clientId + expect(webAuth.url) == auth.url + } + + it("should return a WebAuth instance with matching telemetry") { + let webAuth = auth.webAuth(withConnection: "facebook") as! SafariWebAuth + expect(webAuth.telemetry.info) == auth.telemetry.info + } + + it("should return a WebAuth instance with matching connection") { + let webAuth = auth.webAuth(withConnection: "facebook") as! SafariWebAuth + expect(webAuth.parameters["connection"]) == "facebook" + } + } + } } From 4fb1f12fe202e2921d31c3c4ce95b25e1bb774da Mon Sep 17 00:00:00 2001 From: Hernan Zalazar Date: Mon, 13 Mar 2017 16:50:12 -0300 Subject: [PATCH 2/2] Enable logging if already enabled --- Auth0/Auth0Authentication.swift | 5 ++++- Auth0/Authentication.swift | 21 +++++++++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/Auth0/Auth0Authentication.swift b/Auth0/Auth0Authentication.swift index f9f525ac..f4fcbbf9 100644 --- a/Auth0/Auth0Authentication.swift +++ b/Auth0/Auth0Authentication.swift @@ -186,6 +186,9 @@ struct Auth0Authentication: Authentication { } func webAuth(withConnection connection: String) -> WebAuth { - return SafariWebAuth(clientId: self.clientId, url: self.url, presenter: ControllerModalPresenter(), telemetry: self.telemetry).connection(connection) + var safari = SafariWebAuth(clientId: self.clientId, url: self.url, presenter: ControllerModalPresenter(), telemetry: self.telemetry) + return safari + .logging(enabled: self.logger != nil) + .connection(connection) } } diff --git a/Auth0/Authentication.swift b/Auth0/Authentication.swift index ebd14ea3..7813834f 100644 --- a/Auth0/Authentication.swift +++ b/Auth0/Authentication.swift @@ -398,18 +398,27 @@ public protocol Authentication: Trackable, Loggable { func delegation(withParameters parameters: [String: Any]) -> Request<[String: Any], AuthenticationError> /** - Creates a new WebAuth compliant instance using authentication credentials - and telemetry. + Creates a new WebAuth request to authenticate using Safari browser and OAuth authorize flow. + With the connection name Auth0 will redirect to the associated IdP login page to authenticate + ``` Auth0 - .authentication(clientId: clientId, domain: "samples.auth0.com") - .webAuth(withConnection: "facebook") + .authentication(clientId: clientId, domain: "samples.auth0.com") + .webAuth(withConnection: "facebook") + .start { print($0) } + ``` + + If you need to show your Auth0 account login page just create the WebAuth object directly + + ``` + Auth0 + .webAuth(clientId: clientId, domain: "samples.auth0.com") .start { print($0) } ``` - - parameter connection: name of the connection to use. - - returns: a newly created WebAuth compliant object. + - parameter connection: name of the connection to use + - returns: a newly created WebAuth object. */ func webAuth(withConnection connection: String) -> WebAuth }