├── Certificates
└── Android
│ ├── README.txt
│ ├── XamarinGoogleAuth.keystore
│ └── XamarinGoogleAuth.keyInfo
├── Xamarin_GoogleAuth.iOS
├── Resources
│ ├── ITunesArtwork
│ ├── ITunesArtwork@2x
│ ├── Images.xcassets
│ │ └── AppIcons.appiconset
│ │ │ ├── Icon-76.png
│ │ │ ├── Icon-60@2x.png
│ │ │ ├── Icon-60@3x.png
│ │ │ ├── Icon-76@2x.png
│ │ │ ├── Icon-Small.png
│ │ │ ├── Icon-Small-40.png
│ │ │ ├── Icon-Small@2x.png
│ │ │ ├── Icon-Small@3x.png
│ │ │ ├── Icon-Small-40@2x.png
│ │ │ ├── Icon-Small-40@3x.png
│ │ │ └── Contents.json
│ └── LaunchScreen.storyboard
├── Entitlements.plist
├── Configuration.cs
├── Main.cs
├── packages.config
├── AuthViewController.designer.cs
├── Info.plist
├── AuthViewController.cs
├── AppDelegate.cs
├── Main.storyboard
└── Xamarin_GoogleAuth.iOS.csproj
├── Xamarin_GoogleAuth.Android
├── Resources
│ ├── drawable-hdpi
│ │ └── Icon.png
│ ├── drawable-mdpi
│ │ └── Icon.png
│ ├── values
│ │ └── Strings.xml
│ ├── drawable-xhdpi
│ │ └── Icon.png
│ ├── drawable-xxhdpi
│ │ └── Icon.png
│ ├── drawable-xxxhdpi
│ │ └── Icon.png
│ ├── layout
│ │ └── Main.axml
│ ├── AboutResources.txt
│ └── Resource.designer.cs
├── Configuration.cs
├── Properties
│ ├── AndroidManifest.xml
│ └── AssemblyInfo.cs
├── packages.config
├── Assets
│ └── AboutAssets.txt
├── GoogleAuthInterceptor.cs
├── MainActivity.cs
└── Xamarin_GoogleAuth.Android.csproj
├── Xamarin_GoogleAuth
├── Authentication
│ ├── GoogleOAuthToken.cs
│ ├── IGoogleAuthenticatorDelegate.cs
│ └── GoogleAuthenticator.cs
├── Services
│ ├── GoogleEmail.cs
│ └── GoogleService.cs
├── packages.config
├── Properties
│ └── AssemblyInfo.cs
└── Xamarin_GoogleAuth.csproj
├── README.md
├── .gitignore
└── Xamarin-GoogleAuth.sln
/Certificates/Android/README.txt:
--------------------------------------------------------------------------------
1 | Keystore password = woodenmoose
2 | Key alias = XamarinGoogleAuth
3 | Key password = woodenmoose
--------------------------------------------------------------------------------
/Certificates/Android/XamarinGoogleAuth.keystore:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TimLariviere/Sample-XamarinAuth-Google/HEAD/Certificates/Android/XamarinGoogleAuth.keystore
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.iOS/Resources/ITunesArtwork:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TimLariviere/Sample-XamarinAuth-Google/HEAD/Xamarin_GoogleAuth.iOS/Resources/ITunesArtwork
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.iOS/Resources/ITunesArtwork@2x:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TimLariviere/Sample-XamarinAuth-Google/HEAD/Xamarin_GoogleAuth.iOS/Resources/ITunesArtwork@2x
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.Android/Resources/drawable-hdpi/Icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TimLariviere/Sample-XamarinAuth-Google/HEAD/Xamarin_GoogleAuth.Android/Resources/drawable-hdpi/Icon.png
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.Android/Resources/drawable-mdpi/Icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TimLariviere/Sample-XamarinAuth-Google/HEAD/Xamarin_GoogleAuth.Android/Resources/drawable-mdpi/Icon.png
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.Android/Resources/values/Strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | Login with Google
4 |
5 |
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.Android/Resources/drawable-xhdpi/Icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TimLariviere/Sample-XamarinAuth-Google/HEAD/Xamarin_GoogleAuth.Android/Resources/drawable-xhdpi/Icon.png
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.Android/Resources/drawable-xxhdpi/Icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TimLariviere/Sample-XamarinAuth-Google/HEAD/Xamarin_GoogleAuth.Android/Resources/drawable-xxhdpi/Icon.png
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.Android/Resources/drawable-xxxhdpi/Icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TimLariviere/Sample-XamarinAuth-Google/HEAD/Xamarin_GoogleAuth.Android/Resources/drawable-xxxhdpi/Icon.png
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.iOS/Resources/Images.xcassets/AppIcons.appiconset/Icon-76.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TimLariviere/Sample-XamarinAuth-Google/HEAD/Xamarin_GoogleAuth.iOS/Resources/Images.xcassets/AppIcons.appiconset/Icon-76.png
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.iOS/Resources/Images.xcassets/AppIcons.appiconset/Icon-60@2x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TimLariviere/Sample-XamarinAuth-Google/HEAD/Xamarin_GoogleAuth.iOS/Resources/Images.xcassets/AppIcons.appiconset/Icon-60@2x.png
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.iOS/Resources/Images.xcassets/AppIcons.appiconset/Icon-60@3x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TimLariviere/Sample-XamarinAuth-Google/HEAD/Xamarin_GoogleAuth.iOS/Resources/Images.xcassets/AppIcons.appiconset/Icon-60@3x.png
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.iOS/Resources/Images.xcassets/AppIcons.appiconset/Icon-76@2x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TimLariviere/Sample-XamarinAuth-Google/HEAD/Xamarin_GoogleAuth.iOS/Resources/Images.xcassets/AppIcons.appiconset/Icon-76@2x.png
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.iOS/Resources/Images.xcassets/AppIcons.appiconset/Icon-Small.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TimLariviere/Sample-XamarinAuth-Google/HEAD/Xamarin_GoogleAuth.iOS/Resources/Images.xcassets/AppIcons.appiconset/Icon-Small.png
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.iOS/Resources/Images.xcassets/AppIcons.appiconset/Icon-Small-40.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TimLariviere/Sample-XamarinAuth-Google/HEAD/Xamarin_GoogleAuth.iOS/Resources/Images.xcassets/AppIcons.appiconset/Icon-Small-40.png
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.iOS/Resources/Images.xcassets/AppIcons.appiconset/Icon-Small@2x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TimLariviere/Sample-XamarinAuth-Google/HEAD/Xamarin_GoogleAuth.iOS/Resources/Images.xcassets/AppIcons.appiconset/Icon-Small@2x.png
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.iOS/Resources/Images.xcassets/AppIcons.appiconset/Icon-Small@3x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TimLariviere/Sample-XamarinAuth-Google/HEAD/Xamarin_GoogleAuth.iOS/Resources/Images.xcassets/AppIcons.appiconset/Icon-Small@3x.png
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.iOS/Resources/Images.xcassets/AppIcons.appiconset/Icon-Small-40@2x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TimLariviere/Sample-XamarinAuth-Google/HEAD/Xamarin_GoogleAuth.iOS/Resources/Images.xcassets/AppIcons.appiconset/Icon-Small-40@2x.png
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.iOS/Resources/Images.xcassets/AppIcons.appiconset/Icon-Small-40@3x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TimLariviere/Sample-XamarinAuth-Google/HEAD/Xamarin_GoogleAuth.iOS/Resources/Images.xcassets/AppIcons.appiconset/Icon-Small-40@3x.png
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.iOS/Entitlements.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/Certificates/Android/XamarinGoogleAuth.keyInfo:
--------------------------------------------------------------------------------
1 |
2 |
3 | 2017-08-21T00:00:00
4 | Mon Aug 21 10:36:35 CEST 2017 au : Wed Aug 14 10:36:35 CEST 2047
5 |
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth/Authentication/GoogleOAuthToken.cs:
--------------------------------------------------------------------------------
1 | namespace Xamarin_GoogleAuth.Authentication
2 | {
3 | public class GoogleOAuthToken
4 | {
5 | public string TokenType { get; set; }
6 | public string AccessToken { get; set; }
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth/Services/GoogleEmail.cs:
--------------------------------------------------------------------------------
1 | namespace Xamarin_GoogleAuth.Services
2 | {
3 | public class GoogleEmail
4 | {
5 | public GoogleEmailData Data { get; set; }
6 | }
7 |
8 | public class GoogleEmailData
9 | {
10 | public string Email { get; set; }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Sample-XamarinAuth-Google
2 | Sample app using Xamarin.Auth to authenticate the users against Google on Android and iOS.
3 | Make use of the IsUsingNativeUI flag to comply to Google's new policy for OAuth on native apps.
4 |
5 | See blog post for more information: http://timothelariviere.com/2017/09/01/authenticate-users-through-google-with-xamarin-auth
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.iOS/Configuration.cs:
--------------------------------------------------------------------------------
1 | namespace Xamarin_GoogleAuth.iOS
2 | {
3 | public static class Configuration
4 | {
5 | public const string ClientId = "723962257721-lcjvps5ag42butfd44tojo01cuj85d8t.apps.googleusercontent.com";
6 | public const string Scope = "email";
7 | public const string RedirectUrl = "com.woodenmoose.xamarin.googleauth:/oauth2redirect";
8 | }
9 | }
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.Android/Configuration.cs:
--------------------------------------------------------------------------------
1 | namespace Xamarin_GoogleAuth.Droid
2 | {
3 | public static class Configuration
4 | {
5 | public const string ClientId = "723962257721-ql0tki3si3s22l1lsovimivkmnrfm6rr.apps.googleusercontent.com";
6 | public const string Scope = "email";
7 | public const string RedirectUrl = "com.woodenmoose.xamarin.googleauth:/oauth2redirect";
8 | }
9 | }
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth/Authentication/IGoogleAuthenticatorDelegate.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Xamarin_GoogleAuth.Authentication
4 | {
5 | public interface IGoogleAuthenticationDelegate
6 | {
7 | void OnAuthenticationCompleted(GoogleOAuthToken token);
8 | void OnAuthenticationFailed(string message, Exception exception);
9 | void OnAuthenticationCanceled();
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.iOS/Main.cs:
--------------------------------------------------------------------------------
1 | using UIKit;
2 |
3 | namespace Xamarin_GoogleAuth.iOS
4 | {
5 | public class Application
6 | {
7 | // This is the main entry point of the application.
8 | static void Main (string[] args)
9 | {
10 | // if you want to use a different Application Delegate class from "AppDelegate"
11 | // you can specify it here.
12 | UIApplication.Main (args, null, "AppDelegate");
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.Android/Properties/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.Android/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.Android/Resources/layout/Main.axml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.Android/Assets/AboutAssets.txt:
--------------------------------------------------------------------------------
1 | Any raw assets you want to be deployed with your application can be placed in
2 | this directory (and child directories) and given a Build Action of "AndroidAsset".
3 |
4 | These files will be deployed with your package and will be accessible using Android's
5 | AssetManager, like this:
6 |
7 | public class ReadAsset : Activity
8 | {
9 | protected override void OnCreate (Bundle bundle)
10 | {
11 | base.OnCreate (bundle);
12 |
13 | InputStream input = Assets.Open ("my_asset.txt");
14 | }
15 | }
16 |
17 | Additionally, some Android functions will automatically load asset files:
18 |
19 | Typeface tf = Typeface.CreateFromAsset (Context.Assets, "fonts/samplefont.ttf");
20 |
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.iOS/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth/Services/GoogleService.cs:
--------------------------------------------------------------------------------
1 | using Newtonsoft.Json;
2 | using System.Net.Http;
3 | using System.Net.Http.Headers;
4 | using System.Threading.Tasks;
5 |
6 | namespace Xamarin_GoogleAuth.Services
7 | {
8 | public class GoogleService
9 | {
10 | public async Task GetEmailAsync(string tokenType, string accessToken)
11 | {
12 | var httpClient = new HttpClient();
13 | httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(tokenType, accessToken);
14 | var json = await httpClient.GetStringAsync("https://www.googleapis.com/userinfo/email?alt=json");
15 | var email = JsonConvert.DeserializeObject(json);
16 | return email.Data.Email;
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.iOS/AuthViewController.designer.cs:
--------------------------------------------------------------------------------
1 | // WARNING
2 | //
3 | // This file has been generated automatically by Visual Studio from the outlets and
4 | // actions declared in your storyboard file.
5 | // Manual changes to this file will not be maintained.
6 | //
7 | using Foundation;
8 | using System;
9 | using System.CodeDom.Compiler;
10 | using UIKit;
11 |
12 | namespace Xamarin_GoogleAuth.iOS
13 | {
14 | [Register ("AuthViewController")]
15 | partial class AuthViewController
16 | {
17 | [Outlet]
18 | [GeneratedCode ("iOS Designer", "1.0")]
19 | UIKit.UIButton GoogleLoginButton { get; set; }
20 |
21 | void ReleaseDesignerOutlets ()
22 | {
23 | if (GoogleLoginButton != null) {
24 | GoogleLoginButton.Dispose ();
25 | GoogleLoginButton = null;
26 | }
27 | }
28 | }
29 | }
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 |
4 | // Information about this assembly is defined by the following attributes.
5 | // Change them to the values specific to your project.
6 |
7 | [assembly: AssemblyTitle("Xamarin_GoogleAuth")]
8 | [assembly: AssemblyDescription("")]
9 | [assembly: AssemblyConfiguration("")]
10 | [assembly: AssemblyCompany("")]
11 | [assembly: AssemblyProduct("")]
12 | [assembly: AssemblyCopyright("joe")]
13 | [assembly: AssemblyTrademark("")]
14 | [assembly: AssemblyCulture("")]
15 |
16 | // The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
17 | // The form "{Major}.{Minor}.*" will automatically update the build and revision,
18 | // and "{Major}.{Minor}.{Build}.*" will update just the revision.
19 |
20 | [assembly: AssemblyVersion("1.0.*")]
21 |
22 | // The following attributes are used to specify the signing key for the assembly,
23 | // if desired. See the Mono documentation for more information about signing.
24 |
25 | //[assembly: AssemblyDelaySign(false)]
26 | //[assembly: AssemblyKeyFile("")]
27 |
28 |
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.Android/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using Android.App;
4 |
5 | // Information about this assembly is defined by the following attributes.
6 | // Change them to the values specific to your project.
7 |
8 | [assembly: AssemblyTitle ("Xamarin_GoogleAuth.Android")]
9 | [assembly: AssemblyDescription ("")]
10 | [assembly: AssemblyConfiguration ("")]
11 | [assembly: AssemblyCompany ("")]
12 | [assembly: AssemblyProduct ("")]
13 | [assembly: AssemblyCopyright ("joe")]
14 | [assembly: AssemblyTrademark ("")]
15 | [assembly: AssemblyCulture ("")]
16 |
17 | // The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
18 | // The form "{Major}.{Minor}.*" will automatically update the build and revision,
19 | // and "{Major}.{Minor}.{Build}.*" will update just the revision.
20 |
21 | [assembly: AssemblyVersion ("1.0.0")]
22 |
23 | // The following attributes are used to specify the signing key for the assembly,
24 | // if desired. See the Mono documentation for more information about signing.
25 |
26 | //[assembly: AssemblyDelaySign(false)]
27 | //[assembly: AssemblyKeyFile("")]
28 |
29 |
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.Android/GoogleAuthInterceptor.cs:
--------------------------------------------------------------------------------
1 | using Android.App;
2 | using Android.Content;
3 | using Android.OS;
4 | using System;
5 |
6 | namespace Xamarin_GoogleAuth.Droid
7 | {
8 | [Activity(Label = "GoogleAuthInterceptor")]
9 | [
10 | IntentFilter
11 | (
12 | actions: new[] { Intent.ActionView },
13 | Categories = new[]
14 | {
15 | Intent.CategoryDefault,
16 | Intent.CategoryBrowsable
17 | },
18 | DataSchemes = new[]
19 | {
20 | // First part of the redirect url (Package name)
21 | "com.woodenmoose.xamarin.googleauth"
22 | },
23 | DataPaths = new[]
24 | {
25 | // Second part of the redirect url (Path)
26 | "/oauth2redirect"
27 | }
28 | )
29 | ]
30 | public class GoogleAuthInterceptor : Activity
31 | {
32 | protected override void OnCreate(Bundle savedInstanceState)
33 | {
34 | base.OnCreate(savedInstanceState);
35 |
36 | Android.Net.Uri uri_android = Intent.Data;
37 |
38 | // Convert iOS NSUrl to C#/netxf/BCL System.Uri - common API
39 | Uri uri_netfx = new Uri(uri_android.ToString());
40 |
41 | // Send the URI to the Authenticator for continuation
42 | MainActivity.Auth?.OnPageLoading(uri_netfx);
43 |
44 | Finish();
45 | }
46 | }
47 | }
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.Android/Resources/AboutResources.txt:
--------------------------------------------------------------------------------
1 | Images, layout descriptions, binary blobs and string dictionaries can be included
2 | in your application as resource files. Various Android APIs are designed to
3 | operate on the resource IDs instead of dealing with images, strings or binary blobs
4 | directly.
5 |
6 | For example, a sample Android app that contains a user interface layout (main.axml),
7 | an internationalization string table (strings.xml) and some icons (drawable-XXX/icon.png)
8 | would keep its resources in the "Resources" directory of the application:
9 |
10 | Resources/
11 | drawable/
12 | icon.png
13 |
14 | layout/
15 | main.axml
16 |
17 | values/
18 | strings.xml
19 |
20 | In order to get the build system to recognize Android resources, set the build action to
21 | "AndroidResource". The native Android APIs do not operate directly with filenames, but
22 | instead operate on resource IDs. When you compile an Android application that uses resources,
23 | the build system will package the resources for distribution and generate a class called "R"
24 | (this is an Android convention) that contains the tokens for each one of the resources
25 | included. For example, for the above Resources layout, this is what the R class would expose:
26 |
27 | public class R {
28 | public class drawable {
29 | public const int icon = 0x123;
30 | }
31 |
32 | public class layout {
33 | public const int main = 0x456;
34 | }
35 |
36 | public class strings {
37 | public const int first_string = 0xabc;
38 | public const int second_string = 0xbcd;
39 | }
40 | }
41 |
42 | You would then use R.drawable.icon to reference the drawable/icon.png file, or R.layout.main
43 | to reference the layout/main.axml file, or R.strings.first_string to reference the first
44 | string in the dictionary file values/strings.xml.
45 |
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.iOS/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDisplayName
6 | Xamarin_GoogleAuth
7 | CFBundleIdentifier
8 | com.woodenmoose.xamarin.googleauth
9 | CFBundleShortVersionString
10 | 1.0
11 | CFBundleVersion
12 | 1.0
13 | LSRequiresIPhoneOS
14 |
15 | MinimumOSVersion
16 | 8.0
17 | UIDeviceFamily
18 |
19 | 1
20 | 2
21 |
22 | UILaunchStoryboardName
23 | LaunchScreen
24 | UIMainStoryboardFile
25 | Main
26 | UIMainStoryboardFile~ipad
27 | Main
28 | UIRequiredDeviceCapabilities
29 |
30 | armv7
31 |
32 | UISupportedInterfaceOrientations
33 |
34 | UIInterfaceOrientationPortrait
35 | UIInterfaceOrientationLandscapeLeft
36 | UIInterfaceOrientationLandscapeRight
37 |
38 | UISupportedInterfaceOrientations~ipad
39 |
40 | UIInterfaceOrientationPortrait
41 | UIInterfaceOrientationPortraitUpsideDown
42 | UIInterfaceOrientationLandscapeLeft
43 | UIInterfaceOrientationLandscapeRight
44 |
45 | XSAppIconAssets
46 | Resources/Images.xcassets/AppIcons.appiconset
47 | CFBundleURLTypes
48 |
49 |
50 | CFBundleURLName
51 | Google Auth
52 | CFBundleURLSchemes
53 |
54 | com.woodenmoose.xamarin.googleauth
55 |
56 | CFBundleURLTypes
57 | Viewer
58 |
59 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.iOS/Resources/LaunchScreen.storyboard:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth/Authentication/GoogleAuthenticator.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Xamarin.Auth;
3 |
4 | namespace Xamarin_GoogleAuth.Authentication
5 | {
6 | public class GoogleAuthenticator
7 | {
8 | private const string AuthorizeUrl = "https://accounts.google.com/o/oauth2/v2/auth";
9 | private const string AccessTokenUrl = "https://www.googleapis.com/oauth2/v4/token";
10 | private const bool IsUsingNativeUI = true;
11 |
12 | private OAuth2Authenticator _auth;
13 | private IGoogleAuthenticationDelegate _authenticationDelegate;
14 |
15 | public GoogleAuthenticator(string clientId, string scope, string redirectUrl, IGoogleAuthenticationDelegate authenticationDelegate)
16 | {
17 | _authenticationDelegate = authenticationDelegate;
18 |
19 | _auth = new OAuth2Authenticator(clientId, string.Empty, scope,
20 | new Uri(AuthorizeUrl),
21 | new Uri(redirectUrl),
22 | new Uri(AccessTokenUrl),
23 | null, IsUsingNativeUI);
24 |
25 | _auth.Completed += OnAuthenticationCompleted;
26 | _auth.Error += OnAuthenticationFailed;
27 | }
28 |
29 | public OAuth2Authenticator GetAuthenticator()
30 | {
31 | return _auth;
32 | }
33 |
34 | public void OnPageLoading(Uri uri)
35 | {
36 | _auth.OnPageLoading(uri);
37 | }
38 |
39 | private void OnAuthenticationCompleted(object sender, AuthenticatorCompletedEventArgs e)
40 | {
41 | if (e.IsAuthenticated)
42 | {
43 | var token = new GoogleOAuthToken
44 | {
45 | TokenType = e.Account.Properties["token_type"],
46 | AccessToken = e.Account.Properties["access_token"]
47 | };
48 | _authenticationDelegate.OnAuthenticationCompleted(token);
49 | }
50 | else
51 | {
52 | _authenticationDelegate.OnAuthenticationCanceled();
53 | }
54 | }
55 |
56 | private void OnAuthenticationFailed(object sender, AuthenticatorErrorEventArgs e)
57 | {
58 | _authenticationDelegate.OnAuthenticationFailed(e.Message, e.Exception);
59 | }
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.iOS/AuthViewController.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using UIKit;
3 | using Xamarin_GoogleAuth.Authentication;
4 | using Xamarin_GoogleAuth.Services;
5 |
6 | namespace Xamarin_GoogleAuth.iOS
7 | {
8 | public partial class AuthViewController : UIViewController, IGoogleAuthenticationDelegate
9 | {
10 | public static GoogleAuthenticator Auth;
11 |
12 | public AuthViewController (IntPtr handle) : base (handle)
13 | {
14 | }
15 |
16 | public override void ViewDidLoad()
17 | {
18 | base.ViewDidLoad();
19 |
20 | Auth = new GoogleAuthenticator(Configuration.ClientId, Configuration.Scope, Configuration.RedirectUrl, this);
21 |
22 | GoogleLoginButton.TouchUpInside += OnGoogleLoginButtonClicked;
23 | }
24 |
25 | private void OnGoogleLoginButtonClicked(object sender, EventArgs e)
26 | {
27 | var authenticator = Auth.GetAuthenticator();
28 | var viewController = authenticator.GetUI();
29 | PresentViewController(viewController, true, null);
30 | }
31 |
32 | public async void OnAuthenticationCompleted(GoogleOAuthToken token)
33 | {
34 | // SFSafariViewController doesn't dismiss itself
35 | DismissViewController(true, null);
36 |
37 | var googleService = new GoogleService();
38 | var email = await googleService.GetEmailAsync(token.TokenType, token.AccessToken);
39 |
40 | GoogleLoginButton.SetTitle($"Connected with {email}", UIControlState.Normal);
41 | }
42 |
43 | public void OnAuthenticationCanceled()
44 | {
45 | // SFSafariViewController doesn't dismiss itself
46 | DismissViewController(true, null);
47 |
48 | var alertController = new UIAlertController
49 | {
50 | Title = "Authentication canceled",
51 | Message = "You didn't completed the authentication process"
52 | };
53 | PresentViewController(alertController, true, null);
54 | }
55 |
56 | public void OnAuthenticationFailed(string message, Exception exception)
57 | {
58 | // SFSafariViewController doesn't dismiss itself
59 | DismissViewController(true, null);
60 |
61 | var alertController = new UIAlertController
62 | {
63 | Title = message,
64 | Message = exception?.ToString()
65 | };
66 | PresentViewController(alertController, true, null);
67 | }
68 | }
69 | }
--------------------------------------------------------------------------------
/Xamarin_GoogleAuth.Android/MainActivity.cs:
--------------------------------------------------------------------------------
1 | using Android.App;
2 | using Android.OS;
3 | using Android.Widget;
4 | using System;
5 | using Xamarin_GoogleAuth.Authentication;
6 | using Xamarin_GoogleAuth.Services;
7 |
8 | namespace Xamarin_GoogleAuth.Droid
9 | {
10 | [Activity(Label = "Xamarin_GoogleAuth", MainLauncher = true, Icon = "@drawable/icon")]
11 | public class MainActivity : Activity, IGoogleAuthenticationDelegate
12 | {
13 | // Need to be static because we need to access it in GoogleAuthInterceptor for continuation
14 | public static GoogleAuthenticator Auth;
15 |
16 | protected override void OnCreate(Bundle bundle)
17 | {
18 | base.OnCreate(bundle);
19 |
20 | SetContentView(Resource.Layout.Main);
21 |
22 | Auth = new GoogleAuthenticator(Configuration.ClientId, Configuration.Scope, Configuration.RedirectUrl, this);
23 |
24 | var googleLoginButton = FindViewById