Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Secure Google Plus Sign-in into Web App via Android App

I'm developing an Android app and I want users to sign-in to my app using Google+ Sign-In.

At the moment, I pass the user's name which I get from Google+ within a PHP login script. The script loads a new session with the user's ID.

Currently, the site is highly insecure: anyone who knows another user's username can potentially login as them.

What is a secure way to do this?

How do I authorize a user against my server?

It looks like to me, Google+ was purely a social networking API...

like image 892
Nathaniel Bennett Avatar asked Dec 02 '22 18:12

Nathaniel Bennett


1 Answers

Google+ Sign-In uses OAuth 2.0 - which means the user does not authenticate directly with your server. Instead they authenticate with Google and obtain a token signed by Google. Your app gets that token (from Google Play services on Android) and can pass it to your servers as proof that the user authenticated with Google. You then associate the users Google+ ID with a new or existing user ID on your own servers. So whenever a user can prove that they authenticated with Google for a specific Google+ user ID, you treat them as authenticated on your own server.

To implement, you have a few options depending on how you architect your system:

  1. When you simply want to authenticate your user to your own servers: On an Android device your user is very often already authenticated with Google because they have a Google account in the account manager. Your app can take advantage of this and obtain a token for a user in the account manager without them having to type any passwords. After the user clicks 'Sign in with Google' in your app, you can fetch an ID token for them using GoogleAuthUtils.getToken() and pass it to your server. Your server, after verifying the Google signature, can then safely associate the users session with the appropriate user account and permissions (ie. treat the session as authenticated). The process of getting the token and verifying it is discussed by Tim Bray here and by Ian Barber here.
  2. If you want to authenticate the user to your own servers and make Google+ API calls from your servers: Then you should take a look at the server side flow documentation on developers.google.com. This takes the same approach as option one, but in addition, when the users signs in for the very first time the Android app requests an authorization code instead of an ID token. This can be exchanged by the server for an access token and refresh token - which, in turn, can be used by your server to make API calls on behalf of the user, for example, by using the PHP client library.
  3. If you want to authenticate the user to your own servers, but also make Google API calls from the Android device: Then you should use the PlusClient provided by Google Play services to make Google API calls in addition to the steps you take to authenticate the user with your own server.

You probably want to make Google API calls in your client or on your server so that you can pre-populate your registration form with data from the users Google+ profile, for example.

like image 68
Lee Avatar answered Dec 10 '22 11:12

Lee