|
| 1 | +#import <Cocoa/Cocoa.h> |
| 2 | +#import <AuthenticationServices/AuthenticationServices.h> |
| 3 | + |
| 4 | +#include "../iOS/Common.h" |
| 5 | + |
| 6 | +typedef void (*ASWebAuthenticationSessionCompletionCallback)(void* sessionPtr, const char* callbackUrl, int errorCode, const char* errorMessage); |
| 7 | + |
| 8 | +@interface Thirdweb_ASWebAuthenticationSession : NSObject<ASWebAuthenticationPresentationContextProviding> |
| 9 | + |
| 10 | +@property (readonly, nonatomic) ASWebAuthenticationSession* session; |
| 11 | + |
| 12 | +@end |
| 13 | + |
| 14 | +@implementation Thirdweb_ASWebAuthenticationSession |
| 15 | + |
| 16 | +- (instancetype)initWithURL:(NSURL *)URL callbackURLScheme:(nullable NSString *)callbackURLScheme completionCallback:(ASWebAuthenticationSessionCompletionCallback)completionCallback |
| 17 | +{ |
| 18 | + self = [super init]; |
| 19 | + if (self) |
| 20 | + { |
| 21 | + _session = [[ASWebAuthenticationSession alloc] initWithURL:URL |
| 22 | + callbackURLScheme:callbackURLScheme |
| 23 | + completionHandler:^(NSURL * _Nullable callbackURL, NSError * _Nullable error) |
| 24 | + { |
| 25 | + if (error != nil) |
| 26 | + { |
| 27 | + NSLog(@"[ASWebAuthenticationSession:CompletionHandler] %@", error.description); |
| 28 | + } |
| 29 | + |
| 30 | + completionCallback((__bridge void*)self, toString(callbackURL.absoluteString), (int)error.code, toString(error.localizedDescription)); |
| 31 | + }]; |
| 32 | + |
| 33 | + if (@available(macOS 10.15, *)) |
| 34 | + { |
| 35 | + _session.presentationContextProvider = self; |
| 36 | + } |
| 37 | + } |
| 38 | + return self; |
| 39 | +} |
| 40 | + |
| 41 | +- (ASPresentationAnchor)presentationAnchorForWebAuthenticationSession:(ASWebAuthenticationSession *)session |
| 42 | +{ |
| 43 | + if (@available(macOS 10.15, *)) |
| 44 | + { |
| 45 | + NSWindow* anchor = [NSApplication sharedApplication].keyWindow; |
| 46 | + if (anchor == nil) |
| 47 | + { |
| 48 | + anchor = [NSApplication sharedApplication].mainWindow; |
| 49 | + } |
| 50 | + if (anchor == nil) |
| 51 | + { |
| 52 | + anchor = [NSApplication sharedApplication].windows.firstObject; |
| 53 | + } |
| 54 | + return anchor; |
| 55 | + } |
| 56 | + return nil; |
| 57 | +} |
| 58 | + |
| 59 | +@end |
| 60 | + |
| 61 | +extern "C" |
| 62 | +{ |
| 63 | + Thirdweb_ASWebAuthenticationSession* Thirdweb_ASWebAuthenticationSession_InitWithURL( |
| 64 | + const char* urlStr, const char* urlSchemeStr, ASWebAuthenticationSessionCompletionCallback completionCallback) |
| 65 | + { |
| 66 | + NSURL* url = [NSURL URLWithString: toString(urlStr)]; |
| 67 | + NSString* urlScheme = toString(urlSchemeStr); |
| 68 | + |
| 69 | + Thirdweb_ASWebAuthenticationSession* session = [[Thirdweb_ASWebAuthenticationSession alloc] initWithURL:url |
| 70 | + callbackURLScheme:urlScheme |
| 71 | + completionCallback:completionCallback]; |
| 72 | + return session; |
| 73 | + } |
| 74 | + |
| 75 | + int Thirdweb_ASWebAuthenticationSession_Start(void* sessionPtr) |
| 76 | + { |
| 77 | + Thirdweb_ASWebAuthenticationSession* session = (__bridge Thirdweb_ASWebAuthenticationSession*) sessionPtr; |
| 78 | + BOOL started = [[session session] start]; |
| 79 | + return toBool(started); |
| 80 | + } |
| 81 | + |
| 82 | + void Thirdweb_ASWebAuthenticationSession_Cancel(void* sessionPtr) |
| 83 | + { |
| 84 | + Thirdweb_ASWebAuthenticationSession* session = (__bridge Thirdweb_ASWebAuthenticationSession*) sessionPtr; |
| 85 | + [[session session] cancel]; |
| 86 | + } |
| 87 | + |
| 88 | + int Thirdweb_ASWebAuthenticationSession_GetPrefersEphemeralWebBrowserSession(void* sessionPtr) |
| 89 | + { |
| 90 | + Thirdweb_ASWebAuthenticationSession* session = (__bridge Thirdweb_ASWebAuthenticationSession*) sessionPtr; |
| 91 | + if (@available(macOS 10.15, *)) |
| 92 | + { |
| 93 | + return toBool([[session session] prefersEphemeralWebBrowserSession]); |
| 94 | + } |
| 95 | + return 0; |
| 96 | + } |
| 97 | + |
| 98 | + void Thirdweb_ASWebAuthenticationSession_SetPrefersEphemeralWebBrowserSession(void* sessionPtr, int enable) |
| 99 | + { |
| 100 | + Thirdweb_ASWebAuthenticationSession* session = (__bridge Thirdweb_ASWebAuthenticationSession*) sessionPtr; |
| 101 | + if (@available(macOS 10.15, *)) |
| 102 | + { |
| 103 | + [[session session] setPrefersEphemeralWebBrowserSession:toBool(enable)]; |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments