1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* BEdita, API-first content management framework |
4
|
|
|
* Copyright 2018 ChannelWeb Srl, Chialab Srl |
5
|
|
|
* |
6
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
8
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace BEdita\Core\Model\Table; |
15
|
|
|
|
16
|
|
|
use Cake\Database\Expression\QueryExpression; |
17
|
|
|
use Cake\ORM\Query; |
18
|
|
|
use Cake\ORM\RulesChecker; |
19
|
|
|
use Cake\ORM\Table; |
20
|
|
|
use Cake\Validation\Validator; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* User Tokens Model |
24
|
|
|
* |
25
|
|
|
* @property \BEdita\Core\Model\Table\UsersTable|\Cake\ORM\Association\BelongsTo $Users |
26
|
|
|
* @property \BEdita\Core\Model\Table\ApplicationsTable|\Cake\ORM\Association\BelongsTo $Applications |
27
|
|
|
* |
28
|
|
|
* @method \BEdita\Core\Model\Entity\UserToken get($primaryKey, $options = []) |
29
|
|
|
* @method \BEdita\Core\Model\Entity\UserToken newEntity($data = null, array $options = []) |
30
|
|
|
* @method \BEdita\Core\Model\Entity\UserToken[] newEntities(array $data, array $options = []) |
31
|
|
|
* @method \BEdita\Core\Model\Entity\UserToken|bool save(\Cake\Datasource\EntityInterface $entity, $options = []) |
32
|
|
|
* @method \BEdita\Core\Model\Entity\UserToken patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) |
33
|
|
|
* @method \BEdita\Core\Model\Entity\UserToken[] patchEntities($entities, array $data, array $options = []) |
34
|
|
|
* @method \BEdita\Core\Model\Entity\UserToken findOrCreate($search, callable $callback = null, $options = []) |
35
|
|
|
* |
36
|
|
|
* @mixin \Cake\ORM\Behavior\TimestampBehavior |
37
|
|
|
*/ |
38
|
|
|
class UserTokensTable extends Table |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* List of allowed token types |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
const TOKEN_TPYES = ['otp', 'refresh', 'recovery', '2fa', 'access']; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Initialize method |
49
|
|
|
* |
50
|
|
|
* @param array $config The configuration for the Table. |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
public function initialize(array $config) |
54
|
|
|
{ |
55
|
|
|
parent::initialize($config); |
56
|
|
|
|
57
|
|
|
$this->setTable('user_tokens'); |
58
|
|
|
$this->setDisplayField('id'); |
59
|
|
|
$this->setPrimaryKey('id'); |
60
|
|
|
|
61
|
|
|
$this->addBehavior('Timestamp'); |
62
|
|
|
|
63
|
|
|
$this->belongsTo('Users', [ |
64
|
|
|
'foreignKey' => 'user_id', |
65
|
|
|
'joinType' => 'INNER', |
66
|
|
|
'className' => 'BEdita/Core.Users' |
67
|
|
|
]); |
68
|
|
|
$this->belongsTo('Applications', [ |
69
|
|
|
'foreignKey' => 'application_id', |
70
|
|
|
'className' => 'BEdita/Core.Applications' |
71
|
|
|
]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Default validation rules. |
76
|
|
|
* |
77
|
|
|
* @param \Cake\Validation\Validator $validator Validator instance. |
78
|
|
|
* @return \Cake\Validation\Validator |
79
|
|
|
*/ |
80
|
|
|
public function validationDefault(Validator $validator) |
81
|
|
|
{ |
82
|
|
|
$validator |
83
|
|
|
->integer('id') |
84
|
|
|
->allowEmpty('id', 'create'); |
85
|
|
|
|
86
|
|
|
$validator |
87
|
|
|
->scalar('client_token') |
88
|
|
|
->maxLength('client_token', 255) |
89
|
|
|
->requirePresence('client_token', 'create') |
90
|
|
|
->notEmpty('client_token'); |
91
|
|
|
|
92
|
|
|
$validator |
93
|
|
|
->scalar('secret_token') |
94
|
|
|
->maxLength('secret_token', 255) |
95
|
|
|
->allowEmpty('secret_token'); |
96
|
|
|
|
97
|
|
|
$validator |
98
|
|
|
->scalar('token_type') |
99
|
|
|
->inList('token_type', self::TOKEN_TPYES) |
100
|
|
|
->requirePresence('token_type', 'create') |
101
|
|
|
->notEmpty('token_type'); |
102
|
|
|
|
103
|
|
|
$validator |
104
|
|
|
->dateTime('expires') |
105
|
|
|
->allowEmpty('expires'); |
106
|
|
|
|
107
|
|
|
$validator |
108
|
|
|
->dateTime('used') |
109
|
|
|
->allowEmpty('used'); |
110
|
|
|
|
111
|
|
|
return $validator; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns a rules checker object that will be used for validating |
116
|
|
|
* application integrity. |
117
|
|
|
* |
118
|
|
|
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified. |
119
|
|
|
* @return \Cake\ORM\RulesChecker |
120
|
|
|
* @codeCoverageIgnore |
121
|
|
|
*/ |
122
|
|
|
public function buildRules(RulesChecker $rules) |
123
|
|
|
{ |
124
|
|
|
$rules->add($rules->existsIn(['user_id'], 'Users')); |
125
|
|
|
$rules->add($rules->existsIn(['application_id'], 'Applications')); |
126
|
|
|
|
127
|
|
|
return $rules; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Finder for valid tokens: tokens not expired and not used |
132
|
|
|
* |
133
|
|
|
* @param \Cake\ORM\Query $query Query object instance. |
134
|
|
|
* @return \Cake\ORM\Query |
135
|
|
|
*/ |
136
|
|
|
protected function findValid(Query $query) |
137
|
|
|
{ |
138
|
|
|
$now = $query->func()->now(); |
139
|
|
|
|
140
|
|
|
return $query |
141
|
|
|
->where(function (QueryExpression $exp) use ($now) { |
142
|
|
|
return $exp->and_([ |
143
|
|
|
$exp->isNull($this->aliasField('used')), |
144
|
|
|
$exp->or_(function (QueryExpression $exp) use ($now) { |
|
|
|
|
145
|
|
|
$field = $this->aliasField('expires'); |
146
|
|
|
|
147
|
|
|
return $exp |
148
|
|
|
->isNull($field) |
149
|
|
|
->gte($field, $now); |
150
|
|
|
}), |
151
|
|
|
]); |
152
|
|
|
}); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|