Tuesday, 3 March 2015

Understanding Service Accounts

Misconceptions about Google service accounts are at the heart of a number of problems I’ve seen developers having on Stack Overflow and various issue trackers. Hopefully this post will dispel some common misunderstandings, and break down what they are for.

What is a service account for?

Requests to many APIs need to be authorised to access data or services. In most cases this is done interactively with a user - the site presents a sign-in button, the user grants the site access to a part of their Google account, and the site receives an access token they can pass with their requests. Google checks this token to ensure the query is allowed to access the data it is requesting.

However, there are some situations where the user is not actually present - for example a daily batch script which downloads data from a Google Analytics account, or a process which provisions services for a Google Apps user when a new staff member starts at a company. In these cases, a service account is used to retrieve that authentication token rather than an interactive exchange. Normally service accounts are a "server side" or offline access method, so you are very unlikely to use them from browser Javascript, or and Android or iOS app.

How do service accounts work?

A service account is a sort of virtual user or robot account associated with a given project. It has an email address, and a cryptographic key pair. Google keeps one part of the key, and the developer keeps the other. To authenticate, the developer creates a request for an access token and signs it using the key. The request contains information on the service that the robot account wants to access. Google’s auth services process this request to check the service account should be able to access those things, verify the signature, and if all is well return an access token.

This access token authenticates a request as being from the service account user. This is the step that causes problems for many people - because they created the service account in their Google Developer Console project, they assume that the tokens that the service account retrieves represent them - but it doesn't, it represents this virtual user and all Google knows about it is that it is a member of a certain console project. In most cases, that virtual user needs to be granted specific access to anything you want it to be able to retrieve.

Accessing data and services.

For APIs that can be used without authentication, such as the Google+ search API, authenticating with a service account will just associate the call with your Developer Console project. This can be valuable, as it allows increased quota over and above the anonymous access, but you could equally use a Public API access key, which doesn’t require retrieving a token.

However, in most cases the service account is useful only if it can access some user data - but it itself doesn’t have any. The solution is delegation: granting access to the service account to specific user data. The easiest way to see this is to walk through some scenarios. Note that these things do change over time, so if you’re reading this some time after it was written things might be in a different place, but hopefully the general approach should still work.

Creating a service account

Visit the Google Developers Console and select (or create!) the project you want to use. Then in APIs & auth go to the Credentials option. Select Create Client ID, and select type Service Account.

When you click OK, you’ll download a .p12 file in your browser, and see an entry with an email address in it, something @developer.gserviceaccount.com. The .p12 file is your part of the cryptographic key, so treat it as a secret, and don't lose it! You can generate a new one from the console, but you can't download this one again.

Next you need to enable access to any APIs the service account wants to use. This doesn't give you access to user data, instead just enabling the account to query those APIs at all. Under APIs & auth go to Apis and switch on the services you need - if you're following along with this post choose the Analytics API and Drive API.

Now we can look at how you actually grant service accounts access to a couple of different sets of user data.

Accessing Google Analytics data

The Google Analytics API offers you pretty much complete access to the stats about your site or app, but a service account wont be able to see any of it unless you have granted it access. With analytics this is done by adding the service account email address as a user to your Analytics account. Simply access Analytics, go to the Administration > Property Permissions section and select the User Management settings. At the bottom you’ll see an Add permissions for box, which takes an email address. Add your service account email address from the developer console here.

Once that's done, we can retrieve an access token for the service account user, using the https://www.googleapis.com/auth/analytics.readonly scope, and query the API just as we would if we had a user signed in via the regular OAuth 2.0 flow. In this case we aren't pretending to be a regular Google user at all - we've specifically granted our robot account access to view our analytics data.

This code is PHP, but the flow should be similar with any of the Google API client libraries. Walking through it, we:

  1. Setup the values for our service account - its email address and the location of the p12 file primarily.
  2. Load in the key file and create an assertion for the Google Analytics scope.
  3. Call refreshTokenWithAssertion to get a new access token.
  4. Query the API.

It is worth noting that the call to get an access token is a little expensive - if you're going to be making multiple calls definitely cache the access token, and only refresh it with the service account when it expires.

Access Google Drive for a Google Apps User

Google Apps domains are designed to be administered through a variety of tools, so come with a more flexible delegation model than just approving the email address like in Analytics. You can actually approve a service account to act on behalf of the users of a Google Apps domain with a predefined set of scopes via the Apps admin console (you can read more about it on the Google Drive delegation page).

