Skip to content

Commit e093716

Browse files
committed
Add a section for setting up postman
1 parent f744769 commit e093716

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

readme.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,42 @@ Route::get('/fire', function () {
6868
return "You'll only see this if the signature of the request is valid!";
6969
})->middleware('verify-signature');
7070
```
71+
72+
### Signing Postman Requests
73+
74+
If you, like us, like to use [postman](https://www.getpostman.com/) to share your api internally you can use the following pre-request script to automatically sign your postman requests:
75+
76+
```js
77+
function guid() {
78+
function s4() {
79+
return Math.floor((1 + Math.random()) * 0x10000)
80+
.toString(16)
81+
.substring(1);
82+
}
83+
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
84+
s4() + '-' + s4() + s4() + s4();
85+
}
86+
87+
postman.setEnvironmentVariable("x-signed-id", guid());
88+
postman.setEnvironmentVariable("x-signed-timestamp", (new Date()).toUTCString());
89+
postman.setEnvironmentVariable("x-algorithm", "sha256");
90+
91+
var payload = {
92+
"id": postman.getEnvironmentVariable("x-signed-id"),
93+
"method": request.method,
94+
"timestamp": postman.getEnvironmentVariable("x-signed-timestamp"),
95+
"uri": request.url.replace("{{url}}", postman.getEnvironmentVariable("url")),
96+
"content": (Object.keys(request.data).length === 0) ? "" : request.data
97+
};
98+
99+
var hash = CryptoJS.HmacSHA256(JSON.stringify(payload), postman.getEnvironmentVariable("key"));
100+
var signature = hash.toString();
101+
102+
postman.setEnvironmentVariable("x-signature", signature);
103+
```
104+
105+
Note for this to work you'll have to setup your environment to have the following variables:
106+
- `{{url}}` this is the base url to the api you'll be hitting.
107+
- `{{key}}` this is the shared secret key you'll be using in your environment.
108+
109+
All other environment variables that will be needed will be automatically generated by the above script.

0 commit comments

Comments
 (0)