oauth2-server/auth-server-password.md

103 lines
3.9 KiB
Markdown
Raw Normal View History

2014-10-01 03:14:18 +05:30
---
layout: default
2016-03-23 18:14:53 +05:30
title: Resource owner password credentials grant
2014-10-01 03:14:18 +05:30
permalink: /authorization-server/resource-owner-password-credentials-grant/
---
2016-03-23 18:14:53 +05:30
# Resource owner password credentials grant
2014-10-01 03:14:18 +05:30
2016-03-23 18:14:53 +05:30
This grant is a great user experience for <u>trusted</u> first party clients both on the web and in native applications.
2014-10-01 03:14:18 +05:30
2016-03-23 18:14:53 +05:30
## Flow
2014-10-01 03:14:18 +05:30
2016-03-23 18:14:53 +05:30
The client will ask the user for their authorization credentials (ususally a username and password).
2014-10-01 03:14:18 +05:30
2016-03-23 18:14:53 +05:30
The client then sends a POST request with following body parameters to the authorization server:
2014-10-01 03:14:18 +05:30
2016-03-23 18:14:53 +05:30
* `grant_type` with the value `password`
* `client_id` with the the client's ID
* `client_secret` with the client's secret
* `scope` with a space-delimited list of requested scope permissions.
* `username` with the user's username
* `password` with the user's password
The authorization server will respond with a JSON object containing the following properties:
2014-11-06 11:49:42 +05:30
2016-03-23 18:14:53 +05:30
* `token_type` with the value `Bearer`
* `expires_in` with an integer representing the TTL of the access token
* `access_token` a JWT signed with the authorization server's private key
* `refresh_token` an encrypted payload that can be used to refresh the access token when it expires.
## Setup
2014-10-01 03:14:18 +05:30
2016-03-23 18:14:53 +05:30
Wherever you initialize your objects, initialize a new instance of the authorization server and bind the storage interfaces and authorization code grant:
{% highlight php %}
// Init our repositories
2016-04-17 16:33:45 +05:30
$clientRepository = new ClientRepository(); // instance of ClientRepositoryInterface
$scopeRepository = new ScopeRepository(); // instance of ScopeRepositoryInterface
$accessTokenRepository = new AccessTokenRepository(); // instance of AccessTokenRepositoryInterface
$userRepository = new UserRepository(); // instance of UserRepositoryInterface
$refreshTokenRepository = new RefreshTokenRepository(); // instance of RefreshTokenRepositoryInterface
2016-03-23 18:14:53 +05:30
// Path to public and private keys
2016-03-29 13:35:49 +05:30
$privateKey = 'file://path/to/private.key';
2016-04-17 16:33:45 +05:30
//$privateKey = new CryptKey('file://path/to/private.key', 'passphrase'); // if private key has a pass phrase
2017-07-02 23:22:06 +05:30
$encryptionKey = 'lxZFUEsBCJ2Yb14IF2ygAHI5N4+ZAUXXaSeeJm6+twsUmIen'; // generate using base64_encode(random_bytes(32))
2016-03-29 13:35:49 +05:30
2016-03-23 18:14:53 +05:30
// Setup the authorization server
2016-04-17 17:46:40 +05:30
$server = new \League\OAuth2\Server\AuthorizationServer(
2016-03-23 18:14:53 +05:30
$clientRepository,
$accessTokenRepository,
$scopeRepository,
2016-03-29 13:35:49 +05:30
$privateKey,
2017-07-02 23:22:06 +05:30
$encryptionKey
2016-03-23 18:14:53 +05:30
);
2016-04-17 16:33:45 +05:30
$grant = new \League\OAuth2\Server\Grant\PasswordGrant(
$userRepository,
$refreshTokenRepository
);
2016-04-29 18:55:58 +05:30
$grant->setRefreshTokenTTL(new \DateInterval('P1M')); // refresh tokens will expire after 1 month
2016-04-17 16:33:45 +05:30
// Enable the password grant on the server
2016-03-23 18:14:53 +05:30
$server->enableGrantType(
2016-04-17 16:33:45 +05:30
$grant,
new \DateInterval('PT1H') // access tokens will expire after 1 hour
2016-03-23 18:14:53 +05:30
);
{% endhighlight %}
2014-10-01 03:14:18 +05:30
## Implementation
2016-04-17 16:33:45 +05:30
_Please note: These examples here demonstrate usage with the Slim Framework; Slim is not a requirement to use this library, you just need something that generates PSR7-compatible HTTP requests and responses._
2014-10-01 03:14:18 +05:30
The client will request an access token so create an `/access_token` endpoint.
2016-03-23 18:14:53 +05:30
{% highlight php %}
$app->post('/access_token', function (ServerRequestInterface $request, ResponseInterface $response) use ($app) {
2014-10-01 03:14:18 +05:30
2016-04-17 17:46:40 +05:30
/* @var \League\OAuth2\Server\AuthorizationServer $server */
$server = $app->getContainer()->get(AuthorizationServer::class);
2014-10-01 03:14:18 +05:30
2016-03-23 18:14:53 +05:30
try {
2016-04-17 16:33:45 +05:30
// Try to respond to the request
2016-04-10 21:34:24 +05:30
return $server->respondToAccessTokenRequest($request, $response);
2016-04-17 16:33:45 +05:30
2016-03-23 18:14:53 +05:30
} catch (\League\OAuth2\Server\Exception\OAuthServerException $exception) {
2016-04-17 16:33:45 +05:30
// All instances of OAuthServerException can be formatted into a HTTP response
2016-03-23 18:14:53 +05:30
return $exception->generateHttpResponse($response);
2016-04-17 16:33:45 +05:30
2016-03-23 18:14:53 +05:30
} catch (\Exception $exception) {
2016-04-17 16:33:45 +05:30
// Unknown exception
2016-03-23 18:14:53 +05:30
$body = new Stream('php://temp', 'r+');
$body->write($exception->getMessage());
return $response->withStatus(500)->withBody($body);
2016-04-17 16:33:45 +05:30
2014-10-01 03:14:18 +05:30
}
});
2016-03-23 18:14:53 +05:30
{% endhighlight %}