Skip to content

Commit 6f3f6ab

Browse files
committed
Add user auth grant type
1 parent 91b0db8 commit 6f3f6ab

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

Module.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function init()
3030
public function getServer($force = false)
3131
{
3232
if($this->_server === null || $force === true) {
33-
$storages = $this->createStorage();
33+
$storages = $this->createStorages();
3434
$server = new \OAuth2\Server($storages, $this->options);
3535

3636
$server->addGrantType(new \OAuth2\GrantType\UserCredentials($storages['user_credentials']));
@@ -53,7 +53,7 @@ public function getResponse()
5353
return new \OAuth2\Response();
5454
}
5555

56-
protected function createStorage()
56+
public function createStorages()
5757
{
5858
$connection = Yii::$app->getDb();
5959
if(!$connection->getIsActive()) {

grants/UserAuthCredentials.php

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace filsh\yii2\oauth2server\grants;
4+
5+
use \OAuth2\Storage\ClientCredentialsInterface;
6+
use \OAuth2\Storage\UserCredentialsInterface;
7+
8+
class UserAuthCredentials extends \OAuth2\ClientAssertionType\HttpBasic implements \OAuth2\GrantType\GrantTypeInterface
9+
{
10+
protected $userStorage;
11+
12+
public function __construct(UserCredentialsInterface $userStorage, ClientCredentialsInterface $storage, array $config = array())
13+
{
14+
$this->userStorage = $userStorage;
15+
parent::__construct($storage, $config);
16+
}
17+
18+
public function getQuerystringIdentifier()
19+
{
20+
return 'user_authkey_credentials';
21+
}
22+
23+
public function createAccessToken(\OAuth2\ResponseType\AccessTokenInterface $accessToken, $client_id, $user_id, $scope)
24+
{
25+
return $accessToken->createAccessToken($client_id, $user_id, $scope);
26+
}
27+
28+
public function getUserId()
29+
{
30+
return $this->userInfo['user_id'];
31+
}
32+
33+
public function getScope()
34+
{
35+
return isset($this->userInfo['scope']) ? $this->userInfo['scope'] : null;
36+
}
37+
38+
public function validateRequest(\OAuth2\RequestInterface $request, \OAuth2\ResponseInterface $response)
39+
{
40+
if (!$request->request('authkey') || !$request->request('username')) {
41+
$response->setError(400, 'invalid_request', 'Missing parameters: "authkey" and "username" required');
42+
return null;
43+
}
44+
45+
if (!$this->userStorage->findIdentityByAccessToken($request->request('authkey'))) {
46+
$response->setError(401, 'invalid_grant', 'Invalid user authkey');
47+
return null;
48+
}
49+
50+
$userInfo = $this->userStorage->getUserDetails($request->request('username'));
51+
52+
if (empty($userInfo)) {
53+
$response->setError(400, 'invalid_grant', 'Unable to retrieve user information');
54+
return null;
55+
}
56+
57+
if (!isset($userInfo['user_id'])) {
58+
throw new \LogicException('you must set the user_id on the array returned by getUserDetails');
59+
}
60+
61+
$this->userInfo = $userInfo;
62+
63+
return parent::validateRequest($request, $response);
64+
}
65+
}

0 commit comments

Comments
 (0)