Adding an account is done through the Admin page under Security - Advanced Settings (under More) - Manage Api Client Access. To allow access you enter the service account client id (not the email address) and the scopes you wish to access. In our case that's the https://www.googleapis.com/auth/drive.readonly scope.

We also have to take an extra step in our code and specify precisely which account we want to access, as it could now be any valid user under the apps domain. We use the sub (subject) parameter on the assertion credential to indicate this by setting it to the email address or the Google user ID of the user we want to act on behalf of. Take a look at the following snippet as compared to the analytics one above - other than the service and scope the only difference is that additional sub parameter.

As a follow up to the note about caching above, be aware that the tokens will be different depending on the sub and scopes requested, even if they're from the same service account. For example: if you cached the token from the Analytics query and then the token from the Drive query, they would be different and not have access to each others resources. Be sure to include the sub, scopes parameter, and service account email address in any cache key generation you do.

Anything else?

Service accounts pop up in all sorts of places, each with their own delegation methods. For example, Google Cloud services have automatic service accounts made available in Compute Engine and App Engine to make querying the Cloud APIs easier. In this case you may grant the scopes via the gcloud command line tool and retrieve the access token via calling a special endpoint. In general with the Cloud services you do not have to worry about delegating access as the data they query is generally owned by the project (for example with a service like Cloud Storage) rather than a specific user.

Hopefully this has made the service account idea a little clearer - they're a powerful tool for background or batch access to APIs, but can easily be confusing (like most auth related topics). If you’re having trouble with one make sure you have correctly configured access for the service account in the admin controls of the relevant API, and that you have requested the appropriate scopes and subject when creating the token request assertion credential.

For more information on service accounts, check out the official documentation, and for any thorny questions try the google-oauth StackOverflow tag.

Thursday, 25 September 2014

Upgrading to Google Cast iOS SDK 2.4.0

Recently I've been working on the samples for the Chromecast SDK on iOS. Since we've just updated the sample app for the brand new 2.4.0 SDK, I thought I'd briefly note down the changes I had to make, which should help if you need to update your own app from 2.3.0. As always, the full list of changes can be found in the release notes.

Update Frameworks

The first step is to drop the new GoogleCast.framework into the project. You'll immediately notice that some classes have changed, and that there is a new dependency on SystemConfiguration.framework, so add that in your Linked Frameworks and Libraries section in the General Project Settings.

GCKDeviceScanner

To simplify the scanning and connection process, filtering has been merged right into GCKDeviceScanner. Start by removing any references to GCKDeviceFilterListener or GCKDeviceFilter as this functionality is now part of the GCKDeviceScanner directly. Remove any of the delegate methods you may have implemented in GCKDeviceFilterListener, but keep the contents - you can use the same in the GCKDeviceScannerListener methods.

Remove any references to add/removeDeviceFilterListener, then move the contents of: deviceDidComeOnline:forDeviceFilter: into the GCKDeviceScannerListener method: deviceDidComeOnline:.

For deviceDidGoOffline:forDeviceFilter:, use the GCKDeviceScannerListener equivalent: deviceDidGoOffline:.

Where you would have initiated scanning by creating a GCKDeviceFilter with criteria and a scanner, you now attach the filter criteria directly to the GCKDeviceScanner. Keep your GCKFilterCriteria object, and instead of calling initWithDeviceScanner:criteria: simply attach your criteria object to the filterCriteria property on your GCKDeviceScanner: self.deviceScanner.filterCriteria = filterCriteria;

Listing Devices

Where you would have iterated over the GCKDeviceFilter.devices array to get the currently discovered devices, you can now use the GCKDeviceScanner.devices array directly. Replace all references to the filter array with the GCKDeviceScanner devices one.

There is a gotcha here to be aware of though! The devices array is updated after the deviceDidGoOffline call is fired. If you're using that callback to update whether or not the Cast icon appears in your app, you might hit trouble. The solution is to do your UI update in the next runloop, which you can easily do with performSelector. In the CastVideos sample, we do exactly that:

GCKDeviceManager

The other class that has received significant attention is GCKDeviceManager, with new properties and callbacks to give you more information, or easier access to that information. Some of these will require changes in your code though.

The GCKDeviceManagerDelegate method deviceManager:didReceiveStatusForApplication: has a new name. If you implemented this method, update it to the new signature: deviceManager:didReceiveApplicationMetadata:.

