Skip to content

Commit 3dc03ca

Browse files
lornajanemheap
authored andcommitted
Verify message signatures (#154)
The code was already in the codebase but needed a tiny tweak and some documentation so people can find it. Fixes #90
1 parent dee243b commit 3dc03ca

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ echo "The body of the message was: " . $message->getBody();
167167

168168
### Signing a Message
169169

170+
_You may also like to read the [documentation about message signing](https://developer.nexmo.com/concepts/guides/signing-messages)._
171+
170172
The SMS API supports the ability to sign messages by generating and adding a signature using a "Signature Secret" rather than your API secret. The algorithms supported are:
171173

172174
* `md5hash1`
@@ -180,11 +182,26 @@ Both your application and Nexmo need to agree on which algorithm is used. In the
180182
Create a client using these credentials and the algorithm to use, for example:
181183

182184
```php
183-
$client = new Nexmo\Client(new Nexmo\Client\Credentials\SignatureSecret(API_KEY, API_SECRET, 'sha256'));
185+
$client = new Nexmo\Client(new Nexmo\Client\Credentials\SignatureSecret(API_KEY, SIGNATURE_SECRET, 'sha256'));
184186
```
185187

186188
Using this client, your SMS API messages will be sent as signed messages.
187189

190+
### Verifying an Incoming Message Signature
191+
192+
_You may also like to read the [documentation about message signing](https://developer.nexmo.com/concepts/guides/signing-messages)._
193+
194+
If you have message signing enabled for incoming messages, the SMS webhook will include the fields `sig`, `nonce` and `timestamp`. To verify the signature is from Nexmo, you create a Signature object using the incoming data, your signature secret and the signature method. Then use the `check()` method with the actual signature that was received (usually `_GET['sig']`) to make sure that it is correct.
195+
196+
```php
197+
$signature = new \Nexmo\Client\Signature($_GET, SIGNATURE_SECRET, 'sha256');
198+
199+
// is it valid? Will be true or false
200+
$isValid = $signature->check($_GET['sig']);
201+
```
202+
203+
Using your signature secret and the other supplied parameters, the signature can be calculated and checked against the incoming signature value.
204+
188205
### Starting a Verification
189206

190207
Nexmo's [Verify API][doc_verify] makes it easy to prove that a user has provided their own phone number during signup,

src/Client/Signature.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected function sign($signatureMethod, $data, $secret) {
6767
case 'sha1':
6868
case 'sha256':
6969
case 'sha512':
70-
return hash_hmac($signatureMethod, $data, $secret);
70+
return strtoupper(hash_hmac($signatureMethod, $data, $secret));
7171
break;
7272
default:
7373
throw new Exception('Unknown signature algorithm: '.$signatureMethod.'. Expected: md5hash, md5, sha1, sha256, or sha512');
@@ -107,7 +107,12 @@ public function getSignedParams()
107107
/**
108108
* Check that a signature (or set of parameters) is valid.
109109
*
110-
* @param array| string $signature
110+
* First instantiate a Signature object: this will drop any supplied
111+
* signature parameter and calculate the correct one. Then call this
112+
* method and supply the signature that came in with the request.
113+
*
114+
* @param array| string $signature The incoming sig parameter to check
115+
* (or all incoming params)
111116
* @return bool
112117
* @throws \InvalidArgumentException
113118
*/
@@ -121,7 +126,7 @@ public function check($signature)
121126
throw new \InvalidArgumentException('signature must be string, or present in array or parameters');
122127
}
123128

124-
return $signature == $this->signed['sig'];
129+
return strtolower($signature) == strtolower($this->signed['sig']);
125130
}
126131

127132
/**

test/Client/SignatureTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ public function testHmacSignature($algorithm, $expected){
4242
public function hmacSignatureProvider() {
4343
$data = [];
4444

45-
$data['md5'] = ['md5', '51cdafebb4bbce9525b195c1617cb8d2'];
46-
$data['sha1'] = ['sha1', '0162aec64bc183b2e1256545951fe5639dc98020'];
47-
$data['sha256'] = ['sha256', '9fec5ef6d0f2b3d2bb7558b6e4042569823cab9ea0dd30503472b7b304601975'];
48-
$data['sha512'] = ['sha512', '40bd12b9a4b6000ad1138eefd24ffe9fbd72aee13c3fa04b32bb69dbc256ad0a04a463b1a9af6660d10f6e1e769ee14b9cff6a635502e93afcd0bfab29f38f87'];
45+
$data['md5'] = ['md5', '51CDAFEBB4BBCE9525B195C1617CB8D2'];
46+
$data['sha1'] = ['sha1', '0162AEC64BC183B2E1256545951FE5639DC98020'];
47+
$data['sha256'] = ['sha256', '9FEC5EF6D0F2B3D2BB7558B6E4042569823CAB9EA0DD30503472B7B304601975'];
48+
$data['sha512'] = ['sha512', '40BD12B9A4B6000AD1138EEFD24FFE9FBD72AEE13C3FA04B32BB69DBC256AD0A04A463B1A9AF6660D10F6E1E769EE14B9CFF6A635502E93AFCD0BFAB29F38F87'];
4949

5050
return $data;
5151
}

0 commit comments

Comments
 (0)