forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiKeyAuthenticate.php
More file actions
131 lines (113 loc) · 4.05 KB
/
Copy pathApiKeyAuthenticate.php
File metadata and controls
131 lines (113 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* Copyright 2010 - 2015, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2015, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Auth;
use Cake\Auth\BaseAuthenticate;
use Cake\Core\Configure;
use Cake\Network\Exception\ForbiddenException;
use Cake\Network\Request;
use Cake\Network\Response;
use \OutOfBoundsException;
/**
* Class ApiKeyAuthenticate. Login the uses by API Key
*/
class ApiKeyAuthenticate extends BaseAuthenticate
{
const TYPE_QUERYSTRING = 'querystring';
const TYPE_HEADER = 'header';
public $types = [self::TYPE_QUERYSTRING, self::TYPE_HEADER];
protected $_defaultConfig = [
//type, can be either querystring or header
'type' => self::TYPE_QUERYSTRING,
//name to retrieve the api key value from
'name' => 'api_key',
//db field where the key is stored
'field' => 'api_token',
//require SSL to pass the token. You should always require SSL to use tokens for Auth
'require_ssl' => true,
//set a specific table for API auth, set as null to use Users.table
'table' => null,
//set a specific finder for API auth, set as null to use Auth.authenticate.all.finder
'finder' => null,
];
/**
* Authenticate callback
* Reads the API Key based on configuration and login the user
*
* @param Request $request Cake request object.
* @param Response $response Cake response object.
* @return mixed
*/
public function authenticate(Request $request, Response $response)
{
return $this->getUser($request);
}
/**
* Stateless Authentication System
* http://book.cakephp.org/3.0/en/controllers/components/authentication.html#creating-stateless-authentication-systems
*
* Config:
* $this->Auth->config('storage', 'Memory');
* $this->Auth->config('unauthorizedRedirect', 'false');
* $this->Auth->config('checkAuthIn', 'Controller.initialize');
* $this->Auth->config('loginAction', false);
*
* @param Request $request Cake request object.
* @return mixed
*/
public function getUser(Request $request)
{
$type = $this->config('type');
if (!in_array($type, $this->types)) {
throw new OutOfBoundsException(__d('CakeDC/Users', 'Type {0} is not valid', $type));
}
if (!is_callable([$this, $type])) {
throw new OutOfBoundsException(__d('CakeDC/Users', 'Type {0} has no associated callable', $type));
}
$apiKey = $this->$type($request);
if (empty($apiKey)) {
return false;
}
if ($this->config('require_ssl') && !$request->is('ssl')) {
throw new ForbiddenException(__d('CakeDC/Users', 'SSL is required for ApiKey Authentication', $type));
}
$this->_config['fields']['username'] = $this->config('field');
$this->_config['userModel'] = $this->config('table') ?: Configure::read('Users.table');
$this->_config['finder'] = $this->config('finder') ?: Configure::read('Auth.authenticate.all.finder') ?: 'all';
$result = $this->_query($apiKey)->first();
if (empty($result)) {
return false;
}
return $result->toArray();
//idea: add array with checks to be passed to $request->is(...)
}
/**
* Get the api key from the querystring
*
* @param Request $request request
* @return string api key
*/
public function querystring(Request $request)
{
$name = $this->config('name');
return $request->query($name);
}
/**
* Get the api key from the header
*
* @param Request $request request
* @return string api key
*/
public function header(Request $request)
{
$name = $this->config('name');
return $request->header($name);
}
}