Skip to content

Commit 400e7f4

Browse files
authored
Merge pull request #7 from SoapBox/changes/update-documentation
[Changes] Update Documentation
2 parents 5789db0 + f3a51f8 commit 400e7f4

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

readme.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,24 @@ php artisan vendor:publish
3333
You will need to set the following details in your environment:
3434

3535
```sh
36+
SIGNED_REQUEST_ALGORITHM=
37+
SIGNED_REQUEST_CACHE_PREFIX=
3638
SIGNED_REQUEST_SIGNATURE_HEADER=
3739
SIGNED_REQUEST_ALGORITHM_HEADER=
3840
SIGNED_REQUEST_KEY=
41+
SIGNED_REQUEST_ALLOW_REPLAYS=
42+
SIGNED_REQUEST_TOLERANCE_SECONDS=
3943
```
4044

41-
The `SIGNED_REQUEST_SIGNATURE_HEADER` should be the request header that the signature will be included on, something like `X-SIGNATURE`. Similarly the `SIGNED_REQUEST_ALGORITHM_HEADER` should be the request header that the includes the algorithm used to sign the request. Finally the `SIGNED_REQUEST_KEY` should hold the key used to verify the signed requests.
45+
Each of the settings above allows for a different level of configuration.
46+
47+
- `SIGNED_REQUEST_ALGORITHM` is the algorithm that will be used to generate / verify the signature. This is defaulted to use `sha256` feel free to change this to anything that `hash_hmac` accepts.
48+
- `SIGNED_REQUEST_CACHE_PREFIX` is the prefix to use for all the cache keys that will be generated. Here you can use the default if you're not planning on sharing a cache between multiple applications.
49+
- `SIGNED_REQUEST_SIGNATURE_HEADER` should be the request header that the signature will be included on, `X-Signature` will be used by default.
50+
- `SIGNED_REQUEST_ALGORITHM_HEADER` should be the request header that the includes the algorithm used to sign the request.
51+
- `SIGNED_REQUEST_KEY` is the shared secret key between the application generating the requests, and the application consuming them. This value should not be publically available.
52+
- `SIGNED_REQUEST_ALLOW_REPLAYS` allows you to enable or disable replay attacks. By default replays are disabled.
53+
- `SIGNED_REQUEST_TOLERANCE_SECONDS` is the number of seconds that a request will be considered for. This setting allows for some time drift between servers and is only used when replays are disabled.
4254

4355
### Setup the Middleware
4456

@@ -57,3 +69,43 @@ Route::get('/fire', function () {
5769
return "You'll only see this if the signature of the request is valid!";
5870
})->middleware('verify-signature');
5971
```
72+
73+
### Signing Postman Requests
74+
75+
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:
76+
77+
```js
78+
function guid() {
79+
function s4() {
80+
return Math.floor((1 + Math.random()) * 0x10000)
81+
.toString(16)
82+
.substring(1);
83+
}
84+
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
85+
s4() + '-' + s4() + s4() + s4();
86+
}
87+
88+
postman.setEnvironmentVariable("x-signed-id", guid());
89+
postman.setEnvironmentVariable("x-signed-timestamp", (new Date()).toUTCString());
90+
postman.setEnvironmentVariable("x-algorithm", "sha256");
91+
92+
var payload = {
93+
"id": postman.getEnvironmentVariable("x-signed-id"),
94+
"method": request.method,
95+
"timestamp": postman.getEnvironmentVariable("x-signed-timestamp"),
96+
"uri": request.url.replace("{{url}}", postman.getEnvironmentVariable("url")),
97+
"content": (Object.keys(request.data).length === 0) ? "" : request.data
98+
};
99+
100+
var hash = CryptoJS.HmacSHA256(JSON.stringify(payload), postman.getEnvironmentVariable("key"));
101+
var signature = hash.toString();
102+
103+
postman.setEnvironmentVariable("x-signature", signature);
104+
```
105+
106+
Note for this to work you'll have to setup your environment to have the following variables:
107+
108+
- `{{url}}` this is the base url to the api you'll be hitting.
109+
- `{{key}}` this is the shared secret key you'll be using in your environment.
110+
111+
All other environment variables that will be needed will be automatically generated by the above script.

0 commit comments

Comments
 (0)