|
1 | | -# Helper Methods |
| 1 | +# Build your own auth library |
| 2 | + |
| 3 | +Leaf Auth already provides everything you need to get a full authentication system up and running. However, if you want to build your own custom authentication system from the ground up, Leaf provides you with the tools to do that. |
| 4 | + |
| 5 | +## Working with JWTs |
| 6 | + |
| 7 | +According to the JWT docs, JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm) or a public/private key pair using RSA or ECDSA. |
| 8 | + |
| 9 | +In authentication, you can use JWTs as an identifier that a user has successfully logged in. The JWT usually also contains information about the authenticated user, usually the user's id. Leaf auth has helpers for interacting with JWTs. We'll take a look at them in this document. |
| 10 | + |
| 11 | +## Generating JWTs |
| 12 | + |
| 13 | +Leaf provides multiple ways for generating JWTs. |
| 14 | + |
| 15 | +- ### Using the `generateSimpleToken` method |
| 16 | + |
| 17 | + The simplest method is to use the `generateSimpleToken` method on the `Leaf\Helpers\Authentication` class. This method takes in 3 parameters: |
| 18 | + |
| 19 | + - The user id of the currently authenticated user |
| 20 | + - The jwt secret string |
| 21 | + - The jwt expiry time in seconds |
| 22 | + |
| 23 | + ```php |
| 24 | + use Leaf\Helpers\Authentication; |
| 25 | + |
| 26 | + // ... your application code |
| 27 | + |
| 28 | + Authentication::generateSimpleToken( |
| 29 | + $userId, |
| 30 | + 'MY_JWT_SECRET_STRING', |
| 31 | + time() + 3600 |
| 32 | + ); |
| 33 | + ``` |
| 34 | + |
| 35 | +- ### Using the `generateToken` method |
| 36 | + |
| 37 | + The `generateToken` method on the `Leaf\Helpers\Authentication` class is a more advanced method for generating JWTs. It takes in 2 parameters: |
| 38 | + |
| 39 | + - An array of claims to be added to the JWT |
| 40 | + - The JWT secret string |
| 41 | + |
| 42 | + ```php |
| 43 | + use Leaf\Helpers\Authentication; |
| 44 | + |
| 45 | + // ... your application code |
| 46 | + |
| 47 | + Authentication::generateToken( |
| 48 | + [ |
| 49 | + 'user_id' => $userId, |
| 50 | + 'iat' => time(), |
| 51 | + 'iss' => 'localhost', |
| 52 | + 'exp' => time() + 3600, |
| 53 | + ], |
| 54 | + 'MY_JWT_SECRET_STRING' |
| 55 | + ); |
| 56 | + ``` |
| 57 | + |
| 58 | +## Getting JWTs from the request |
| 59 | + |
| 60 | +Tokens from authenticated requests are usually sent in the `Authorization` header. Leaf Auth provides helpers that allow you directly pull in tokens from the request header. |
| 61 | + |
| 62 | +### Using the `getAuthorizationHeader` method |
| 63 | + |
| 64 | +Leaf Auth provides the `getAuthorizationHeader` helper method for getting the token from the request header. It returns null if no authorization header is found. |
| 65 | + |
| 66 | +```php |
| 67 | +use Leaf\Helpers\Authentication; |
| 68 | + |
| 69 | +$header = Authentication::getAuthorizationHeader(); |
| 70 | +``` |
| 71 | + |
| 72 | +### Using the `getBearerToken` method |
| 73 | + |
| 74 | +Leaf Auth provides the `getBearerToken` helper method for getting the token from the request header. It returns null if no bearer token is found. |
| 75 | + |
| 76 | +```php |
| 77 | +use Leaf\Helpers\Authentication; |
| 78 | + |
| 79 | +$token = Authentication::getBearerToken(); |
| 80 | +``` |
| 81 | + |
| 82 | +## Verifying JWTs |
| 83 | + |
| 84 | +Verifying JWTs is a very important step in the authentication process. Tokens can be tampered with, they can also expire, or be issued by an untrusted source. For this reason, you should always verify the token before using it. |
| 85 | + |
| 86 | +Leaf Auth provides 2 methods for verifying JWTs. |
| 87 | + |
| 88 | +- ### Using the `validateToken` method |
| 89 | + |
| 90 | +This method automatically checks if there is a token in the request header, and if there is, it verifies the token. It returns the decoded token if the token is valid, and returns false if the token is invalid. |
| 91 | + |
| 92 | +It takes in a single parameter, which is the JWT secret string. This secret string should be the same as the one used to generate the token. |
| 93 | + |
| 94 | +```php |
| 95 | +use Leaf\Helpers\Authentication; |
| 96 | + |
| 97 | +$data = Authentication::validateToken($secret); |
| 98 | +``` |
| 99 | + |
| 100 | +Note that this method will automatically return false if there is no token in the request header. You can use the `errors` method to get the error message which is why the token validation failed. |
| 101 | + |
| 102 | +```php |
| 103 | +use Leaf\Helpers\Authentication; |
| 104 | + |
| 105 | +$data = Authentication::validateToken($secret); |
| 106 | + |
| 107 | +if (!$data) { |
| 108 | + $errors = Authentication::errors(); |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +- ### Using the `validate` method |
| 113 | + |
| 114 | +Unlike the `validateToken` method, this method does not automatically check for a token in the request header. It takes in 2 parameters: |
| 115 | + |
| 116 | +- The token to be validated |
| 117 | +- The JWT secret string |
| 118 | + |
| 119 | +```php |
| 120 | +use Leaf\Helpers\Authentication; |
| 121 | + |
| 122 | +$token = Authentication::getBearerToken(); |
| 123 | +$data = Authentication::validate($token, $secret); |
| 124 | +``` |
| 125 | + |
| 126 | +Just like the `validateToken` method, this method returns the decoded token if the token is valid, and returns false if the token is invalid. You can use the `errors` method to get the error message which is why the token validation failed. |
| 127 | + |
| 128 | +```php |
| 129 | +use Leaf\Helpers\Authentication; |
| 130 | + |
| 131 | +$token = Authentication::getBearerToken(); |
| 132 | +$data = Authentication::validate($token, $secret); |
| 133 | + |
| 134 | +if (!$data) { |
| 135 | + $errors = Authentication::errors(); |
| 136 | +} |
| 137 | +``` |
0 commit comments