Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ASWebAuthenticationSession Support #226

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions ios/ByteowlsCapacitorOauth2/Source/ByteowlsCapacitorOauth2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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: ""),
Copy link

@narcis-ro narcis-ro Mar 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be?

callbackUrlScheme: URL(string: redirectUrl)!.scheme!,

So it works with any host url? e.g ://auth

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it should be as you've written. I've not tested it with other host urls but it should with any url.

presentationContextProvider: self,
prefersEphemeralWebBrowserSession: false
);

oauthSwift.authorizeURLHandler = urlHandler
self.oauthSwift = oauthSwift

Expand Down Expand Up @@ -664,7 +676,5 @@ extension OAuth2ClientPlugin: ASAuthorizationControllerDelegate {
self.savedPluginCall?.reject(self.ERR_GENERAL)
}
}



}

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}