Skip to content
Open
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ba25906
docs: update .gitignore to ignore .idea directory
marchrius Mar 15, 2024
1567f53
feat(refresh token): add possibility to generate and return a refresh…
marchrius Mar 15, 2024
636f634
feat(refresh token): add support for issued at and add different hook…
marchrius Mar 18, 2024
7ce659c
feature(refresh token): add different flow types as: body, parameter,…
marchrius Mar 18, 2024
77bfe66
fix(refresh token): wrong flow check for cookie flow
marchrius Mar 18, 2024
a33f3f1
fix(refresh token): manage cookie as supported flow
marchrius Mar 18, 2024
3fd08c5
fix(refresh token): manage device as mandatory field. Can be empty
marchrius Mar 19, 2024
322fbe8
fix(refresh token): better retrieve data from request based on flow d…
marchrius Mar 19, 2024
f9b3454
fix(refresh token): restore response content based on flow when refre…
marchrius Mar 19, 2024
7f00143
feat(refresh token): add token type to differentiate token in validat…
marchrius Mar 19, 2024
c440257
feat(logger): add psr/log as base dependency to let users define a cu…
marchrius Mar 19, 2024
d27630a
feature(refresh token): add type (typ) property for token to check co…
marchrius Mar 19, 2024
6b3395a
fix(refresh token): use device from validated token
marchrius Mar 19, 2024
50dcd46
fix(refresh token): don't let hook modify the token type
marchrius Mar 19, 2024
69f36b5
fix(refresh token): restore error code for invalid refresh token (not…
marchrius Mar 19, 2024
8d2559c
feature(unit test): update unit test for new refresh token format
marchrius Mar 19, 2024
acbbbeb
fix(unit test): restore environment names
marchrius Mar 19, 2024
4453afd
fix(unit test): restore CURL constants
marchrius Mar 19, 2024
364d106
docs: add missing dev requirement ext-curl
marchrius Mar 19, 2024
6bfa233
fix(refresh token): rollback WP_REST_Request to retrieve refresh toke…
marchrius Mar 20, 2024
2f72a02
docs(refresh token): updated README.md with latest information, updat…
marchrius Apr 12, 2024
f5de091
Merge branch 'develop' into feature/refresh_token_flow
marchrius Jul 25, 2024
021dcbc
fix: updated tests with correct body implementation
marchrius Jul 25, 2024
4b5cc49
feat: add method to check if a flow is enabled (possibility to implem…
marchrius Jul 30, 2024
fcf7431
chore: update composer.lock
marchrius Jul 30, 2024
1e9617f
fix(refresh_token): revert all style modification to code
marchrius Sep 30, 2024
8deeff8
fix(refresh_token): revert class-devices.php
marchrius Oct 2, 2024
272c481
fix(refresh_token): revert property initialization in class-setup.php
marchrius Oct 2, 2024
f4ae050
fix(refresh_token): revert const definition for JWT_AUTH_PLUGIN_VERSION
marchrius Oct 2, 2024
25363ee
fix(refresh token): indent with spaces
marchrius Oct 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.vscode
/.idea
/node_modules
/vendor
/.phpunit.result.cache
Expand Down
178 changes: 169 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ You can use the optional parameter `device` with the device identifier to let us
"message": "Credential is valid",
"data": {
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvcG9pbnRzLmNvdXZlZS5jby5pZCIsImlhdCI6MTU4ODQ5OTE0OSwibmJmIjoxNTg4NDk5MTQ5LCJleHAiOjE1ODkxMDM5NDksImRhdGEiOnsidXNlciI6eyJpZCI6MX19fQ.w3pf5PslhviHohmiGF-JlPZV00XWE9c2MfvBK7Su9Fw",
"refresh_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvcG9pbnRzLmNvdXZlZS5jby5pZCIsImlhdCI6MTU4ODQ5OTE0OSwibmJmIjoxNTg4NDk5MTQ5LCJleHAiOjE1ODkxMDM5NDksImRhdGEiOnsidXNlciI6eyJpZCI6MX19fQ.w3pf5PslhviHohmiGF-JlPZV00XWE9c2MfvBK7Su9Fw",
"id": 1,
"email": "[email protected]",
"nicename": "contactjavas",
Expand Down Expand Up @@ -217,10 +218,24 @@ This means that a refresh token cannot be shared. To allow multiple devices to a
curl -F device="abc-def" -F username=myuser -F password=mypass /wp-json/jwt-auth/v1/token
```
```sh
curl -F device="abc-def" -b "refresh_token=123.abcdef..." /wp-json/jwt-auth/v1/token
# For a cookie flow
curl -F device="abc-def" -b "refresh_token=eyJ0eXAiOi..." /wp-json/jwt-auth/v1/token

# For a body flow
curl -F device="abc-def" -d "refresh_token=eyJ0eXAiOi..." /wp-json/jwt-auth/v1/token

# For a parameter flow
curl -F device="abc-def" "/wp-json/jwt-auth/v1/token?refresh_token=eyJ0eXAiOi..."
```
```sh
curl -F device="abc-def" -b "refresh_token=123.abcdef..." /wp-json/jwt-auth/v1/token/refresh
# For a cookie flow
curl -F device="abc-def" -b "refresh_token=eyJ0eXAiOi..." /wp-json/jwt-auth/v1/token/refresh

# For a body flow
curl -F device="abc-def" -d "refresh_token=eyJ0eXAiOi..." /wp-json/jwt-auth/v1/token/refresh

# For a parameter flow
curl -F device="abc-def" "/wp-json/jwt-auth/v1/token/refresh?refresh_token=eyJ0eXAiOi..."
```


Expand Down Expand Up @@ -331,7 +346,17 @@ If the token is invalid an error will be returned. Here are some samples of erro
"success": false,
"statusCode": 401,
"code": "jwt_auth_invalid_refresh_token",
"message": "Invalid refresh token",
"message": "Device not found in the refresh token.",
"data": []
}
```

```json
{
"success": false,
"statusCode": 401,
"code": "jwt_auth_invalid_refresh_token",
"message": "Invalid token type",
"data": []
}
```
Expand Down Expand Up @@ -393,6 +418,36 @@ add_filter(
```


### jwt_auth_flow

The **jwt_auth_flow** allows you to decide which flow use for current request.

The supported options are:
- cookie __*(default)*__
- body
- query
- header

To enable the desired refresh token flow add an hook to your theme's functions.php file.
```php
/**
* Change the flow for refresh token.
*
* @param string $flow The current flow.
*/
add_filter(
'jwt_auth_flow',
function ( $headers ) {
if (wp_doing_ajax()) {
// Modify the flow here.
return 'body';
}
return $flow;
);
```

This value will be used to establish from with part of the request the refresh token will be taken.

### jwt_auth_authorization_header

The **jwt_auth_authorization_header** allows you to modify the Authorization header key used to validating a token. Useful when the server already uses the 'Authorization' key for another auth method.
Expand Down Expand Up @@ -455,6 +510,8 @@ add_filter(

### jwt_auth_not_before

#### alias for [jwt_auth_toke_not_before](#jwt_auth_token_not_before)

The `jwt_auth_not_before` allows you to change the [**nbf**](https://tools.ietf.org/html/rfc7519#section-4.1.5) value before the payload is encoded to be a token

Default Value:
Expand Down Expand Up @@ -486,8 +543,43 @@ add_filter(
);
```

### jwt_auth_token_not_before

The `jwt_auth_token_not_before` allows you to change the [**nbf**](https://tools.ietf.org/html/rfc7519#section-4.1.5) value before the payload is encoded to be a token

Default Value:

```
// Creation time.
time()
```

Usage example:

```php
/**
* Change the token's nbf value.
*
* @param int $not_before The default "nbf" value in timestamp.
* @param int $issued_at The "iat" value in timestamp.
*
* @return int The "nbf" value.
*/
add_filter(
'jwt_auth_token_not_before',
function ( $not_before, $issued_at ) {
// Modify the "not_before" here.
return $not_before;
},
10,
2
);
```

### jwt_auth_expire

#### alias for [jwt_auth_token_expire](#jwt_auth_token_expire)

The `jwt_auth_expire` allows you to change the [**exp**](https://tools.ietf.org/html/rfc7519#section-4.1.4) value before the payload is encoded to be a token

Default Value:
Expand Down Expand Up @@ -518,9 +610,77 @@ add_filter(
);
```


### jwt_auth_token_expire

The `jwt_auth_token_expire` allows you to change the [**exp**](https://tools.ietf.org/html/rfc7519#section-4.1.4) value before the payload is encoded to be a token

Default Value:

```
time() + (MINUTE_IN_SECONDS * 10)
```

Usage example:

```php
/**
* Change the token's expire value.
*
* @param int $expire The default "exp" value in timestamp.
* @param int $issued_at The "iat" value in timestamp.
*
* @return int The "nbf" value.
*/
add_filter(
'jwt_auth_token_expire',
function ( $expire, $issued_at ) {
// Modify the "expire" here.
return $expire;
},
10,
2
);
```



### jwt_auth_refresh_not_before

The `jwt_auth_refresh_not_before` allows you to change the [**nbf**](https://tools.ietf.org/html/rfc7519#section-4.1.5) value before the payload is encoded to be a refresh token

Default Value:

```
// Creation time.
time()
```

Usage example:

```php
/**
* Change the refresh token's nbf value.
*
* @param int $not_before The default "nbf" value in timestamp.
* @param int $issued_at The "iat" value in timestamp.
*
* @return int The "nbf" value.
*/
add_filter(
'jwt_auth_refresh_not_before',
function ( $not_before, $issued_at ) {
// Modify the "not_before" here.
return $not_before;
},
10,
2
);
```

### jwt_auth_refresh_expire

The `jwt_auth_refresh_expire` filter hook allows you to change the expiration date of the refresh token.
The `jwt_auth_refresh_expire` filter hook allows you to change the [**exp**](https://tools.ietf.org/html/rfc7519#section-4.1.4) value before the payload is encoded to be a refresh token

Default Value:

Expand Down Expand Up @@ -750,15 +910,15 @@ add_filter(
There are end-to-end tests you can run to confirm that the API works correctly:

```console
$ URL=https://example.local USERNAME=myuser PASSWORD=mypass composer run test
$ URL=https://example.local USERNAME=myuser PASSWORD=mypass FLOW=cookie composer run test
> ./vendor/bin/phpunit
PHPUnit 9.5.13 by Sebastian Bergmann and contributors.
PHPUnit 9.5.25 #StandWithUkraine

............. 13 / 13 (100%)
............... 15 / 15 (100%)

Time: 00:12.377, Memory: 6.00 MB
Time: 00:48.086, Memory: 8.00 MB

OK (13 tests, 110 assertions)
OK (15 tests, 143 assertions)
```


Expand Down
Loading