From 90031085da401b019cd402fc7508989c5dfabf7e Mon Sep 17 00:00:00 2001 From: Lars Grevelink Date: Wed, 10 Jun 2020 12:47:51 +0200 Subject: [PATCH] Update README --- README.md | 29 +++++++++++++++++++++++++++++ tests/TokenBlueprintTest.php | 6 ++++++ 2 files changed, 35 insertions(+) diff --git a/README.md b/README.md index cd1e3b9..2c62e56 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/tests/TokenBlueprintTest.php b/tests/TokenBlueprintTest.php index f228e27..ae71592 100644 --- a/tests/TokenBlueprintTest.php +++ b/tests/TokenBlueprintTest.php @@ -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()