@@ -21,14 +21,14 @@ class OAuthComponent extends Component
21
21
*
22
22
* @var array
23
23
*/
24
- protected $ _allowedGrants = ['AuthCode ' , 'RefreshToken ' , 'ClientCredentials ' ];
24
+ protected $ _allowedGrants = ['AuthCode ' , 'RefreshToken ' , 'ClientCredentials ' , ' Password ' ];
25
25
26
26
/**
27
27
* @var array
28
28
*/
29
29
protected $ _defaultConfig = [
30
30
'tokenTTL ' => 2592000 , //TTL 30 * 24 * 60 * 60 in seconds
31
- 'supportedGrants ' => ['AuthCode ' , 'RefreshToken ' , 'ClientCredentials ' ],
31
+ 'supportedGrants ' => ['AuthCode ' , 'RefreshToken ' , 'ClientCredentials ' , ' Password ' ],
32
32
'storages ' => [
33
33
'session ' => [
34
34
'className ' => 'OAuthServer.Session '
@@ -61,6 +61,7 @@ protected function _getAuthorizationServer()
61
61
{
62
62
$ serverConfig = $ this ->config ('authorizationServer ' );
63
63
$ serverClassName = App::className ($ serverConfig ['className ' ]);
64
+
64
65
return new $ serverClassName ();
65
66
}
66
67
@@ -85,7 +86,23 @@ public function initialize(array $config)
85
86
}
86
87
87
88
$ 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 );
89
106
}
90
107
91
108
$ server ->setAccessTokenTTL ($ this ->config ('tokenTTL ' ));
@@ -111,6 +128,7 @@ public function checkAuthParams($authGrant)
111
128
$ controller ->response ->statusCode ($ e ->httpStatusCode );
112
129
$ controller ->response ->header ($ e ->getHttpHeaders ());
113
130
$ controller ->set ('response ' , $ e );
131
+
114
132
return false ;
115
133
}
116
134
}
0 commit comments