This repository has been archived by the owner on Jun 8, 2024. It is now read-only.
forked from lucadegasperi/oauth2-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Bloemendaal/device-flow-grant-update
Device flow grant update
- Loading branch information
Showing
27 changed files
with
1,706 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
/** | ||
* @author Andrew Millington <[email protected]> | ||
* @copyright Copyright (c) Alex Bilbie | ||
* @license http://mit-license.org/ | ||
* | ||
* @link https://github.com/thephpleague/oauth2-server | ||
*/ | ||
|
||
include __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use League\OAuth2\Server\AuthorizationServer; | ||
use League\OAuth2\Server\Exception\OAuthServerException; | ||
use League\OAuth2\Server\Grant\DeviceCodeGrant; | ||
use OAuth2ServerExamples\Repositories\AccessTokenRepository; | ||
use OAuth2ServerExamples\Repositories\ClientRepository; | ||
use OAuth2ServerExamples\Repositories\DeviceCodeRepository; | ||
use OAuth2ServerExamples\Repositories\RefreshTokenRepository; | ||
use OAuth2ServerExamples\Repositories\ScopeRepository; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Slim\App; | ||
use Zend\Diactoros\Stream; | ||
|
||
$app = new App([ | ||
'settings' => [ | ||
'displayErrorDetails' => true, | ||
], | ||
AuthorizationServer::class => function () { | ||
// Init our repositories | ||
$clientRepository = new ClientRepository(); | ||
$scopeRepository = new ScopeRepository(); | ||
$accessTokenRepository = new AccessTokenRepository(); | ||
$refreshTokenRepository = new RefreshTokenRepository(); | ||
$deviceCodeRepository = new DeviceCodeRepository(); | ||
|
||
$privateKeyPath = 'file://' . __DIR__ . '/../private.key'; | ||
|
||
// Set up the authorization server | ||
$server = new AuthorizationServer( | ||
$clientRepository, | ||
$accessTokenRepository, | ||
$scopeRepository, | ||
$privateKeyPath, | ||
'lxZFUEsBCJ2Yb14IF2ygAHI5N4+ZAUXXaSeeJm6+twsUmIen' | ||
); | ||
|
||
// Enable the device code grant on the server with a token TTL of 1 hour | ||
$server->enableGrantType( | ||
new DeviceCodeGrant( | ||
$deviceCodeRepository, | ||
$refreshTokenRepository, | ||
new \DateInterval('PT10M'), | ||
5 | ||
), | ||
new \DateInterval('PT1H') | ||
); | ||
|
||
return $server; | ||
}, | ||
]); | ||
|
||
$app->post('/device_authorization', function (ServerRequestInterface $request, ResponseInterface $response) use ($app) { | ||
/* @var \League\OAuth2\Server\AuthorizationServer $server */ | ||
$server = $app->getContainer()->get(AuthorizationServer::class); | ||
|
||
try { | ||
$deviceAuthRequest = $server->validateDeviceAuthorizationRequest($request); | ||
|
||
// Once the user has logged in, set the user on the authorization request | ||
//$deviceAuthRequest->setUser(); | ||
|
||
// Once the user has approved or denied the client, update the status | ||
//$deviceAuthRequest->setAuthorizationApproved(true); | ||
|
||
// Return the HTTP redirect response | ||
return $server->completeDeviceAuthorizationRequest($deviceAuthRequest, $response); | ||
} catch (OAuthServerException $exception) { | ||
return $exception->generateHttpResponse($response); | ||
} catch (\Exception $exception) { | ||
$body = new Stream('php://temp', 'r+'); | ||
$body->write($exception->getMessage()); | ||
|
||
return $response->withStatus(500)->withBody($body); | ||
} | ||
}); | ||
|
||
$app->post('/access_token', function (ServerRequestInterface $request, ResponseInterface $response) use ($app) { | ||
/* @var \League\OAuth2\Server\AuthorizationServer $server */ | ||
$server = $app->getContainer()->get(AuthorizationServer::class); | ||
|
||
try { | ||
return $server->respondToAccessTokenRequest($request, $response); | ||
} catch (OAuthServerException $exception) { | ||
return $exception->generateHttpResponse($response); | ||
} catch (\Exception $exception) { | ||
$body = new Stream('php://temp', 'r+'); | ||
$body->write($exception->getMessage()); | ||
|
||
return $response->withStatus(500)->withBody($body); | ||
} | ||
}); | ||
|
||
$app->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
/** | ||
* @author Andrew Millington <[email protected]> | ||
* @copyright Copyright (c) Alex Bilbie | ||
* @license http://mit-license.org/ | ||
* | ||
* @link https://github.com/thephpleague/oauth2-server | ||
*/ | ||
|
||
namespace OAuth2ServerExamples\Entities; | ||
|
||
use League\OAuth2\Server\Entities\DeviceCodeEntityInterface; | ||
use League\OAuth2\Server\Entities\Traits\DeviceCodeTrait; | ||
use League\OAuth2\Server\Entities\Traits\EntityTrait; | ||
use League\OAuth2\Server\Entities\Traits\TokenEntityTrait; | ||
|
||
class DeviceCodeEntity implements DeviceCodeEntityInterface | ||
{ | ||
use EntityTrait, DeviceCodeTrait, TokenEntityTrait; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/** | ||
* @author Andrew Millington <[email protected]> | ||
* @copyright Copyright (c) Alex Bilbie | ||
* @license http://mit-license.org/ | ||
* | ||
* @link https://github.com/thephpleague/oauth2-server | ||
*/ | ||
|
||
namespace OAuth2ServerExamples\Repositories; | ||
|
||
use League\OAuth2\Server\Entities\ClientEntityInterface; | ||
use League\OAuth2\Server\Entities\DeviceCodeEntityInterface; | ||
use League\OAuth2\Server\Repositories\DeviceCodeRepositoryInterface; | ||
use OAuth2ServerExamples\Entities\DeviceCodeEntity; | ||
|
||
class DeviceCodeRepository implements DeviceCodeRepositoryInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getNewDeviceCode() | ||
{ | ||
return new DeviceCodeEntity(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function persistNewDeviceCode(DeviceCodeEntityInterface $deviceCodeEntity) | ||
{ | ||
// Some logic to persist a new device code to a database | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDeviceCodeEntityByDeviceCode($deviceCode, $grantType, ClientEntityInterface $clientEntity) | ||
{ | ||
$deviceCode = new DeviceCodeEntity(); | ||
|
||
// The user identifier should be set when the user authenticates on the OAuth server | ||
$deviceCode->setUserIdentifier(1); | ||
|
||
return $deviceCode; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function revokeDeviceCode($codeId) | ||
{ | ||
// Some logic to revoke device code | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isDeviceCodeRevoked($codeId) | ||
{ | ||
// Some logic to check if a device code has been revoked | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* @author Alex Bilbie <[email protected]> | ||
* @copyright Copyright (c) Alex Bilbie | ||
* @license http://mit-license.org/ | ||
* | ||
* @link https://github.com/thephpleague/oauth2-server | ||
*/ | ||
|
||
namespace League\OAuth2\Server\Entities; | ||
|
||
interface DeviceCodeEntityInterface extends TokenInterface | ||
{ | ||
/** | ||
* @return string | ||
*/ | ||
public function getUserCode(); | ||
|
||
/** | ||
* @param string $userCode | ||
*/ | ||
public function setUserCode($userCode); | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getVerificationUri(); | ||
|
||
/** | ||
* @param string $verificationUri | ||
*/ | ||
public function setVerificationUri($verificationUri); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.