Coder Social home page Coder Social logo

xamarin.googleauth's Introduction

Xamarin.GoogleAuth

Cross platform plugin for doing Google Authentication for Xamarin iOS and Android apps

Introduction

Google has recently updated its security restrictions for OAuth flow. They are not going to allow native web-views to initiate OAuth flows, but rather are encouraging people to use the OS browsers to do so. The current implementation of the Xamarin.Auth mechanism still uses web-views for initiating the Auth flow which is deprecated and will not work. This plugin attempts to solve that problem by implementing the recommended mechanism for doing Google Auth on native iOS and Android applications and porting it to Xamarin.

Integration guide

Here are the basic steps that you need to follow to add Google Authentication into your Xamarin forms application

  1. Setup your Application on the Google developer's console to use OAuth2. Guide here. Make sure you select the Web Application option.

  2. Download this repository and copy over the GoogleAuth, Plugin.GoogleAuth, Plugin.GoogleAuth.Android and Plugin.GoogleAuth.iOS folders into your application and add them into your solution in VS or Xamarin Studio.

  3. Add references to the Plugin.GoogleAuth and Plugin.GoogleAuth.Abstractions projects into your PCL project.

  4. Add references to the Plugin.GoogleAuth.Android and Plugin.GoogleAuth.Abstractions projects into your Android project.

  5. Add references to the Plugin.GoogleAuth.iOS and Plugin.GoogleAuth.Abstractions projects into your iOS project. Also add a reference to the GoogleSignin project which resides inside the Plugin.GoogleAuth.iOS/GoogleSignInWrapper folder.

  6. Add the Xamarin Google Play Services - Base, Basement and Auth Nuget packages (recommended version : 32.961.0) into your Android project.

  7. In your iOS project, open the Build Settings pane and add these compiler flags under Additional mtouch arguments:

         -gcc_flags "-ObjC -framework CoreText -framework SafariServices -framework SystemConfiguration"
    
  8. Also, make sure to add the URL types required for handling Google SignIn in the Info.plist file of your iOS application.

  9. Inside your platform specific projects initialize the Google Authentication service as shown below.

     	IGoogleAuthenticationService _service;
     	_service = CrossGoogleAuth.Current;
     	Dictionary<string, object> googleSignInConfig = new Dictionary<string, object> {
     		{"clientId", "<client-id>"},
     		{"context", this} // Android only
     	};
     	_service.Init(googleSignInConfig);
    
  10. For iOS, override the OpenUrl method call in the AppDelegate.cs file and call the HandleURL method of the plugin.

     public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
     {
     	string sourceApplication = options["UIApplicationOpenURLOptionsSourceApplicationKey"].ToString();
     	NSObject annotation = options["UIApplicationOpenURLOptionsAnnotationKey"] ?? new NSObject();
     	return _service.HandleURL(url, sourceApplication, annotation);
     }
    
  11. For Android, override the OnActivityResult method call in the MainActivity.cs file and call the OnActivityResult method of the Android plugin.

     protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
     {
     	base.OnActivityResult(requestCode, resultCode, data);
     	var googleAuthService = _service as GoogleAuthImpl;
     	googleAuthService.OnActivityResult(requestCode, resultCode, data);
     }
    

Usage

Init

Initialize the Google Auth plugin by providing the required keys in the IGoogleAuthenticationService.Init() method. Currently supported keys :

  1. clientId : Client ID for authentication with Google Auth
  2. context : Application context (Android Only)

Connect and Signin

To connect to the Google SignIn service and provide a way for the user to sign in to your application, use the IGoogleAuthenticationService.SignIn() method call. If the application has already connected, the plugin will try and perform a sign in silently. To sign out, please call the IGoogleAuthenticationService.SignOut()

Callbacks

To get the status of the sign in call, please implement the IGoogleAuthenticationCallbacks interface and its methods.

void OnConnectionSucceeded();
void OnConnectionFailed(string errorMessage);

Set the Authentication callbacks via the IGoogleAuthenticationCallbacks.SetAuthenticationCallbacks() method.

Fetch Information

