Skip to main content

httpie auth plugin for OAuth2.0 client credentials flow.

Project description

httpie-auth-plugin-for-oauth2-client-credentials

As an auth plugin for httpie, it obtains a token with the OAuth2.0 client_credentials flow before executing http, and adds the Authorization: Bearer ${token} header to the executed request.

Token request patterns

Token request patterns are supported for the following:

Installation

pip install httpie-auth-plugin-for-oauth2-client-credentials

Usage

Since the format of the request to get the token depends on the support of the server, this module supports the following three patterns depending on the --token-request-type option.
The SCOPE parameter is optional in all patterns.

Basic authentication (default)

Set CLIENT_ID and CLIENT_SECRET to Basic authentication to get the token.
Since this pattern is the default, you can omit the --token-request-type option.

Execute command:

http --auth-type=oauth2-client-credentials \
     --auth="${CLIENT_ID}:${CLIENT_SECRET}" \
     --token-endpoint="${TOKEN_ENDPOINT_URL}" \
     --token-request-type="basic" \
     --scope="${SCOPE}" \
     ${TARGET_ENDPOINT_URL}

Token request:

POST ${TOKEN_ENDPOINT_URL} HTTP/1.1
Host: ${TOKEN_ENDPOINT_HOST}
Authorization: Basic ${CLIENT_ID:CLIENT_SECRET base64 strings}
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials
&scope=${SCOPE}

Form request (application/x-www-form-urlencoded)

Send CLIENT_ID and CLIENT_SECRET as part of the Form data.

Execute command:

http --auth-type=oauth2-client-credentials \
     --auth="${CLIENT_ID}:${CLIENT_SECRET}" \
     --token-endpoint="${TOKEN_ENDPOINT_URL}" \
     --token-request-type="form" \
     --scope="${SCOPE}" \
     ${TARGET_ENDPOINT_URL}

Token request:

POST ${TOKEN_ENDPOINT_URL} HTTP/1.1
Host: ${TOKEN_ENDPOINT_HOST}
Content-Type: application/x-www-form-urlencoded

client_id=${CLIENT_ID}
&client_secret=${CLIENT_SECRET}
&grant_type=client_credentials
&scope=${SCOPE}

JSON request (application/json)

Sends all request properties as JSON format.

Execute command:

http --auth-type=oauth2-client-credentials \
     --auth="${CLIENT_ID}:${CLIENT_SECRET}" \
     --token-endpoint="${TOKEN_ENDPOINT_URL}" \
     --token-request-type="json" \
     --scope="${SCOPE}" \
     ${TARGET_ENDPOINT_URL}

Token request:

POST ${TOKEN_ENDPOINT_URL} HTTP/1.1
Host: ${TOKEN_ENDPOINT_HOST}
Content-Type: application/json

{
    "client_id": "${client_id}",
    "client_secret": "${client_secret}",
    "grant_type": "client_credentials",
    "scope": "${SCOPE}"
}

Supported .netrc

Supported .netrc.
Please check the httpie documentation for usage instructions.

Important Notes before Use

The value for "machine" in the ".netrc" file is the TARGET_ENDPOINT host, not the TOKEN_ENDPOINT host. It should be TOKEN_ENDPOINT, but the main body of httpie is designed to extract authentication information from the TARGET_ENDPOINT host.

# Create(or add) .netrc file.
cat <<EOF>> ~/.netrc

machine   {TARGET_ENDPOINT_HOST}
login     {Your Client ID}
password  {Your Client Secret}
EOF

# Change permission.
chmod 600 ~/.netrc
# Example request.
http --auth-type=oauth2-client-credentials \
     --token-endpoint="${TOKEN_ENDPOINT_URL}" \
     --token-request-type="form" \
     --scope="${SCOPE}" \
     ${TARGET_ENDPOINT_URL}

Options

  • --print-token-response
    Output the token acquisition response to the console

Note

Token response

The token response must be JSON in the following format.
The format to be given to the Authorization header of the target endpoint is ${token_type} ${access_token}.
If token_type is not included in the response, the default value of the Prefix is Bearer.

{
  "token_type": "Bearer",
  "access_token": "xxxxxxxxxxxx",
  "expires_in": 3599
}

Caution

This plugin does not have a function to cache the token until "expires_in", so it will send a token request every time you execute the http command.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file httpie_auth_plugin_for_oauth2_client_credentials-0.1.0.tar.gz.

File metadata

File hashes

Hashes for httpie_auth_plugin_for_oauth2_client_credentials-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6f52d767af800301b11541738d3444ae158e8a5bd0b0a0e84a793ded1f9b5365
MD5 08b80618922f1c85dfcd8a106227de16
BLAKE2b-256 c1ae487998c93c8d5a987b3ba86d2a4e01ff889d59bbe997998f77bd7a743ee2

See more details on using hashes here.

File details

Details for the file httpie_auth_plugin_for_oauth2_client_credentials-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for httpie_auth_plugin_for_oauth2_client_credentials-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a5cf0c98bd7e26330b19697fb7fc8870485ae92a6601ab25e2a00bcd93a071ef
MD5 3a842ff883e00b110fcdc7e8b0daf487
BLAKE2b-256 3b1c69f6ceeb6bd0a249b7e42cf8a41c7408b4ef55d5e602321cc19ab3c8c5c1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page