Skip to content

Commit 47235b9

Browse files
authored
Merge pull request #41 from klickagent/patch-1
Passwort grant type integration
2 parents 8856523 + 24143c5 commit 47235b9

File tree

5 files changed

+28
-3
lines changed

5 files changed

+28
-3
lines changed

src/Auth/OAuthAuthenticate.php

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function __construct(ComponentRegistry $registry, $config)
6565

6666
if ($this->config('server')) {
6767
$this->Server = $this->config('server');
68+
6869
return;
6970
}
7071

@@ -131,6 +132,7 @@ public function unauthenticated(Request $request, Response $response)
131132
]
132133
)
133134
);
135+
134136
return $response;
135137
}
136138
$message = __d('authenticate', 'You are not authenticated.');
@@ -147,6 +149,7 @@ public function getUser(Request $request)
147149
$this->Server->isValidRequest(true, $request->query('access_token'));
148150
} catch (OAuthException $e) {
149151
$this->_exception = $e;
152+
150153
return false;
151154
}
152155
$ownerModel = $this->Server

src/Controller/Component/OAuthComponent.php

+21-3
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ class OAuthComponent extends Component
2121
*
2222
* @var array
2323
*/
24-
protected $_allowedGrants = ['AuthCode', 'RefreshToken', 'ClientCredentials'];
24+
protected $_allowedGrants = ['AuthCode', 'RefreshToken', 'ClientCredentials', 'Password'];
2525

2626
/**
2727
* @var array
2828
*/
2929
protected $_defaultConfig = [
3030
'tokenTTL' => 2592000, //TTL 30 * 24 * 60 * 60 in seconds
31-
'supportedGrants' => ['AuthCode', 'RefreshToken', 'ClientCredentials'],
31+
'supportedGrants' => ['AuthCode', 'RefreshToken', 'ClientCredentials', 'Password'],
3232
'storages' => [
3333
'session' => [
3434
'className' => 'OAuthServer.Session'
@@ -61,6 +61,7 @@ protected function _getAuthorizationServer()
6161
{
6262
$serverConfig = $this->config('authorizationServer');
6363
$serverClassName = App::className($serverConfig['className']);
64+
6465
return new $serverClassName();
6566
}
6667

@@ -85,7 +86,23 @@ public function initialize(array $config)
8586
}
8687

8788
$className = '\\League\\OAuth2\\Server\\Grant\\' . $grant . 'Grant';
88-
$server->addGrantType(new $className());
89+
$objGrant = new $className();
90+
if ($grant === 'Password') {
91+
$objGrant->setVerifyCredentialsCallback(function ($username, $password) {
92+
$controller = $this->_registry->getController();
93+
$controller->Auth->constructAuthenticate();
94+
$userfield = $controller->components['Auth']['authenticate']['Form']['fields']['username'];
95+
$controller->request->data[$userfield] = $username;
96+
$controller->request->data['password'] = $password;
97+
$loginOk = $controller->Auth->identify();
98+
if ($loginOk) {
99+
return $loginOk['id'];
100+
} else {
101+
return false;
102+
}
103+
});
104+
}
105+
$server->addGrantType($objGrant);
89106
}
90107

91108
$server->setAccessTokenTTL($this->config('tokenTTL'));
@@ -111,6 +128,7 @@ public function checkAuthParams($authGrant)
111128
$controller->response->statusCode($e->httpStatusCode);
112129
$controller->response->header($e->getHttpHeaders());
113130
$controller->set('response', $e);
131+
114132
return false;
115133
}
116134
}

src/Controller/OAuthController.php

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public function authorize()
124124
$redirectUri = $this->OAuth->Server->getGrantType('authorization_code')->newAuthorizeRequest($ownerModel, $ownerId, $authParams);
125125
$event = new Event('OAuthServer.afterAuthorize', $this);
126126
EventManager::instance()->dispatch($event);
127+
127128
return $this->redirect($redirectUri);
128129
} elseif ($this->request->is('post')) {
129130
$event = new Event('OAuthServer.afterDeny', $this);

src/Model/Entity/Client.php

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ protected function _getParent()
2929
return null;
3030
}
3131
$parentTable = TableRegistry::get($this->parent_model);
32+
3233
return $parentTable->get($this->parent_id);
3334
}
3435

src/Traits/GetStorageTrait.php

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ protected function _resolveClassName($class)
2020
if (!$className) {
2121
throw new Exception(sprintf('Storage class "%s" was not found.', $class));
2222
}
23+
2324
return $className;
2425
}
2526

@@ -39,6 +40,7 @@ protected function _getStorage($name)
3940
}
4041

4142
$className = $this->_resolveClassName($config['className']);
43+
4244
return new $className();
4345
}
4446
}

0 commit comments

Comments
 (0)