22
33[ ![ codecov] ( https://codecov.io/gh/WebdevCave/jwt/graph/badge.svg?token=U0OXfWrDJl )] ( https://codecov.io/gh/WebdevCave/jwt )
44
5- ![ JWT Logo] ( https://jwt.io/img/logo-asset.svg )
5+ <div style =" text-align : center " >
6+ <a href =" https://jwt.io/ " target =" _blank " >
7+ <img src =" https://jwt.io/img/logo-asset.svg " >
8+ </a >
9+ </div >
610
711## How to install
812
@@ -29,38 +33,83 @@ composer require webdevcave/jwt
2933 <td>HS512</td>
3034 <td>1.0</td>
3135 </tr>
36+ <tr>
37+ <td>RS256</td>
38+ <td>1.1</td>
39+ </tr>
40+ <tr>
41+ <td>RS384</td>
42+ <td>1.1</td>
43+ </tr>
44+ <tr>
45+ <td>RS512</td>
46+ <td>1.1</td>
47+ </tr>
3248</table >
3349
3450## Provided claim validators
3551<table >
3652 <tr>
3753 <th>Claim</th>
3854 <th>Version</th>
55+ <th>Description</th>
56+ <th>RFC</th>
57+ </tr>
58+ <tr>
59+ <td>aud</td>
60+ <td>1.1</td>
61+ <td>Audience</td>
62+ <td>https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.3</td>
3963 </tr>
4064 <tr>
4165 <td>exp</td>
4266 <td>1.0</td>
67+ <td>Expiration time (timestamp)</td>
68+ <td>https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.4</td>
69+ </tr>
70+ <tr>
71+ <td>iss</td>
72+ <td>1.1</td>
73+ <td>Issuer</td>
74+ <td>https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.1</td>
4375 </tr>
4476 <tr>
4577 <td>nbf</td>
4678 <td>1.0</td>
79+ <td>Not before (timestamp)</td>
80+ <td>https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.5</td>
81+ </tr>
82+ <tr>
83+ <td>sub</td>
84+ <td>1.1</td>
85+ <td>Subject</td>
86+ <td>https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.2</td>
4787 </tr>
4888</table >
4989
90+
91+ - "typ" claim is defined as JWT by default.
92+ - "iat" and "nbf" claims are starts with the current timestamp by default.
93+ - "jti" validator isn't provided but it can be implemented by your application as presented in "Validating your private
94+ claims" section
95+
5096## Basic Usage
5197
52- ### Generating token
98+ ### Generating a token
99+
53100``` php
54101<?php
55102
56103use Webdevcave\Jwt\Token;
57104use Webdevcave\Jwt\SignerFactory;
105+ use \Webdevcave\Jwt\Secrets\HsSecret;
58106
107+ $secret = new HsSecret('your_secret_here');
59108$token = Token::create()
60- ->with('exp', strtotime('+ 1 hour ')) //Expires in one hour
61- ->withSigner(SignerFactory::build('HS256 ')) //HS256 signer is provided by default. This could be omitted
62- ->sign($mySecret )
63- ->toString();
109+ ->withSigner(SignerFactory::build('HS256 ')) //HS256 signer is provided by default. This could be omitted
110+ ->with('exp', strtotime('+ 1 hour ')) //Expires in one hour
111+ ->sign($secret )
112+ ->toString();
64113```
65114
66115### Validating and reading values from a token
@@ -70,15 +119,49 @@ $token = Token::create()
70119use Webdevcave\Jwt\Token;
71120
72121$token = Token::fromString('xxxx.yyyyy.zzzzz');
73-
74- $isValid = $token->validate($mySecret);
122+ $isValid = $token->validate($secret);
75123
76124if ($isValid) {
77125 $payload = $token->getPayload();
78126 $headers = $token->getHeaders();
79127}
80128```
81129
130+ ### RSA Tokens:
131+
132+ First of all, you will need a public/private key pair. If you don't have one, you can generate it easily at the
133+ following page: https://cryptotools.net/rsagen
134+
135+ With your public/private key pair in hand, the process will be similar to the hmac tokens in the above example:
136+
137+ ``` php
138+ <?php
139+
140+ use Webdevcave\Jwt\Token;
141+ use Webdevcave\Jwt\SignerFactory;
142+ use \Webdevcave\Jwt\Secrets\RsSecret;
143+
144+ $secret = new RsSecret('private_key', 'public_key');
145+
146+ //Generate a token string
147+ $tokenString = Token::create()
148+ ->withSigner(SignerFactory::build('RS256'))
149+ ->with('exp', strtotime('+ 1 hour')) //Expires in one hour
150+ ->sign($secret)
151+ ->toString();
152+
153+ //Validating...
154+ $token = Token::fromString($tokenString);
155+ if ($token->validate($secret)) {
156+ //token is valid...
157+ $creationDate = date(DATE_RFC3339, $token->getPayload('iat'));
158+ $expirationDate = date(DATE_RFC3339, $token->getPayload('exp'));
159+
160+ echo "Your token was created at $creationDate.";
161+ echo "It will expire at $expirationDate.";
162+ }
163+ ```
164+
82165### Validating your private claims
83166
84167First you have to create your validator
@@ -124,3 +207,33 @@ if ($isValid) {
124207 $myClaim = $token->getPayload('my-claim');
125208}
126209```
210+
211+ ## Shortcuts
212+
213+ You can get an Token instance directly from the Authorization header or through a query parameter with the following
214+ methods:
215+
216+ ``` php
217+
218+ use Webdevcave\Jwt\Token;
219+
220+ //Load from authorization bearer
221+ $token1 = Token::fromAuthorizationBearer();
222+
223+ //Load from get parameters
224+ $token2 = Token::fromQueryString('token');
225+ $token3 = Token::fromQueryString('token2');
226+ ```
227+
228+ ## Contributing
229+
230+ Contributions are welcome! If you find any issues or have suggestions for improvements,
231+ please open an issue or a pull request on GitHub.
232+
233+ ## License
234+
235+ This project is licensed under the MIT License - see the [ LICENSE] ( LICENSE ) file for details.
236+
237+ ## Credits
238+
239+ Original project can be found [ here] ( https://github.com/corviz/jwt )
0 commit comments