If you were listening for deviceManager:didDisconnectWithError to pick up network based disconnections (e.g. the WiFi disappearing), there is a small issue that that this wont fire if you disable the WiFi on your device, which you may do when testing or similar). However, you can listen to the new deviceManager:didSuspendConnectionWithReason: and will get a callback with the reason GCKConnectionSuspendReasonNetworkError (see Connection Suspension below) in any case.

Error Handling

One wider change is that many GCKDeviceManager methods now return an NSInteger containing the request ID rather than a BOOL, with immediate failure indicated by the value kInvalidRequestID, which conveniently is 0. This allows tracking whether requests subsequently fail, thanks to a new delegate protocol method: deviceManager:request:didFailWithError:.

Connection Suspension

There are new delegate methods as well for suspended and resumed connections, didSuspendConnectionWithReason and deviceManagerDidResumeConnection:rejoinedApplication. Suspended indicates the connection has been interrupted, but the device manager will attempt to connect automatically. You shouldn’t use this method to force a reconnection, but you might want to hint in the UI that the Chromecast device isn’t going to respond immediately to commands. GCKConnectionSuspendedReason is an enum indicating whether the suspension is due to the app being backgrounded, or a network related problem.

Device Status Text

The device manager protocol also has new status information callbacks. One of the most useful is a text field describing the currently running application: deviceManager:didReceiveApplicationStatusText:. This will contain "YouTube TV" or similar to indicate the app currently running. This can be helpful to display to users in case there are similarly named Cast devices on their network. The same information is conveniently available on a GCKDevice directly as the statusText property.

Launching An App

Finally, if you were previously using launchApplication:relaunchIfRunning: then its worth noting that method has been deprecated in favour of a more extensible launchApplication:withLaunchOptions. The second parameter is a new GCKLaunchOptions object. To recreate the old call, you can use:

Monday, 4 August 2014

Detect whether users have uploaded a profile picture to Google+

Very short post on a small feature that I know will be a popular one with some people! A regular feature request has been the ability to determine whether the profile picture with a Google+ profile is the default blue head or not. You can do that right now with the people.get API calls - and you can try it yourself from that page using the explorer.

The change is that under the "image" key you'll see an "isDefault" value. For my user you can see it's false:

"image": {
  "url": "https://lh6.googleusercontent.com/.../photo.jpg?sz=50",
  "isDefault": false
 },

But for this blue head user, it's true:

"image": {
  "url": "https://lh3.googleusercontent.com/.../photo.jpg?sz=50",
  "isDefault": true
 },

Hopefully this should make it easier to determine whether to use the profile picture from Google+ when people sign in to your apps.Client libraries might take some time to be regenerated, but its certainly available in the API right now.

Wednesday, 18 June 2014

Google Sign In with Server Side Auth on iOS

The release today of version 1.7 of the Google+ iOS SDK added the ability to authorise both a client and server for access to Google APIs. This has been a feature for Android and Web based sign-ins for a while, and now is available across all three platforms. This should simplify server side code for people who have been building cross platform apps - for example if you need to retrieve profile information on the client, but retrieve circles on a server for a friend finding feature.

The implementation for iOS is fairly straightforward. When setting up authentication, simply specify the client ID of your server on on the GPPSignIn object. This should be a client ID from the developer console which is used by your server-side code, and should be part of the same console project as your iOS client ID. For those that have done it, this is the same parameter used when retrieving an ID token:

You can see that the first part of the client ID (the project number) is the same, but the other part varies. If you try and exchange a code generated for one client ID using a different client ID you'll get an invalid_client error, even if both client IDs are part of the same project. When the user is signed in via [GPPSignIn authenticate] you can retrieve the code parameter from the GPPSignIn instance as part of the auth callback:

You can send this parameter to your server exactly as you would with one from Javascript or Android. When exchanging, be sure to not set a redirect uri in the server side auth configuration or you will get back a HTTP 400 error "redirect_uri_mismatch" from the token POST call. The exchange should always result in a refresh token (unlike with the web), so be sure to store that safely in a database or other persistent store if you're using it.

If the user is seamlessly signed in via a call to trySilentAuthentication, you won’t receive a code to use. Instead of forcing the user to sign in again with authenticate (and swap out to the browser or Google+ app to consent again) when you’re establishing a session with the server, try sending across an ID token and checking whether the server already has a refresh token for that user - this will mean a smoother experience for the user in most cases.