Skip to content

Commit

Permalink
Add support for Bearer token auhentication
Browse files Browse the repository at this point in the history
  • Loading branch information
rrd108 committed Mar 4, 2024
1 parent 25ff37b commit a70d13b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ return [
];
```

#### Authorization with Bearer token

If you want to use the `Authorization` header with `Bearer` token, you should set the `header` key to `Authorization` and the `prefix` key to `Bearer` in your `config/apiTokenAuthenticator.php` file.

```php
<?php
return [
'ApiTokenAuthenticator' => [
'header' => 'Authorization',
'prefix' => 'Bearer',
]
];
```

## Authentication

The plugin authentication workflow is the following.
Expand All @@ -39,19 +53,19 @@ At your client appliacation you should send a POST request to `/users/login.json

```json
{
"email": "[email protected]",
"password": "rrd"
"email": "[email protected]",
"password": "rrd"
}
```

If the login was successful than you will get a response like this.

```json
{
"user": {
"id": 1,
"token": "yourSecretTokenComingFromTheDatabase"
}
"user": {
"id": 1,
"token": "yourSecretTokenComingFromTheDatabase"
}
}
```

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "rrd108/api-token-authenticator",
"description": "A Simple Token Authentication Plugin for CakePHP 5 REST API-s",
"version": "1.0.4",
"version": "1.1.0",
"type": "cakephp-plugin",
"license": "MIT",
"autoload": {
Expand Down
5 changes: 5 additions & 0 deletions config/apiTokenAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
'username' => 'email',
'password' => 'password'
],

// name of the header for the token
'header' => 'Token',
// for Bearer Athorization use this instead of the default
//'header' => 'Authorization
//'tokenPrefix' => 'Bearer',

// login controller and action
'login' => [
'controller' => 'Users',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,21 @@ public function testAuthenticateWithExpiredToken()
$result = $tokenAuth->authenticate($requestWithHeader);
$this->assertSame('TOKEN_EXPIRED', $result->getStatus());
}

public function testAuthenticateWithBearerToken()
{
$options = Configure::read('ApiTokenAuthenticator');
$extraOptions = ['header' => 'Authorization', 'tokenPrefix' => 'Bearer'];
Configure::write('ApiTokenAuthenticator', $extraOptions + $options);
$tokenAuth = new ProvisoryTokenAuthenticator($this->identifiers, $extraOptions + $options);

$request = ServerRequestFactory::fromGlobals(
['REQUEST_URI' => '/testpath'],
[],
['username' => 'rrd', 'password' => 'webmania']
);
$requestWithHeader = $request->withAddedHeader('Authorization', 'Bearer token-1');
$result = $tokenAuth->authenticate($requestWithHeader);
$this->assertSame(Result::SUCCESS, $result->getStatus());
}
}

0 comments on commit a70d13b

Please sign in to comment.