-
Notifications
You must be signed in to change notification settings - Fork 87
Add macOS OAuth browser support and platform integration #241
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| #import <Cocoa/Cocoa.h> | ||
| #import <AuthenticationServices/AuthenticationServices.h> | ||
|
|
||
| #include "../iOS/Common.h" | ||
|
|
||
| typedef void (*ASWebAuthenticationSessionCompletionCallback)(void* sessionPtr, const char* callbackUrl, int errorCode, const char* errorMessage); | ||
|
|
||
| @interface Thirdweb_ASWebAuthenticationSession : NSObject<ASWebAuthenticationPresentationContextProviding> | ||
|
|
||
| @property (readonly, nonatomic) ASWebAuthenticationSession* session; | ||
|
|
||
| @end | ||
|
|
||
| @implementation Thirdweb_ASWebAuthenticationSession | ||
|
|
||
| - (instancetype)initWithURL:(NSURL *)URL callbackURLScheme:(nullable NSString *)callbackURLScheme completionCallback:(ASWebAuthenticationSessionCompletionCallback)completionCallback | ||
| { | ||
| self = [super init]; | ||
| if (self) | ||
| { | ||
| _session = [[ASWebAuthenticationSession alloc] initWithURL:URL | ||
| callbackURLScheme:callbackURLScheme | ||
| completionHandler:^(NSURL * _Nullable callbackURL, NSError * _Nullable error) | ||
| { | ||
| if (error != nil) | ||
| { | ||
| NSLog(@"[ASWebAuthenticationSession:CompletionHandler] %@", error.description); | ||
| } | ||
|
|
||
| completionCallback((__bridge void*)self, toString(callbackURL.absoluteString), (int)error.code, toString(error.localizedDescription)); | ||
| }]; | ||
|
|
||
| if (@available(macOS 10.15, *)) | ||
| { | ||
| _session.presentationContextProvider = self; | ||
| } | ||
| } | ||
| return self; | ||
| } | ||
|
|
||
| - (ASPresentationAnchor)presentationAnchorForWebAuthenticationSession:(ASWebAuthenticationSession *)session | ||
| { | ||
| if (@available(macOS 10.15, *)) | ||
| { | ||
| NSWindow* anchor = [NSApplication sharedApplication].keyWindow; | ||
| if (anchor == nil) | ||
| { | ||
| anchor = [NSApplication sharedApplication].mainWindow; | ||
| } | ||
| if (anchor == nil) | ||
| { | ||
| anchor = [NSApplication sharedApplication].windows.firstObject; | ||
| } | ||
| return anchor; | ||
| } | ||
| return nil; | ||
| } | ||
|
|
||
| @end | ||
|
|
||
| extern "C" | ||
| { | ||
| Thirdweb_ASWebAuthenticationSession* Thirdweb_ASWebAuthenticationSession_InitWithURL( | ||
| const char* urlStr, const char* urlSchemeStr, ASWebAuthenticationSessionCompletionCallback completionCallback) | ||
| { | ||
| NSURL* url = [NSURL URLWithString: toString(urlStr)]; | ||
| NSString* urlScheme = toString(urlSchemeStr); | ||
|
|
||
| Thirdweb_ASWebAuthenticationSession* session = [[Thirdweb_ASWebAuthenticationSession alloc] initWithURL:url | ||
| callbackURLScheme:urlScheme | ||
| completionCallback:completionCallback]; | ||
| return session; | ||
| } | ||
|
|
||
| int Thirdweb_ASWebAuthenticationSession_Start(void* sessionPtr) | ||
| { | ||
| Thirdweb_ASWebAuthenticationSession* session = (__bridge Thirdweb_ASWebAuthenticationSession*) sessionPtr; | ||
| BOOL started = [[session session] start]; | ||
| return toBool(started); | ||
| } | ||
|
|
||
| void Thirdweb_ASWebAuthenticationSession_Cancel(void* sessionPtr) | ||
| { | ||
| Thirdweb_ASWebAuthenticationSession* session = (__bridge Thirdweb_ASWebAuthenticationSession*) sessionPtr; | ||
| [[session session] cancel]; | ||
| } | ||
|
|
||
| int Thirdweb_ASWebAuthenticationSession_GetPrefersEphemeralWebBrowserSession(void* sessionPtr) | ||
| { | ||
| Thirdweb_ASWebAuthenticationSession* session = (__bridge Thirdweb_ASWebAuthenticationSession*) sessionPtr; | ||
| if (@available(macOS 10.15, *)) | ||
| { | ||
| return toBool([[session session] prefersEphemeralWebBrowserSession]); | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| void Thirdweb_ASWebAuthenticationSession_SetPrefersEphemeralWebBrowserSession(void* sessionPtr, int enable) | ||
| { | ||
| Thirdweb_ASWebAuthenticationSession* session = (__bridge Thirdweb_ASWebAuthenticationSession*) sessionPtr; | ||
| if (@available(macOS 10.15, *)) | ||
| { | ||
| [[session session] setPrefersEphemeralWebBrowserSession:toBool(enable)]; | ||
| } | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # macOS OAuth Plugin | ||
|
|
||
| The native bridge in this folder (`libMacBrowser.dylib`) is a universal binary (arm64 + x86_64) built from `MacBrowser.mm`. | ||
|
|
||
| ## Rebuild steps | ||
|
|
||
| Run these commands from the repository root on macOS: | ||
|
|
||
| ```bash | ||
| cd Assets/Thirdweb/Plugins/macOS | ||
| clang++ -std=c++17 -ObjC++ -fmodules -Wall -Werror -arch arm64 -framework Cocoa -framework AuthenticationServices -dynamiclib MacBrowser.mm -o /tmp/libMacBrowser_arm64.dylib | ||
| clang++ -std=c++17 -ObjC++ -fmodules -Wall -Werror -arch x86_64 -framework Cocoa -framework AuthenticationServices -dynamiclib MacBrowser.mm -o /tmp/libMacBrowser_x86_64.dylib | ||
| lipo -create /tmp/libMacBrowser_arm64.dylib /tmp/libMacBrowser_x86_64.dylib -output libMacBrowser.dylib | ||
| rm /tmp/libMacBrowser_arm64.dylib /tmp/libMacBrowser_x86_64.dylib | ||
| ``` | ||
|
|
||
| After rebuilding, re-run your Unity macOS build to make sure the new binary is picked up. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.