Signing Requests | OAuth1 (2024)

One of the hardest parts of working with OAuth 1 is signing requests. It's important to understand the process from the start.

Even once you understand the process, it's recommended you use an existing library for this process. There are a lot of intricacies and edge cases to signing requests that are easy to miss. If you're ever in doubt on any details, the OAuth RFC is the canonical reference on signatures; this is only an easier guide to it.

Request signing in OAuth is a key part of ensuring your application can't be spoofed. This uses a pre-established shared secret only known by the server and the client, which is a key reason why you should keep your credentials secret. This secret is then mixed with the request data and a nonce to ensure the signature can't be used multiple times.

Note for experienced developers: The OAuth plugin only supports HMAC-SHA1 signatures, and PHP-style GET parameters (a[]=1&a[]=2) are treated literally, with the [] included in the parameter names. This may differ from other PHP-powered OAuth servers.

Base String

Before you can create a signature, you need something to sign. The first step is to take the request you're about to send and turn it into a single string. This needs to take into consideration the whole request, so it's generate it as late as possible. Ideally, using an OAuth implementation built into your HTTP client will ensure your base string is accurate.

The base string uses three pieces of data: the HTTP method (GET, POST, etc), the URL (without GET parameters), and any passed parameters. These follow a very specific set of rules, which loosely summarised are:

  • Method: Uppercase HTTP method.
  • URL: Lowercase scheme and host, port excluded if 80 for HTTP or 443 for SSL.
  • Request Parameters: OAuth parameters from Authorization header (excluding oauth_signature itself), GET parameters from the URL, and POST parameters if they're form encoded (a=b&c=d format; not JSON). Encode the name and value for each, sort by name (and value for duplicate keys). Combine key and value with a =, then concatenate with & into a string.

These pieces are then combined by URL-encoding each, then concatenating with & into a single string.

For example, for the following request:

POST /wp-json/wp/v2/postsHost: example.comAuthorization: OAuth oauth_consumer_key="key" oauth_token="token" oauth_signature_method="HMAC-SHA1" oauth_timestamp="123456789", oauth_nonce="nonce", oauth_signature="..."{ "title": "Hello World!"}

The base string pieces are:

  • Method: POST
  • URL: http://example.com/wp-json/wp/v2/posts
  • Params: oauth_consumer_key=key&oauth_nonce=nonce&oauth_signature_method=HMAC-SHA1&oauth_timestamp=123456789&oauth_token=token

The resulting base string would then be:

POST&http%3A%2F%2Fexample.com%2Fwp-json%2Fwp%2Fv2%2Fposts&oauth_consumer_key%3Dkey%26oauth_nonce%3Dnonce%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D123456789%26oauth_token%3Dtoken

Signature Key

The OAuth plugin only supports a single signature method: HMAC-SHA1. This uses a HMAC (Hash-based Message Authentication Code), which looks similar to a normal SHA1 hash, but differs significantly. Importantly, it's immune to length extension attacks. It also needs two pieces: a key and the text to hash. The text is the base string created above.

The signature key for HMAC-SHA1 is created by taking the client/consumer secret and the token secret, URL-encoding each, then concatenating them with & into a string.

This process is always the same, even if you don't have a token yet.

For example, if your client secret is abcd and your token secret is 1234, the key is abcd&1234. If your client secret is abcd, and you don't have a token yet, the key is abcd&.

Signature

Once you have the base string and the signature, you can create the signature itself. The OAuth plugin only supports HMAC-SHA1 signatures, so the signature is always set to the result of HMAC-SHA (key, text).

The HMAC key should be set to the signature key as above, and the HMAC text should be set to the base string. The result of the HMAC hashing is used as the signature.

(The hash should be the base64-encoded digest. Many languages handle this by default, but you may need to base64-encode it manually if not. This should always look like "wOJIO9A2W5mFwDgiDvZbTSMK/PY=", not raw binary data.)

Even if you're writing the signature handling from scratch, the HMAC hashing should always be handled by an existing library. HMAC-SHA1 is built into many languages natively, and libraries are available for basically every other language. Do not write your own code to handle hashing.

For example, in PHP, the hash_hmac function can be used to generate HMAC hashes:

$base_string = 'POST&http...';$key = 'abcd&1234';$signature = hash_hmac( 'sha1', $base_string, $key );

Common Problems

