This repository has been archived by the owner on Dec 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWebAuthenticationBroker.cs
98 lines (85 loc) · 4.66 KB
/
WebAuthenticationBroker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="WebAuthenticationBroker.cs" company="In The Hand Ltd">
// Copyright (c) 2019 In The Hand Ltd, All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
namespace InTheHand.Security.Authentication.Web
{
/// <summary>
/// Starts the authentication operation. You can call the methods of this class multiple times in a single application or across multiple applications at the same time.
/// Replaces the UWP equivalent and uses the Edge browser for authentication with apps like GitHub which displays a warning running on Internet Explorer.
/// </summary>
public sealed class WebAuthenticationBroker
{
private static Uri redirectUri;
private static ContentDialog dialog;
private static string code = string.Empty;
private static uint errorCode = 0;
public static Task<WebAuthenticationResult> AuthenticateAsync(WebAuthenticationOptions options, Uri requestUri)
{
return AuthenticateAsync(options, requestUri, Windows.Security.Authentication.Web.WebAuthenticationBroker.GetCurrentApplicationCallbackUri());
}
public static async Task<WebAuthenticationResult> AuthenticateAsync(WebAuthenticationOptions options, Uri requestUri, Uri callbackUri)
{
if (options != WebAuthenticationOptions.None)
throw new ArgumentException("WebAuthenticationBroker currently only supports WebAuthenticationOptions.None", "options");
redirectUri = callbackUri;
dialog = new ContentDialog();
var grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition() { Height = Windows.UI.Xaml.GridLength.Auto });
grid.RowDefinitions.Add(new RowDefinition() { Height = new Windows.UI.Xaml.GridLength(1, Windows.UI.Xaml.GridUnitType.Star) });
var label = new TextBlock();
label.Text = "Connect to a service";
label.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left;
label.Margin = new Windows.UI.Xaml.Thickness(0);
grid.Children.Add(label);
var closeButton = new Button();
closeButton.Content = "";
closeButton.FontFamily = new FontFamily("Segoe UI Symbol");
closeButton.BorderBrush = new SolidColorBrush(Windows.UI.Color.FromArgb(0, 0, 0, 0));
closeButton.Background = new SolidColorBrush(Windows.UI.Color.FromArgb(0, 0, 0, 0));
closeButton.Margin = new Windows.UI.Xaml.Thickness(0);
closeButton.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Right;
closeButton.Click += (s,e) => { dialog.Hide(); };
grid.Children.Add(closeButton);
var webView = new WebView(WebViewExecutionMode.SameThread) { Source = requestUri };
webView.AllowFocusOnInteraction = true;
webView.SetValue(Grid.RowProperty, 1);
webView.NavigationStarting += WebView_NavigationStarting;
webView.NavigationFailed += WebView_NavigationFailed;
webView.MinWidth = 480;
webView.MinHeight = 600;
grid.Children.Add(webView);
dialog.Content = grid;
dialog.GotFocus += (s, e) => { webView.Focus(Windows.UI.Xaml.FocusState.Programmatic); };
var res = await dialog.ShowAsync();
return new WebAuthenticationResult(code, errorCode, errorCode > 0 ? WebAuthenticationStatus.ErrorHttp : string.IsNullOrEmpty(code) ? WebAuthenticationStatus.UserCancel : WebAuthenticationStatus.Success);
}
private static void WebView_NavigationFailed(object sender, WebViewNavigationFailedEventArgs e)
{
errorCode = (uint)e.WebErrorStatus;
dialog.Hide();
}
private static void WebView_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
{
if (args.Uri.ToString().StartsWith(redirectUri.ToString()))
{
var querySegs = args.Uri.Query.Substring(1).Split('&');
foreach (string seg in querySegs)
{
if (seg.StartsWith("code="))
{
code = args.Uri.ToString();
break;
}
}
args.Cancel = true;
dialog.Hide();
}
}
}
}