Our provider is not doing the whole 9 yards of OAuth (consumer being redirected to the login page, then send generate request token, etc.) Since this is a company portal, consumers/provider are trusted, the provider I'm creating just needs to generate a time-sensitive token based on some value and append it to the querystring so the consumer can decrypt it. Mar 02, 2017 If you use the same API key in multiple apps, a broken app could destroy your users' data without an easy way to stop just that one app. Some apps let users generate new API keys, or even have multiple API keys with the option to revoke one that may have gone into the wrong hands. The ability to change an API key limits the security downsides.
Important:
The Google OAuth 2.0 system supports server-to-server interactions such as those between a web application and a Google service. For this scenario you need a service account, which is an account that belongs to your application instead of to an individual end user. Your application calls Google APIs on behalf of the service account, so users aren't directly involved. This scenario is sometimes called 'two-legged OAuth,' or '2LO.' (The related term 'three-legged OAuth' refers to scenarios in which your application calls Google APIs on behalf of end users, and in which user consent is sometimes required.)
Typically, an application uses a service account when the application uses Google APIs to work with its own data rather than a user's data. For example, an application that uses Google Cloud Datastore for data persistence would use a service account to authenticate its calls to the Google Cloud Datastore API.
G Suite domain administrators can also grant service accounts domain-wide authority to access user data on behalf of users in the domain.
This document describes how an application can complete the server-to-server OAuth 2.0 flow by using either a Google APIs client library (recommended) or HTTP.
With some Google APIs, you can make authorized API calls using a signed JWT instead of using OAuth 2.0, which can save you a network request. See Addendum: Service account authorization without OAuth.
Overview
To support server-to-server interactions, first create a service account for your project in the API Console. If you want to access user data for users in your G Suite domain, then delegate domain-wide access to the service account.
Then, your application prepares to make authorized API calls by using the service account's credentials to request an access token from the OAuth 2.0 auth server.
Finally, your application can use the access token to call Google APIs.
Recommendation: Your application can complete these tasks either by using the Google APIs client library for your language, or by directly interacting with the OAuth 2.0 system using HTTP. However, the mechanics of server-to-server authentication interactions require applications to create and cryptographically sign JSON Web Tokens (JWTs), and it's easy to make serious errors that can have a severe impact on the security of your application.
For this reason, we strongly encourage you to use libraries, such as the Google APIs client libraries, that abstract the cryptography away from your application code.
Creating a service account
A service account's credentials include a generated email address that is unique and at least one public/private key pair. If domain-wide delegation is enabled, then a client ID is also part of the service account's credentials.
If your application runs on Google App Engine, a service account is set up automatically when you create your project.
If your application runs on Google Compute Engine, a service account is also set up automatically when you create your project, but you must specify the scopes that your application needs access to when you create a Google Compute Engine instance. For more information, see Preparing an instance to use service accounts.
If your application doesn't run on Google App Engine or Google Compute Engine, you must obtain these credentials in the Google API Console. To generate service-account credentials, or to view the public credentials that you've already generated, do the following:
Your new public/private keypair is generated and downloaded to your machine; it serves as theonly copy of this key. You are responsible for storing it securely. If you lose this keypair,you will need to generate a new one.
You can return to the API Console at any time to view the email address, public key fingerprints, and other information, or to generate additional public/private key pairs. For more details about service account credentials in the API Console, see Service accounts in the API Console help file.
Take note of the service account's email address and store the service account's P12 private key file in a location accessible to your application. Your application needs them to make authorized API calls.
Note: You must store and manage private keys securely in both development and production environments. Google does not keep a copy of your private keys, only your public keys.
Delegating domain-wide authority to the service account
If you have a G Suite domain—if you use G Suite, for example—an administrator of the G Suite domain can authorize an application to access user data on behalf of users in the G Suite domain. For example, an application that uses the Google Calendar API to add events to the calendars of all users in a G Suite domain would use a service account to access the Google Calendar API on behalf of users. Authorizing a service account to access data on behalf of users in a domain is sometimes referred to as 'delegating domain-wide authority' to a service account.
Note: When you use G Suite Marketplace to install an application for your domain, the required permissions are automatically granted to the application during installation. You do not need to manually authorize the service accounts that the application uses. The account must have domain-wide delegation before the application is installed.Note: Although you can use service accounts in applications that run from a G Suite domain, service accounts are not members of your G Suite account and aren't subject to domain policies set by G Suite administrators. For example, a policy set in the G Suite admin console to restrict the ability of G Suite end users to share documents outside of the domain would not apply to service accounts.
To delegate domain-wide authority to a service account, first enable domain-wide delegation for an existing service account in the Service accounts page or create a new service account with domain-wide delegation enabled.
Then, an administrator of the G Suite domain must complete the following steps:
Your application now has the authority to make API calls as users in your domain (to 'impersonate' users). When you prepare to make authorized API calls, you specify the user to impersonate.
Preparing to make an authorized API callJava
After you obtain the client email address and private key from the API Console, use the Google APIs Client Library for Java to create a
GoogleCredential object from the service account's credentials and the scopes your application needs access to. For example:
If you are developing an app on Google Cloud Platform, you can use the application default credentials instead, which can simplify the process.
Delegate domain-wide authority
If you have delegated domain-wide access to the service account and you want to impersonate a user account, specify the email address of the user account with the
setServiceAccountUser method of the GoogleCredential factory. For example:
Use the
GoogleCredential object to call Google APIs in your application.
Python
After you obtain the client email address and private key from the API Console, use the Google APIs Client Library for Python to complete the following steps:
Use the Credentials object to call Google APIs in your application.
HTTP/REST
Recommendation: Although your application can complete these tasks by directly interacting with the OAuth 2.0 system using HTTP, the mechanics of server-to-server authentication interactions require applications to create and cryptographically sign JSON Web Tokens (JWTs), and it's easy to make serious errors that can have a severe impact on the security of your application.
For this reason, we strongly encourage you to use libraries, such as the Google APIs client libraries, that abstract the cryptography away from your application code.
After you obtain the client ID and private key from the API Console, your application needs to complete the following steps:
![]()
The sections that follow describe how to complete these steps.
If the response includes an access token, you can use the access token to call a Google API. (If the response does not include an access token, your JWT and token request might not be properly formed, or the service account might not have permission to access the requested scopes.)
When the access token expires, your application generates another JWT, signs it, and requests another access token.
The rest of this section describes the specifics of creating a JWT, signing the JWT, forming the access token request, and handling the response.
Creating a JWT
A JWT is composed of three parts: a header, a claim set, and a signature. The header and claim set are JSON objects. These JSON objects are serialized to UTF-8 bytes, then encoded using the Base64url encoding. This encoding provides resilience against encoding changes due to repeated encoding operations. The header, claim set, and signature are concatenated together with a period (
. ) character.
A JWT is composed as follows:
The base string for the signature is as follows:
Forming the JWT header
The header consists of two fields that indicate the signing algorithm and the format of the assertion. Both fields are mandatory, and each field has only one value. As additional algorithms and formats are introduced, this header will change accordingly.
Service accounts rely on the RSA SHA-256 algorithm and the JWT token format. As a result, the JSON representation of the header is as follows:
The Base64url representation of this is as follows:
Forming the JWT claim set
The JWT claim set contains information about the JWT, including the permissions being requested (scopes), the target of the token, the issuer, the time the token was issued, and the lifetime of the token. Most of the fields are mandatory. Like the JWT header, the JWT claim set is a JSON object and is used in the calculation of the signature.
Required claims
The required claims in the JWT claim set are shown below. They may appear in any order in the claim set.
Suitecrm Api Oauth Unable To Generate Private Key Blockchain
The JSON representation of the required fields in a JWT claim set is shown below:
Additional claims
In some enterprise cases, an application can request permission to act on behalf of a particular user in an organization. Permission to perform this type of impersonation must be granted before an application can impersonate a user, and is usually handled by a domain administrator. For more information on domain administration, see Managing API client access.
To obtain an access token that grants an application delegated access to a resource, include the email address of the user in the JWT claim set as the value of the
sub field.
If an application does not have permission to impersonate a user, the response to an access token request that includes the
sub field will be an error.
An example of a JWT claim set that includes the
sub field is shown below:
Encoding the JWT claim set
Like the JWT header, the JWT claim set should be serialized to UTF-8 and Base64url-safe encoded. Below is an example of a JSON representation of a JWT Claim set:
Computing the signature
JSON Web Signature (JWS) is the specification that guides the mechanics of generating the signature for the JWT. The input for the signature is the byte array of the following content:
The signing algorithm in the JWT header must be used when computing the signature. The only signing algorithm supported by the Google OAuth 2.0 Authorization Server is RSA using SHA-256 hashing algorithm. This is expressed as
RS256 in the alg field in the JWT header.
Sign the UTF-8 representation of the input using SHA256withRSA (also known as RSASSA-PKCS1-V1_5-SIGN with the SHA-256 hash function) with the private key obtained from the Google API Console. The output will be a byte array.
The signature must then be Base64url encoded. The header, claim set, and signature are concatenated together with a period (
. ) character. The result is the JWT. It should be the following (line breaks added for clarity):
Below is an example of a JWT before Base64url encoding:
Below is an example of a JWT that has been signed and is ready for transmission:
Making the access token request
After generating the signed JWT, an application can use it to request an access token. This access token request is an HTTPS
POST request, and the body is URL encoded. The URL is shown below:
The following parameters are required in the HTTPS
POST request:
Below is a raw dump of the HTTPS
POST request used in an access token request:
Below is the same request, using
curl :
Handling the response
If the JWT and access token request are properly formed and the service account has permission to perform the operation, then the JSON response from the Authorization Server includes an access token. The following is an example response:
Access tokens can be reused until during the duration noted in the
expires_in value.
Calling Google APIsJava
Use the
GoogleCredential object to call Google APIs by completing the following steps:
Python
Use the authorized
Credentials object to call Google APIs by completing the following steps:
HTTP/REST
After your application obtains an access token, you can use the token to make calls to a Google API on behalf of a given service account or user account if the scope(s) of access required by the API have been granted. To do this, include the access token in a request to the API by including either an
access_token query parameter or an Authorization HTTP header Bearer value. When possible, the HTTP header is preferable, because query strings tend to be visible in server logs. In most cases you can use a client library to set up your calls to Google APIs (for example, when calling the Drive Files API).
You can try out all the Google APIs and view their scopes at the OAuth 2.0 Playground.
HTTP GET examples
A call to the
drive.files endpoint (the Drive Files API) using the Authorization: Bearer HTTP header might look like the following. Note that you need to specify your own access token:
Here is a call to the same API for the authenticated user using the
access_token query string parameter:
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |