This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 506
/
WebAuthenticator.android.cs
144 lines (121 loc) · 5.29 KB
/
WebAuthenticator.android.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Threading.Tasks;
using Android.Content;
#if __ANDROID_29__
using AndroidX.Browser.CustomTabs;
#else
using Android.Support.CustomTabs;
#endif
namespace Xamarin.Essentials
{
public partial class WebAuthenticator
{
static TaskCompletionSource<WebAuthenticatorResult> tcsResponse = null;
static Uri currentRedirectUri = null;
internal static bool OnResume(Intent intent)
{
// If we aren't waiting on a task, don't handle the url
if (tcsResponse?.Task?.IsCompleted ?? true)
return false;
if (intent?.Data == null)
{
tcsResponse.TrySetCanceled();
return false;
}
try
{
var intentUri = new Uri(intent.Data.ToString());
// Only handle schemes we expect
if (!WebUtils.CanHandleCallback(currentRedirectUri, intentUri))
{
tcsResponse.TrySetException(new InvalidOperationException($"Invalid Redirect URI, detected `{intentUri}` but expected a URI in the format of `{currentRedirectUri}`"));
return false;
}
tcsResponse?.TrySetResult(new WebAuthenticatorResult(intentUri));
return true;
}
catch (Exception ex)
{
tcsResponse.TrySetException(ex);
return false;
}
}
static async Task<WebAuthenticatorResult> PlatformAuthenticateAsync(WebAuthenticatorOptions webAuthenticatorOptions)
{
var url = webAuthenticatorOptions?.Url;
var callbackUrl = webAuthenticatorOptions?.CallbackUrl;
var packageName = Platform.AppContext.PackageName;
// Create an intent to see if the app developer wired up the callback activity correctly
var intent = new Intent(Intent.ActionView);
intent.AddCategory(Intent.CategoryBrowsable);
intent.AddCategory(Intent.CategoryDefault);
intent.SetPackage(packageName);
intent.SetData(global::Android.Net.Uri.Parse(callbackUrl.OriginalString));
// Try to find the activity for the callback intent
if (!Platform.IsIntentSupported(intent, packageName))
throw new InvalidOperationException($"You must subclass the `{nameof(WebAuthenticatorCallbackActivity)}` and create an IntentFilter for it which matches your `{nameof(callbackUrl)}`.");
// Cancel any previous task that's still pending
if (tcsResponse?.Task != null && !tcsResponse.Task.IsCompleted)
tcsResponse.TrySetCanceled();
tcsResponse = new TaskCompletionSource<WebAuthenticatorResult>();
currentRedirectUri = callbackUrl;
if (!(await StartCustomTabsActivity(url)))
{
// Fall back to opening the system-registered browser if necessary
var urlOriginalString = url.OriginalString;
var browserIntent = new Intent(Intent.ActionView, global::Android.Net.Uri.Parse(urlOriginalString));
Platform.CurrentActivity.StartActivity(browserIntent);
}
return await tcsResponse.Task;
}
static async Task<bool> StartCustomTabsActivity(Uri url)
{
// Is only set to true if BindServiceAsync succeeds and no exceptions are thrown
var success = false;
var parentActivity = Platform.GetCurrentActivity(true);
var customTabsActivityManager = CustomTabsActivityManager.From(parentActivity);
try
{
if (await BindServiceAsync(customTabsActivityManager))
{
var customTabsIntent = new CustomTabsIntent.Builder(customTabsActivityManager.Session)
.SetShowTitle(true)
.Build();
customTabsIntent.Intent.SetData(global::Android.Net.Uri.Parse(url.OriginalString));
if (customTabsIntent.Intent.ResolveActivity(parentActivity.PackageManager) != null)
{
WebAuthenticatorIntermediateActivity.StartActivity(parentActivity, customTabsIntent.Intent);
success = true;
}
}
}
finally
{
try
{
customTabsActivityManager.Client?.Dispose();
}
finally
{
}
}
return success;
}
static Task<bool> BindServiceAsync(CustomTabsActivityManager manager)
{
var tcs = new TaskCompletionSource<bool>();
manager.CustomTabsServiceConnected += OnCustomTabsServiceConnected;
if (!manager.BindService())
{
manager.CustomTabsServiceConnected -= OnCustomTabsServiceConnected;
tcs.TrySetResult(false);
}
return tcs.Task;
void OnCustomTabsServiceConnected(ComponentName name, CustomTabsClient client)
{
manager.CustomTabsServiceConnected -= OnCustomTabsServiceConnected;
tcs.TrySetResult(true);
}
}
}
}