Skip to content

Commit 803978c

Browse files
add examples
1 parent dcd57ed commit 803978c

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

examples/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,31 @@ curl -X "POST" "http://localhost:4444/device_code.php/access_token" \
7979
--data-urlencode "client_id=myawesomeapp" \
8080
--data-urlencode "client_secret=abc123"
8181
```
82+
83+
## Testing the token revocation example
84+
85+
Send the following cURL request. Replace `{{TOKEN}}` with an access token or a refresh token from another grant above:
86+
87+
```
88+
curl -X "POST" "http://localhost:4444/token_revocation.php/revoke_token" \
89+
-H "Content-Type: application/x-www-form-urlencoded" \
90+
-H "Accept: 1.0" \
91+
--data-urlencode "client_id=myawesomeapp" \
92+
--data-urlencode "client_secret=abc123" \
93+
--data-urlencode "token_type_hint=access_token" \
94+
--data-urlencode "token={{TOKEN}}"
95+
```
96+
97+
## Testing the token introspection example
98+
99+
Send the following cURL request. Replace `{{TOKEN}}` with an access token or a refresh token from another grant above:
100+
101+
```
102+
curl -X "POST" "http://localhost:4444/token_introspection.php/introspect_token" \
103+
-H "Content-Type: application/x-www-form-urlencoded" \
104+
-H "Accept: 1.0" \
105+
--data-urlencode "client_id=myawesomeapp" \
106+
--data-urlencode "client_secret=abc123" \
107+
--data-urlencode "token_type_hint=access_token" \
108+
--data-urlencode "refresh_token={{TOKEN}}"
109+
```
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
include __DIR__ . '/../vendor/autoload.php';
6+
7+
use Laminas\Diactoros\Stream;
8+
use League\OAuth2\Server\Exception\OAuthServerException;
9+
use League\OAuth2\Server\TokenServer;
10+
use OAuth2ServerExamples\Repositories\AccessTokenRepository;
11+
use OAuth2ServerExamples\Repositories\ClientRepository;
12+
use OAuth2ServerExamples\Repositories\RefreshTokenRepository;
13+
use Psr\Http\Message\ResponseInterface;
14+
use Psr\Http\Message\ServerRequestInterface;
15+
use Slim\App;
16+
17+
$app = new App([
18+
'settings' => [
19+
'displayErrorDetails' => true,
20+
],
21+
TokenServer::class => function () {
22+
// Init our repositories
23+
$clientRepository = new ClientRepository();
24+
$accessTokenRepository = new AccessTokenRepository();
25+
$refreshTokenRepository = new RefreshTokenRepository();
26+
27+
$publicKeyPath = 'file://' . __DIR__ . '/../public.key';
28+
29+
// Setup the authorization server
30+
return new TokenServer(
31+
$clientRepository,
32+
$accessTokenRepository,
33+
$refreshTokenRepository,
34+
$publicKeyPath,
35+
'lxZFUEsBCJ2Yb14IF2ygAHI5N4+ZAUXXaSeeJm6+twsUmIen'
36+
);
37+
},
38+
]);
39+
40+
$app->post('/introspect_token', function (ServerRequestInterface $request, ResponseInterface $response) use ($app) {
41+
/* @var \League\OAuth2\Server\TokenServer $server */
42+
$server = $app->getContainer()->get(TokenServer::class);
43+
44+
try {
45+
return $server->respondToTokenIntrospectionRequest($request, $response);
46+
} catch (OAuthServerException $exception) {
47+
return $exception->generateHttpResponse($response);
48+
} catch (Exception $exception) {
49+
$body = new Stream('php://temp', 'r+');
50+
$body->write($exception->getMessage());
51+
52+
return $response->withStatus(500)->withBody($body);
53+
}
54+
});
55+
56+
$app->run();
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
include __DIR__ . '/../vendor/autoload.php';
6+
7+
use Laminas\Diactoros\Stream;
8+
use League\OAuth2\Server\Exception\OAuthServerException;
9+
use League\OAuth2\Server\TokenServer;
10+
use OAuth2ServerExamples\Repositories\AccessTokenRepository;
11+
use OAuth2ServerExamples\Repositories\ClientRepository;
12+
use OAuth2ServerExamples\Repositories\RefreshTokenRepository;
13+
use Psr\Http\Message\ResponseInterface;
14+
use Psr\Http\Message\ServerRequestInterface;
15+
use Slim\App;
16+
17+
$app = new App([
18+
'settings' => [
19+
'displayErrorDetails' => true,
20+
],
21+
TokenServer::class => function () {
22+
// Init our repositories
23+
$clientRepository = new ClientRepository();
24+
$accessTokenRepository = new AccessTokenRepository();
25+
$refreshTokenRepository = new RefreshTokenRepository();
26+
27+
$publicKeyPath = 'file://' . __DIR__ . '/../public.key';
28+
29+
// Setup the authorization server
30+
return new TokenServer(
31+
$clientRepository,
32+
$accessTokenRepository,
33+
$refreshTokenRepository,
34+
$publicKeyPath,
35+
'lxZFUEsBCJ2Yb14IF2ygAHI5N4+ZAUXXaSeeJm6+twsUmIen'
36+
);
37+
},
38+
]);
39+
40+
$app->post('/revoke_token', function (ServerRequestInterface $request, ResponseInterface $response) use ($app) {
41+
/* @var \League\OAuth2\Server\TokenServer $server */
42+
$server = $app->getContainer()->get(TokenServer::class);
43+
44+
try {
45+
return $server->respondToTokenRevocationRequest($request, $response);
46+
} catch (OAuthServerException $exception) {
47+
return $exception->generateHttpResponse($response);
48+
} catch (Exception $exception) {
49+
$body = new Stream('php://temp', 'r+');
50+
$body->write($exception->getMessage());
51+
52+
return $response->withStatus(500)->withBody($body);
53+
}
54+
});
55+
56+
$app->run();

0 commit comments

Comments
 (0)