예제 #1
0
        static async Task <WebAuthenticatorResult> PlatformAuthenticateAsync(Options options)
        {
            if (DeviceInfo.Version.Major < 13)
            {
                throw new FeatureNotSupportedException();
            }

            var provider = new ASAuthorizationAppleIdProvider();
            var req      = provider.CreateRequest();

            authManager = new AuthManager(Platform.GetCurrentWindow());

            var scopes = new List <ASAuthorizationScope>();

            if (options.IncludeFullNameScope)
            {
                scopes.Add(ASAuthorizationScope.FullName);
            }
            if (options.IncludeEmailScope)
            {
                scopes.Add(ASAuthorizationScope.Email);
            }

            req.RequestedScopes = scopes.ToArray();
            var controller = new ASAuthorizationController(new[] { req });

            controller.Delegate = authManager;
            controller.PresentationContextProvider = authManager;

            controller.PerformRequests();

            var creds = await authManager.GetCredentialsAsync();

            if (creds == null)
            {
                return(null);
            }

            var idToken  = new NSString(creds.IdentityToken, NSStringEncoding.UTF8).ToString();
            var authCode = new NSString(creds.AuthorizationCode, NSStringEncoding.UTF8).ToString();
            var name     = NSPersonNameComponentsFormatter.GetLocalizedString(creds.FullName, NSPersonNameComponentsFormatterStyle.Default, 0);

            var appleAccount = new WebAuthenticatorResult();

            appleAccount.Properties.Add("id_token", idToken);
            appleAccount.Properties.Add("authorization_code", authCode);
            appleAccount.Properties.Add("state", creds.State);
            appleAccount.Properties.Add("email", creds.Email);
            appleAccount.Properties.Add("user_id", creds.User);
            appleAccount.Properties.Add("name", name);
            appleAccount.Properties.Add("realuserstatus", creds.RealUserStatus.ToString());

            return(appleAccount);
        }
예제 #2
0
        static Task PlatformShowRequestAsync(ShareRequestBase request, List <NSObject> items)
        {
            var window = Platform.GetCurrentWindow();
            var view   = window.ContentView;

            var rect = request.PresentationSourceBounds.ToPlatformRectangle();

            rect.Y = view.Bounds.Height - rect.Bottom;

            var picker = new NSSharingServicePicker(items.ToArray());

            picker.ShowRelativeToRect(rect, view, NSRectEdge.MinYEdge);

            return(Task.CompletedTask);
        }
        internal static async Task <WebAuthenticatorResult> PlatformAuthenticateAsync(Uri url, Uri callbackUrl)
        {
            // Cancel any previous task that's still pending
            if (tcsResponse?.Task != null && !tcsResponse.Task.IsCompleted)
            {
                tcsResponse.TrySetCanceled();
            }

            tcsResponse = new TaskCompletionSource <WebAuthenticatorResult>();
            redirectUri = callbackUrl;

            try
            {
                var scheme = redirectUri.Scheme;

                if (!VerifyHasUrlSchemeOrDoesntRequire(scheme))
                {
                    tcsResponse.TrySetException(new InvalidOperationException("You must register your URL Scheme handler in your app's Info.plist!"));
                    return(await tcsResponse.Task);
                }

#if __IOS__
                void AuthSessionCallback(NSUrl cbUrl, NSError error)
                {
                    if (error == null)
                    {
                        OpenUrl(cbUrl);
                    }
                    else
                    {
                        tcsResponse.TrySetException(new NSErrorException(error));
                    }
                }

                if (UIDevice.CurrentDevice.CheckSystemVersion(12, 0))
                {
                    was = new ASWebAuthenticationSession(new NSUrl(url.OriginalString), scheme, AuthSessionCallback);

                    if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
                    {
                        var ctx = new ContextProvider(Platform.GetCurrentWindow());
                        void_objc_msgSend_IntPtr(was.Handle, ObjCRuntime.Selector.GetHandle("setPresentationContextProvider:"), ctx.Handle);
                    }

                    using (was)
                    {
                        was.Start();
                        return(await tcsResponse.Task);
                    }
                }

                if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
                {
                    sf = new SFAuthenticationSession(new NSUrl(url.OriginalString), scheme, AuthSessionCallback);
                    using (sf)
                    {
                        sf.Start();
                        return(await tcsResponse.Task);
                    }
                }

                // THis is only on iOS9+ but we only support 10+ in Essentials anyway
                var controller = new SFSafariViewController(new NSUrl(url.OriginalString), false)
                {
                    Delegate = new NativeSFSafariViewControllerDelegate
                    {
                        DidFinishHandler = (svc) =>
                        {
                            // Cancel our task if it wasn't already marked as completed
                            if (!(tcsResponse?.Task?.IsCompleted ?? true))
                            {
                                tcsResponse.TrySetException(new OperationCanceledException());
                            }
                        }
                    },
                };

                currentViewController = controller;
                await Platform.GetCurrentUIViewController().PresentViewControllerAsync(controller, true);

                return(await tcsResponse.Task);
#else
                var opened = UIApplication.SharedApplication.OpenUrl(url);
                if (!opened)
                {
                    tcsResponse.TrySetException(new Exception("Error opening Safari"));
                }
#endif
            }
            catch (Exception ex)
            {
                tcsResponse.TrySetException(ex);
            }

            return(await tcsResponse.Task);
        }