Skip to content

Commit 0b14ca8

Browse files
committed
Now works on JSON body API calls too
Body must be minified so signature matches body
1 parent 5cad429 commit 0b14ca8

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,38 @@ Provide the values manually or from your private environment variables:
6161

6262
![request example](./.github/assets/insomnia-edit-tag.png)
6363

64+
**NB:** Ignore the Live Preview error
65+
6466
The plugin will generate and add the headers:
6567

6668
- `X-Ovh-Timestamp`
6769
- `X-Ovh-Signature`
70+
71+
## Important Minification in JSON Body Text
72+
73+
Because of the way `PUT` and `POST` methods need the JSON body included in the signature, currently the body of your request needs to be minified with no spaces, for example when editing a credential with `PUT` on the `/1.0/me/api/credential/<credentialId>` endpoint:
74+
75+
The body might be like this, and [must be fully minified](https://stackoverflow.com/a/48487241).
76+
77+
```json
78+
{"allowedIPs":["127.0.0.1/32","127.0.0.2/32"]}
79+
```
80+
81+
This results in the insomnia request Timeline being:
82+
83+
```
84+
> PUT /1.0/me/api/credential/553184188 HTTP/1.1
85+
> Host: api.ovh.com
86+
> User-Agent: insomnia/2022.6.0
87+
> Content-Type: application/json
88+
> Accept: application/json
89+
> X-Ovh-Application: 1234567890abcdef
90+
> X-Ovh-Consumer: 0987654321defabc
91+
> X-Ovh-Timestamp: 1667135223
92+
> X-Ovh-Signature: $1$0987654321123456789009876543211234567890
93+
> Content-Length: 51
94+
95+
| {"allowedIPs":["127.0.0.1/32","127.0.0.2/32"]}
96+
```
97+
98+
If there are spaces in your JSON body, the plugin currently can't generate a matching signature. A future TODO for sure.

plugin.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports.templateTags = [
44
{
55
name: 'ovhsignature',
66
displayName: 'OVH Signature',
7-
description: 'Sign OVH CLoud API requests',
7+
description: 'Sign OVH Cloud API requests',
88

99
args: [
1010
{
@@ -33,19 +33,17 @@ module.exports.templateTags = [
3333
]
3434

3535
function signOvhRequest(as, ck, httpMethod, url, body, timestamp) {
36-
let signed = [ as, ck, httpMethod, url, '', timestamp ];
37-
return '$1$' + crypto.createHash('sha1').update(signed.join('+')).digest('hex');
36+
if (typeof(body) === 'object' && Object.keys(body).length > 0) {
37+
body = JSON.stringify(body, null)
38+
}
39+
let signed = [ as, ck, httpMethod, url, body, timestamp ];
40+
return '$1$' + crypto.createHash('sha1').update(signed.join('+')).digest('hex');
3841
}
3942

4043
module.exports.requestHooks = [async (context) => {
41-
console.log("Inserting X-Ovh-Signature into http headers")
4244
const as = await context.store.getItem("ovhcloud-as")
43-
console.log("as " + as)
4445
const ck = await context.store.getItem("ovhcloud-ck")
45-
console.log("ck " + ck)
4646
const requestUrl = context.request.getUrl();
47-
console.log("requestUrl " + requestUrl)
48-
console.log("requestBody " + requestBody);
4947
const httpMethod = context.request.getMethod().toUpperCase();
5048
const body = context.request.getBody()
5149
let requestBody = '';
@@ -56,7 +54,9 @@ module.exports.requestHooks = [async (context) => {
5654
}
5755
}
5856
const timestamp = Math.round(Date.now() / 1000)
57+
console.log("Inserting X-Ovh-Timestamp into http headers")
5958
context.request.setHeader("X-Ovh-Timestamp", timestamp);
6059
const signature = signOvhRequest(as, ck, httpMethod, requestUrl, requestBody, timestamp)
60+
console.log("Inserting X-Ovh-Signature into http headers")
6161
context.request.setHeader("X-Ovh-Signature", signature);
6262
}]

0 commit comments

Comments
 (0)