You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/includes/wp-api-v1/_authentication.md
+34-20
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
# Authentication #
2
2
3
-
WooCommerce includes by default two ways to authenticate with the WP REST API, depending on whether the site supports SSL. It's still possible to use any [WP REST API authetication](http://v2.wp-api.org/guide/authentication/) plugin or method too.
3
+
WooCommerce includes two ways to authenticate with the WP REST API. In addition, it is possible to use any [WP REST API authentication](http://v2.wp-api.org/guide/authentication/) plugin or method too.
4
4
5
5
### Over HTTPS ###
6
6
7
-
You may use [HTTP Basic Auth](http://en.wikipedia.org/wiki/Basic_access_authentication) by providing the REST API Consumer Key as the username and the REST API Consumer Secret as the password.
7
+
You may use [HTTP Basic Auth](http://en.wikipedia.org/wiki/Basic_access_authentication) by providing the REST API Consumer Key as the username and the REST API Consumer Secret as the password.
Occasionally some servers may not parse the Authorization header correctly (if you see a "Consumer key is missing" error when authenticating over SSL, you have a server issue). In this case, you may provide the consumer key/secret as query string parameters.
16
+
Occasionally some servers may not parse the Authorization header correctly (if you see a "Consumer key is missing" error when authenticating over SSL, you have a server issue). In this case, you may provide the consumer key/secret as query string parameters instead.
17
17
18
18
> Example for servers that not properly parse the Authorization header:
You must use [OAuth 1.0a "one-legged" authentication](http://tools.ietf.org/html/rfc5849) to ensure REST API credentials cannot be intercepted. Typically you will use any standard OAuth 1.0a library in the language of choice to handle the authentication, or generate the necessary parameters by following the following instructions.
26
+
You must use [OAuth 1.0a "one-legged" authentication](http://tools.ietf.org/html/rfc5849) to ensure REST API credentials cannot be intercepted by an attacker. Typically you will use any standard OAuth 1.0a library in the language of your choice to handle the authentication, or generate the necessary parameters by following the following instructions.
27
27
28
28
#### Generating an OAuth signature ####
29
29
30
-
1) Set the HTTP method for the request:
30
+
1) Set the HTTP method for the request to `GET`
31
31
32
-
`GET`
32
+
2) Set your base request URI -- this is the full request URI without query string parameters and URL encode according to RFC 3986.
33
33
34
-
2) Set your base request URI -- this is the full request URI without query string parameters -- and URL encode according to RFC 3986:
3) Collect and normalize your query string parameters. This includes all `oauth_*` parameters except for the signature itself. Parameters should be normalized using URL encoding according to RFC 3986 (use `rawurlencode` in PHP) and percent(`%`) characters should be double-encoded (e.g. `%` becomes `%25`.
3) Collect and normalize your query string parameters. This includes all `oauth_*` parameters except for the signature. Parameters should be normalized by URL encoding according to RFC 3986 (`rawurlencode` in PHP) and percent(`%`) characters should be double-encoded (e.g. `%` becomes `%25`.
50
+
> PHP sorting example:
43
51
44
-
4) Sort the parameters in byte-order (`uksort( $params, 'strcmp' )` in PHP)
52
+
```
53
+
uksort( $params, 'strcmp' )
54
+
```
45
55
46
-
5) Join each parameter with an encoded equals sign (`%3D`):
56
+
5) Join each parameter with an encoded equals sign (`%3D`) and each key/value pair with an encoded ampersand (`%26`)
47
57
48
-
`oauth_signature_method%3DHMAC-SHA1`
58
+
> Parameters example:
49
59
50
-
6) Join each parameter key/value with an encoded ampersand (`%26`):
8) Generate the signature using the string to key and your consumer secret key
72
+
7) Generate the signature using the string to key and your consumer secret key
59
73
60
74
If you are having trouble generating a correct signature, you'll want to review the string you are signing for encoding errors. The [authentication source](https://github.com/woothemes/woocommerce/blob/master/includes/api/class-wc-rest-authentication.php#L141) can also be helpful in understanding how to properly generate the signature.
61
75
62
76
#### OAuth Tips ####
63
77
64
78
* The OAuth parameters must be added as query string parameters and *not* included in the Authorization header. This is because there is no reliable cross-platform way to get the raw request headers in WordPress.
65
-
* The require parameters are: `oauth_consumer_key`, `oauth_timestamp`, `oauth_nonce`, `oauth_signature`, and `oauth_signature_method`. `oauth_version` is not required and should be omitted.
79
+
* The required parameters are: `oauth_consumer_key`, `oauth_timestamp`, `oauth_nonce`, `oauth_signature`, and `oauth_signature_method`. `oauth_version` is not required and should be omitted.
66
80
* HMAC-SHA1 or HMAC-SHA256 are the only accepted hash algorithms.
67
81
* The OAuth nonce can be any randomly generated 32 character (recommended) string that is unique to the consumer key. Read more suggestions on [generating nonces on the Twitter REST API forums](https://dev.twitter.com/discussions/12445).
68
82
* The OAuth timestamp should be the unix timestamp at the time of the request. The REST API will deny any requests that include a timestamp outside of a 15 minute window to prevent replay attacks.
0 commit comments