Skip to content
This repository was archived by the owner on Aug 7, 2023. It is now read-only.

Commit 2cb9771

Browse files
Update doc [skip ci].
1 parent 0b1f312 commit 2cb9771

File tree

5 files changed

+183
-3
lines changed

5 files changed

+183
-3
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/assets export-ignore
44
/tests export-ignore
5+
/src/Resources/doc export-ignore
56
/.gitattributes export-ignore
67
/.github export-ignore
78
/.gitignore export-ignore

README.md

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77

88
## About
99

10-
This bundle provides JWT authentication for request forwarded by Istio sidecar.
10+
The Symfony bundle provides JWT authentication for request forwarded by Istio sidecar.
1111

12-
> To use this bundle, ensure your application container had injected Istio sidecar and Istio [RequestAuthentication](https://istio.io/latest/docs/reference/config/security/request_authentication/) CRD had configured, if not your application **IS NOT SECURE**.
12+
> To use this bundle, make sure your K8S application pod had injected Istio sidecar and [RequestAuthentication](https://istio.io/latest/docs/reference/config/security/request_authentication/) CRD had configured, if not your application **IS NOT SECURE**.
13+
14+
The difference between this bundle and the awesome [Lexik JWT Authentication](https://github.com/lexik/LexikJWTAuthenticationBundle) bundle is it's **NOT** validate JWT token because Istio sidecar proxy had validated before forward request to your application,
15+
so that your application don't need to hold public key and double validate JWT token.
1316

1417
## Requirements
1518

@@ -27,9 +30,80 @@ Symfony versions:
2730
composer require php-istio/jwt-authentication-bundle
2831
```
2932

30-
## Document
33+
## Configuration
34+
35+
Enable [the authenticator manager](https://symfony.com/doc/current/security/authenticator_manager.html) setting:
36+
37+
```yaml
38+
# config/packages/security.yaml
39+
security:
40+
enable_authenticator_manager: true
41+
# ...
42+
```
43+
44+
Configure your `config/packages/security.yaml`:
45+
46+
```yaml
47+
security:
48+
enable_authenticator_manager: true
49+
access_control:
50+
- path: ^/
51+
roles: IS_AUTHENTICATED_FULLY
52+
firewalls:
53+
#...
54+
main:
55+
stateless: true
56+
istio_jwt_authenticator:
57+
- issuer: issuer_1 # Required
58+
user_identifier_claim: sub #Default is `sub` claim
59+
origin_token_headers: [authorization] #Required at least once of `origin_token_headers`, `origin_token_query_params` or `base64_headers`. Use this option when your Istio JWTRule CRD using `forwardOriginalToken`.
60+
origin_token_query_params: [token] #Use this option when your Istio JWTRule CRD using `forwardOriginalToken` and your JWT token in query param.
61+
base64_headers: [x-istio-jwt-payload] # Use this option when your Istio JWTRule CRD using `outputPayloadToHeader`.
62+
```
63+
64+
In case your application have multi issuers:
65+
66+
```yaml
67+
#....
68+
main:
69+
stateless: true
70+
istio_jwt_authenticator:
71+
- issuer: issuer_1
72+
origin_token_headers: [authorization]
73+
- issuer: issuer_2
74+
user_identifier_claim: aud
75+
base64_headers: [x-istio-jwt-payload]
76+
#....
77+
```
78+
79+
80+
## Usages
81+
82+
Generate mock JWT token forwarded by Istio sidecar:
83+
84+
```shell
85+
payload='{"issuer":"issuer_1", "sub": "test"}'; \
86+
base64_payload=$(echo -n $payload | base64 -); \
87+
origin_token=$(echo "header.$base64_payload.signature")
88+
```
89+
90+
You can test authenticate origin token with curl:
91+
92+
```shell
93+
curl -H "Authorization: $origin_token" http://localhost/
94+
```
95+
96+
Or authenticate base64 payload header:
97+
98+
```shell
99+
curl -H "X-Istio-JWT-Payload: $base64_header" http://localhost/
100+
```
31101

102+
## Further readings:
32103

104+
+ [Get JWT payload of authenticated user](src/Resources/doc/stateless-user-provider.md)
105+
+ [Use stateless user provider](src/Resources/doc/stateless-user-provider.md)
106+
+ [Create custom user provider](src/Resources/doc/create-custom-user-provider.md)
33107

34108
## Credits
35109

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Create custom user provider
2+
=============================
3+
4+
In case [stateless user provider](stateless-user-provider.md) not fit to your requirements, you can create your own [custom user provider](https://symfony.com/doc/current/security/user_provider.html#creating-a-custom-user-provider)
5+
implement [JWTPayloadAwareUserProviderInterface](/src/User/JWTPayloadAwareUserProviderInterface.php)
6+
when you want to create user instance depend on JWT payload.
7+
This interface base on Symfony `UserProviderInterface` just add more optional arg `$payload` to `loadUserByIdentifier` method.
8+
9+
Configuring the user provider
10+
-----------------------------
11+
Config custom user provider in `security.yaml`:
12+
13+
```yaml
14+
# config/packages/security.yaml
15+
security:
16+
providers:
17+
jwt:
18+
id: App\Security\UserProvider # your user provider service id, change it if you want.
19+
```
20+
21+
#### Sample implementation
22+
23+
```php
24+
namespace App\Security;
25+
26+
use Istio\Symfony\JWTAuthentication\User\JWTPayloadAwareUserProviderInterface;
27+
28+
final class UserProvider implements JWTPayloadAwareUserProviderInterface {
29+
30+
//....
31+
public function loadUserByIdentifier(string $identifier, array $payload = null) {
32+
// use $identifier and $payload to create `UserInterface` instance.
33+
}
34+
35+
}
36+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Get JWT payload of authenticated user
2+
=============================
3+
4+
You can get JWT payload of authenticated user via `jwt_payload` attribute in security token.
5+
6+
#### Sample:
7+
8+
```php
9+
<?php
10+
11+
namespace App\Services;
12+
13+
use Symfony\Component\Security\Core\Security;
14+
15+
class MyService {
16+
17+
public function __construct(Security $security) {
18+
$security->getToken()->getAttribute('jwt_payload');
19+
}
20+
21+
}
22+
```
23+
24+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
A stateless user provider
2+
=============================
3+
4+
This feature inspired by the awesome [Lexik JWT Authentication](https://github.com/lexik/LexikJWTAuthenticationBundle) bundle.
5+
6+
Stateless user provider help to create user instances from the JWT payload.
7+
8+
Configuring the user provider
9+
-----------------------------
10+
11+
First, you need to config `istio_jwt_stateless` provider in `security.yaml`:
12+
13+
```yaml
14+
# config/packages/security.yaml
15+
security:
16+
providers:
17+
jwt:
18+
istio_jwt_stateless:
19+
class: App\Security\User # your user class, you can change it if you want.
20+
```
21+
22+
Then, create a user class `istio_jwt_stateless.class` had set in config, in this case is `App\Security\User`, this class need to implement [StatelessUserInterface](/src/User/StatelessUserInterface.php).
23+
This interface contains only a `fromPayload(array $payload)` method returns an instance of the class.
24+
25+
#### Sample implementation
26+
27+
```php
28+
namespace App\Security;
29+
30+
use Istio\Symfony\JWTAuthentication\User\StatelessUserInterface;
31+
32+
final class User implements StatelessUserInterface
33+
{
34+
//....
35+
36+
public static function fromPayload(array $payload): static
37+
{
38+
$instance = new static();
39+
40+
// use $payload to config your user instance.
41+
42+
return $instance;
43+
}
44+
}
45+
```

0 commit comments

Comments
 (0)