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
107 lines (92 loc) · 3.1 KB
/
Copy pathApiKeyAuthenticate.php
File metadata and controls
107 lines (92 loc) · 3.1 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
<?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,
];
/**
* 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)
{
$type = $this->config('type');
if (!in_array($type, $this->types)) {
throw new OutOfBoundsException(__d('Users', 'Type {0} is not valid', $type));
}
if (!is_callable([$this, $type])) {
throw new OutOfBoundsException(__d('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('Users', 'SSL is required for ApiKey Authentication', $type));
}
$this->_config['fields']['username'] = $this->config('field');
$this->_config['userModel'] = Configure::read('Users.table');
$this->_config['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);
}
}