Your application can fetch the following user information in the OnConnectionSucceeded method.

  1. Use the IGoogleAuthenticationCallbacks.GetIdToken method to get the ID token for the signed in user. This ID token can be used to authenticate the user on the server side.
  2. Use the IGoogleAuthenticationCallbacks.GetAccountName method to get the name of the signed in user.
  3. Use the IGoogleAuthenticationCallbacks.Disconnect method to disconnect the user account from this app in Google Auth.

Gotchas

  1. iOS requires the client-id of an iOS client application created on the Google Developer console.
  2. Android on the other hand requires the client-id of a Web application.

TODOs

  1. Convert this into a NuGet package !

xamarin.googleauth's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

xamarin.googleauth's Issues

Android Issue

Hello,

I have followed your directions multiple times and I cannot figure out what I am receiving this error:

Unable to find explicit activity class {com.openarc.bunxious/com.google.android.gms.auth.api.signin.internal.SignInHubActivity}; have you declared this activity in your AndroidManifest.xml?

The error is occurring in GoogleAuthAndroid on line 79.

Any help would be appreciated.

Great Library!!!

Thanks,
Marc

iOS Device Issue

Hello,

I have integrated your library into my project and it is running fine on a Simulator. When I run it on a device, I receive this error:

uiDelegate must either be a |UIViewController| or implement the |signIn:presentViewController:| and |signIn:dismissViewController:| methods from |GIDSignInUIDelegate|

Any assistance would be greatly appreciated.

Thanks,
Marc

iOS fails - Invalid IL code in GoogleSignIn

First, @rhishikeshj, thanks for the effort you've put into this project. Much appreciated. Finally got Droid working, and love the way it behaves.

Now for iOS - I can't get the sample iOS project to initialize. Here's the error I get when running in the simulator:
screen shot 2017-05-14 at 11 21 28 pm

I get this no matter what I send in as a clientId.

Steps to reproduce:
Clone current repo.
Open in Xamarin Studio or Visual Studio on Mac
Update to Xamarin Forms 2.3.4.231 (wouldn't let me run otherwise).
Run GoogleAuthSample.iOS in a simulator

Details: at Plugin.GoogleAuth.GoogleAuthImpl.Init (System.Collections.Generic.Dictionary`2[TKey,TValue] config) [0x00035] in /Development/github/authAgain/Xamarin.GoogleAuth/Plugin.GoogleAuth.iOS/GoogleAuthiOS.cs:122
at GoogleAuthSample.iOS.AppDelegate.FinishedLaunching (UIKit.UIApplication uiApplication, Foundation.NSDictionary launchOptions) [0x0003a] in /Development/github/authAgain/Xamarin.GoogleAuth/iOS/AppDelegate.cs:23
at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/data/lanes/4691/3e5ac5ff/source/xamarin-macios/src/UIKit/UIApplication.cs:79
at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/4691/3e5ac5ff/source/xamarin-macios/src/UIKit/UIApplication.cs:63
at GoogleAuthSample.iOS.Application.Main (System.String[] args) [0x00001] in /Development/github/authAgain/Xamarin.GoogleAuth/iOS/Main.cs:17

I'd run it on a device, but I get an error like

Native linking failed for '/Development/github/authAgain/Xamarin.GoogleAuth/iOS/obj/iPhone/Debug/device-builds/ipad5.1-10.3.1/mtouch-cache/arm64/libGoogleSignIn.dll.dylib'

If anything jumps out at anyone, please let me know. Not sure if anyone has already resolved a similar issue .

Signin Error: Status Code 12501

Hi Rhishikesh! First of all, thank you for your amazing job doing this !

I'm facing an issue while trying the sample project on Android, and I don't know what I'm doing wrong.
In your sample project, I only added my client id from my OAuth client and I run it. When I log in, I am getting always this:

image

Do you know what is happening ?

Thank you !

Add server auth code flow

The access code returned by GetAuthToken can only be used in the client; it cannot be passed to a server. This means you can't use this library for Google Sign In if you need to be signed in on your server.

This is a feature of GoogleSignIn. I'm adding GetServerAuthCode() to the interface. I have it working for iOS.

For Android, there is an additional issue that GoogleAuthUtil, used in this library, seems to be deprecated and may not return the server auth code:

If you use GoogleAuthUtil to get a server auth code, you should migrate to the new Sign-In API auth code flow.

https://developers.google.com/identity/sign-in/android/migration-guide

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.