oauth2l

repository·master·Indexed 20 days ago

https://github.com/google/oauth2l

A Go-based command-line tool designed to simplify working with Google OAuth 2.0. It allows developers to fetch access tokens for various authentication flows—including user accounts, service accounts, JWT, and SSO—and use them in shell scripts, with curl, or via a provided wrapper command. The tool supports JSON credential files, token caching, and provides utilities to inspect token metadata and validate token status.

Tokens
4.9K
Snippets
28
Records
31
Agent score
70%

What's inside oauth2l

  1. How oauth2l handles authentication flows

    master

    The oauth2l tool automatically detects the appropriate authentication context based on your environment:

    • Google Compute Engine (GCE) / Google Kubernetes Engine (GKE): Uses the current service account credentials if available.
    • Google Cloud SDK (gcloud): Uses the current active gcloud session credentials.
    • JSON Credential Files: Use the --credentials xxx flag to point to a service account key or OAuth client ID JSON file.
    • JWT (Service Accounts): Use --type jwt --audience xxx with a service account key to generate a signed JWT token.
    • SSO: Use --type sso --email xxx to invoke an external sso command for Single Sign-on tokens.

    Caching

    By default, tokens are cached in ~/.oauth2l.

    • Override location: --cache xxx
    • Disable caching: --cache "" (empty string)
  2. Configure authentication types with --type

    master

    The --type option determines the authentication mechanism. Supported types are oauth, jwt, and sso (defaults to oauth).

    oauth

    Performs a 2-legged OAuth flow if a service account key is provided, or a 3-legged OAuth flow (requiring user consent) if an OAuth Client ID is provided.

    $ oauth2l fetch --type oauth --credentials ~/client_credentials.json --scope cloud-platform

    jwt

    Generates a JWT token signed by a service account private key. Requires either --audience or --scope to be specified.

    # Using audience
    $ oauth2l fetch --type jwt --credentials ~/service_account.json --audience https://pubsub.googleapis.com/
    
    # Using scope
    $ oauth2l fetch --type jwt --credentials ~/service_account.json --scope cloud-platform

    sso

    Uses an external Single Sign-on (SSO) CLI to fetch a token. Requires an --email.

    # Using default Google corporate SSO CLI
    $ oauth2l header --type sso --email me@google.com --scope cloud-platform
    
    # Using a custom SSO CLI
    # The custom CLI must accept: [email] [scope1] [scope2]
    $ oauth2l header --type sso --ssocli /usr/bin/sso --email me@google.com --scope cloud-platform
  3. Install oauth2l

    master

    You can install oauth2l using several methods depending on your environment:

    Homebrew (Mac OS X)

    brew install oauth2l

    Docker

    Use the official image gcr.io/oauth2l/oauth2l. You can run it directly:

    docker run -it gcr.io/oauth2l/oauth2l header cloud-platform

    Or copy the binary into your own Dockerfile:

    FROM my-awesome-container
    COPY --from=gcr.io/oauth2l/oauth2l /bin/oauth2l /bin/oauth2l

    From Source (Linux or Mac)

    Requires Go 1.10.3 or higher:

    git clone https://github.com/google/oauth2l
    cd oauth2l
    make dev

    Note: If you install from source, ensure $GOPATH/bin is in your $PATH to run oauth2l directly.

    brew install oauth2l
  4. Install oauth2l via pip

    master

    You can install oauth2l using pip. Depending on your OS and desired installation scope, use one of the following methods:

    System-wide installation (typically /usr/local/bin):

    # Mac only. Install pip first if needed
    $ sudo easy_install pip
    
    # Install and upgrade
    $ pip install google-oauth2l --upgrade
    
    # If you encounter errors on OS X El Capitan or later
    $ pip install google-oauth2l --upgrade --ignore-installed

    User-specific installation (typically ~/.local/bin on Linux or ~/Library/Python/2.7/bin on Mac):

    $ pip install --user google-oauth2l
    # Install oauth2l under the current user
    $ pip install --user google-oauth2l
  5. Use oauth2l commands to manage OAuth 2.0 tokens

    master

    The oauth2l CLI provides several commands to fetch, inspect, and manage OAuth 2.0 access tokens. The primary commands are:

    • fetch: Fetches an access token.
    • header: Fetches an access token and returns it in an HTTP header format (e.g., Authorization: Bearer <token>).
    • curl: Fetches an access token and immediately uses it to make a curl request to a specified URL.
    • info: Displays metadata and information about an existing OAuth access token.
    • test: Validates an OAuth access token. Returns exit code 0 if the token is valid.
    • reset: Clears the local credential cache.
    • web: Launches a local instance of the OAuth2l Playground web app (experimental).
  6. Configure authentication types for fetching tokens

    master

    When using fetch, header, or curl commands, you must specify an authentication type using the --type flag. The available types are:

    • oauth: Executes a 2-legged OAuth (2LO) flow for Service Accounts or a 3-legged OAuth (3LO) flow for OAuth Client IDs. This is the default.
    • jwt: Signs claims using a private key. This only works for Service Accounts. Requires the --audience flag.
    • sso: Exchanges a LOAS credential for an OAuth token. Requires the --email flag.
    # Example: Using OAuth flow (default)
    # Example: Using JWT flow
    # Example: Using SSO flow
  7. Use oauth2l with curl via the header command

    master

    The header command is a wrapper for fetch that outputs the token in the Authorization: Bearer <token> HTTP header format. This is ideal for use with curl.

    Direct usage with curl:

    $ curl -H "$(oauth2l header pubsub)" https://pubsub.googleapis.com/v1/projects/my-project-id/topics

    Recommended: Create a shell alias for frequent API calls:

    alias gcurl='curl -H "$(oauth2l header cloud-platform)" -H "Content-Type: application/json" '
    $ gcurl 'https://pubsub.googleapis.com/v1/projects/my-project-id/topics'
    $ curl -H "$(oauth2l header pubsub)" https://pubsub.googleapis.com/v1/projects/my-project-id/topics
  8. Test token validity in shell pipelines

    master

    The test command checks if a token is valid. It returns an exit code of 0 for a valid token and 1 for an invalid one, making it useful for shell script logic.

    $ oauth2l test <TOKEN>
    $ echo $?
    $ oauth2l test ya29.zyxwvutsrqpnmolkjihgfedcba
  9. Fetch OAuth 2.0 access tokens

    master

    The fetch command retrieves and prints an access token for the specified OAuth scopes.

    Output Formats: Use the -f flag to specify the output format. Supported formats are:

    • bare (default): Prints only the token string.
    • json: Prints a JSON object containing the token, expiry, and metadata.
    • json_compact: Compact JSON output.
    • pretty: Pretty-printed JSON.
    • header: (Use the header command instead for HTTP header format).

    Example usage:

    # Fetch bare token
    $ oauth2l fetch userinfo.email cloud-platform
    
    # Fetch JSON metadata
    $ oauth2l fetch -f json userinfo.email cloud-platform
    $ oauth2l fetch userinfo.email cloud-platform
  10. Inspect and test tokens with oauth2l info and test

    master

    oauth2l info

    Prints metadata about a token, including expiration time and scopes. If the token has userinfo.email or plus.me scopes, it also prints the authenticated email address.

    $ oauth2l info --token <TOKEN>
    {
        "expires_in": 3599,
        "scope": "https://www.googleapis.com/auth/pubsub",
        "email": "user@gmail.com"
    }

    oauth2l test

    Validates a token and returns an exit code (0 for valid, 1 for invalid). This is ideal for shell pipelines.

    $ oauth2l test --token <TOKEN>
    0
    $ echo $?
    0
    oauth2l info --token $(oauth2l fetch --scope pubsub)
  11. Generate JWT tokens for Service Accounts

    master

    When using a Service Account key file with the --json option, you can use the --jwt flag to generate a JWT token signed by the service account's private key.

    When using --jwt, you do not provide a list of scopes; instead, you must provide a single JWT audience (e.g., the URL of the service you are accessing).

    $ oauth2l fetch --jwt --json ~/service_account.json https://pubsub.googleapis.com/google.pubsub.v1.Publisher
    oauth2l fetch --jwt --json ~/service_account.json https://pubsub.googleapis.com/google.pubsub.v1.Publisher