Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsGrevelink committed Jun 10, 2020
1 parent 4f419b8 commit 9003108
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,35 @@ $token->verify(new RsaSha256(null, $publicKey));
// true
```

### Through blueprints

The signature can also be attached to a `TokenBlueprint` to keep everything contained in the blueprint instead of somewhere in application code.

```php
use LGrevelink\SimpleJWT\TokenBlueprint;
use LGrevelink\SimpleJWT\TokenSignature;

class MyToken extends TokenBlueprint
{
// ...

public function signature($key) {
return new TokenSignature(new HmacSha(), md5($key));
}
}

$token = MyToken::generate([
'custom-claim' => 'data'
])->signature(MyToken::signature('some-key'));

// or

$token = MyToken::generateAndSign([
'custom-claim' => 'data'
], 'some-key');
```

All arguments after the custom claims passed to `TokenBlueprint::generateAndSign` will be proxied to the `TokenBlueprint::signature` method so they can be used there.

## Parsing tokens

Expand Down
6 changes: 6 additions & 0 deletions tests/TokenBlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ public function testGenerateAndSign()
], 'custom-key');

$this->assertSame($token->toString(), 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhZGRpdGlvbmFsIjoiY2xhaW0ifQ.C7bk4bT1GtU3q8ByI6dqhelAqEEzf4FqOpQjksUgsOo');

$tokenVerification = SignatureBlueprintMock::generate([
'additional' => 'claim',
])->signature(SignatureBlueprintMock::signature('custom-key'));

$this->assertSame($token->toString(), $tokenVerification->toString());
}

public function testSignature()
Expand Down

0 comments on commit 9003108

Please sign in to comment.