From 8809bcadf4b23b47231c98ecbcce80d35b3b00b3 Mon Sep 17 00:00:00 2001 From: Nicola Sampaolesi Date: Tue, 17 Jan 2023 10:16:52 +0100 Subject: [PATCH 1/4] Switched to ASWebAuthenticationURLHandler --- .../Source/ByteowlsCapacitorOauth2.swift | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/ios/ByteowlsCapacitorOauth2/Source/ByteowlsCapacitorOauth2.swift b/ios/ByteowlsCapacitorOauth2/Source/ByteowlsCapacitorOauth2.swift index 04737292..021c1173 100644 --- a/ios/ByteowlsCapacitorOauth2/Source/ByteowlsCapacitorOauth2.swift +++ b/ios/ByteowlsCapacitorOauth2/Source/ByteowlsCapacitorOauth2.swift @@ -7,8 +7,7 @@ import AuthenticationServices typealias JSObject = [String:Any] @objc(OAuth2ClientPlugin) -public class OAuth2ClientPlugin: CAPPlugin { - +public class OAuth2ClientPlugin: CAPPlugin, ASWebAuthenticationPresentationContextProviding { var savedPluginCall: CAPPluginCall? let JSON_KEY_ACCESS_TOKEN = "access_token" @@ -62,6 +61,16 @@ public class OAuth2ClientPlugin: CAPPlugin { var oauth2SafariDelegate: OAuth2SafariDelegate? var handlerClasses = [String: OAuth2CustomHandler.Type]() var handlerInstances = [String: OAuth2CustomHandler]() + + public func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor { + var view: ASPresentationAnchor? + + DispatchQueue.main.sync { + view = self.bridge?.webView?.window + } + + return view ?? ASPresentationAnchor() + } func registerHandlers() { let classCount = objc_getClassList(nil, 0) @@ -275,9 +284,12 @@ public class OAuth2ClientPlugin: CAPPlugin { ) } - let urlHandler = SafariURLHandler(viewController: (bridge?.viewController)!, oauthSwift: oauthSwift) - // if the user touches "done" in safari without entering the credentials the USER_CANCELLED error is sent #71 - urlHandler.delegate = self.oauth2SafariDelegate + let urlHandler = ASWebAuthenticationURLHandler( + callbackUrlScheme: redirectUrl.replacingOccurrences(of: "://oauth", with: ""), + presentationContextProvider: self, + prefersEphemeralWebBrowserSession: false + ); + oauthSwift.authorizeURLHandler = urlHandler self.oauthSwift = oauthSwift @@ -411,10 +423,10 @@ public class OAuth2ClientPlugin: CAPPlugin { self.log("Returned to JS:\n\(jsonObj)") } call.resolve(jsonObj) - } catch { + } /* catch { self.log("Invalid json in response \(error.localizedDescription)") call.reject(self.ERR_GENERAL) - } + } */ } else { // `parameters` will be response parameters var result = parameters @@ -659,12 +671,13 @@ extension OAuth2ClientPlugin: ASAuthorizationControllerDelegate { case .failed: self.log("SIWA: Error.failed") self.savedPluginCall?.reject(self.ERR_AUTHORIZATION_FAILED) + case .notInteractive: + self.log("SIWA: Error.notInteractive") + self.savedPluginCall?.reject(self.ERR_AUTHORIZATION_FAILED) @unknown default: self.log("SIWA: Error.default") self.savedPluginCall?.reject(self.ERR_GENERAL) } } - - - } + From 20b5dcb23fcd0369a13bda0314327f3f28a6aad0 Mon Sep 17 00:00:00 2001 From: Nicola Sampaolesi Date: Tue, 17 Jan 2023 10:27:27 +0100 Subject: [PATCH 2/4] Revert useless edits --- .../Source/ByteowlsCapacitorOauth2.swift | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/ios/ByteowlsCapacitorOauth2/Source/ByteowlsCapacitorOauth2.swift b/ios/ByteowlsCapacitorOauth2/Source/ByteowlsCapacitorOauth2.swift index 021c1173..b21d70e6 100644 --- a/ios/ByteowlsCapacitorOauth2/Source/ByteowlsCapacitorOauth2.swift +++ b/ios/ByteowlsCapacitorOauth2/Source/ByteowlsCapacitorOauth2.swift @@ -423,10 +423,10 @@ public class OAuth2ClientPlugin: CAPPlugin, ASWebAuthenticationPresentationConte self.log("Returned to JS:\n\(jsonObj)") } call.resolve(jsonObj) - } /* catch { + } catch { self.log("Invalid json in response \(error.localizedDescription)") call.reject(self.ERR_GENERAL) - } */ + } } else { // `parameters` will be response parameters var result = parameters @@ -671,9 +671,6 @@ extension OAuth2ClientPlugin: ASAuthorizationControllerDelegate { case .failed: self.log("SIWA: Error.failed") self.savedPluginCall?.reject(self.ERR_AUTHORIZATION_FAILED) - case .notInteractive: - self.log("SIWA: Error.notInteractive") - self.savedPluginCall?.reject(self.ERR_AUTHORIZATION_FAILED) @unknown default: self.log("SIWA: Error.default") self.savedPluginCall?.reject(self.ERR_GENERAL) From 7acca20d8604891091b69bda74758cdeff174599 Mon Sep 17 00:00:00 2001 From: Nicola Sampaolesi Date: Tue, 17 Jan 2023 13:00:24 +0100 Subject: [PATCH 3/4] Build sources after installation from git repo --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 00fdf203..2b12625b 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ "test": "jest", "removePacked": "rm -f capacitor-oauth2-*.tgz", "publishLocally": "npm run removePacked && npm run build && npm pack", - "prepublishOnly": "npm run build" + "prepublishOnly": "npm run build", + "postinstall": "npm run build" }, "files": [ "android/src/main/", From 425ebe4e550c4be65a4f76af0acbeffe64c6a858 Mon Sep 17 00:00:00 2001 From: Nicola Sampaolesi Date: Tue, 17 Jan 2023 13:57:40 +0100 Subject: [PATCH 4/4] Including other files in build --- tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 7e3baacc..0b1eede1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -16,5 +16,5 @@ "strictPropertyInitialization": false, "target": "es2017" }, - "files": ["src/index.ts"] + "files": ["src/index.ts", "src/web.ts", "src/web-utils.ts", "src/definitions.ts"] }