Signatures are without a doubt the hardest part of the OAuth 1 process. If your signature is incorrect, you'll receive a json_oauth1_signature_mismatch. Here's a couple of things that are easy to fix.

Array Parameters

If you're generating your signature in PHP and you have array parameters (that is, a[]=1&a[]=2), you may be generating parameters incorrectly. Some PHP signature implementations incorrectly treat this as a=1&a=2, or may even generate a=Array. Check that your implementation correctly generates these.

JSON Data

When sending data to the REST API, you'll likely be sending JSON data as the body. These parameters should not be included in the base string; the OAuth specification explicitly states that only form-encoded data should be included.

Signing Requests | OAuth1 (2024)

FAQs

Is OAuth1 obsolete? ›

Effective July 1, 2021, OAuth 1.0a will no longer be certified.

How to make an OAuth signature? ›

Creating the signature base string

This is called the signature base string by the OAuth specification. To encode the HTTP method, base URL, and parameter string into a single string: Convert the HTTP Method to uppercase and set the output string equal to this value. Append the '&' character to the output string.

What is OAuth 1.0 and how does it work? ›

OAuth 1.0 support makes it possible for users to share their private resources between sites without providing users and passwords. Private resources can be anything, but common examples include photos, videos, and contact lists.

What is the difference between OAuth and OAuth2? ›

OAuth lets you store its tokens for a year or more while OAuth 2.0 offers access tokens with a short-lived expiration date. These refresh tokens offer better security and reduce the chances of phishing. New tokens can be produced without reauthorizing.

Is OAuth2 more secure than OAuth1? ›

OAuth2 is not necessarily more secure than OAuth1, and using OAuth2 does not inherently lead to better security. Many considerations must go into each specific implementation.

What are the benefits of OAuth2 over OAuth1? ›

Main Benefits Gained from Using OAuth 2

Enhanced Security: OAuth 2.0 eliminates the need for shared secrets between the client and resource server, a potential vulnerability in OAuth 1.0. It relies on access tokens with limited lifespans and refresh tokens for extended access, improving overall security.

Why is a bad idea to use OAuth 2.0 for authentication? ›

Leaking authorization codes and access tokens. Perhaps the most infamous OAuth-based vulnerability is when the configuration of the OAuth service itself enables attackers to steal authorization codes or access tokens associated with other users' accounts.

Is OAuth 2.0 a modern authentication? ›

Modern Authentication or OAuth 2.0 is the new, secure way to connect your Microsoft Outlook email account to third party applications such as EXACT. Gmail are introducing changes in February 2024, the changes affect anyone sending email communications to Gmail accounts.

What is OAuth 2.0 in layman's terms? ›

OAuth 2.0 is an authorization protocol and NOT an authentication protocol. As such, it is designed primarily as a means of granting access to a set of resources, for example, remote APIs or user data. OAuth 2.0 uses Access Tokens.

Is OAuth2 outdated? ›

It states that OAuth 2.0 is deprecated.

Should I use OpenID or OAuth2? ›

If you only need to authorize a client to access resources on behalf of a user, OAuth2 is sufficient. However, if you also need to authenticate the user and get their basic profile information, OpenID Connect is the better choice as it extends OAuth2 with an identity layer.

Are OAuth and JWT the same? ›

OAuth and JWT are both standards for authorization and authentication. OAuth is suitable for delegating user authorization, accessing third-party applications, and session management. JWT is suitable for stateless applications, API authentication, and server-to-server authorization.

Is OAuth2 vulnerable to CSRF? ›

However, since the state parameter is not required for a successful OAuth2 workflow, it is very often this parameter is omitted or ignored during OAuth2 implementation. Without validation on the state parameter, CSRF attack could be launched easily against the client application.

What is the difference between Auth0 and auth2? ›

Auth0 also includes user consent management, single sign-on (SSO), and customizable authentication flows. On the other hand, OAuth2 is an open standard for authorization that defines a framework for secure delegated access to resources. It focuses primarily on granting access tokens and managing permissions.

What was before OAuth? ›

3 Answers. "Back in the day" we didn't really have any standards prior to oauth, everything was hand rolled and custom. (so for example the photobucket API provided its own direct authentication mechanism. ) Usually token based authentication with a API key (or even plain credentials. )

What is OAuth in the rest API? ›

OAuth is primarily designed for authorization. It grants a third-party service access to certain resources associated with a user on another service. It is not designed for authentication, but it can be used to authenticate in some circ*mstances.

Top Articles
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 6126

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.