This repository was archived by the owner on Nov 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathClient.php
174 lines (157 loc) · 3.99 KB
/
Client.php
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
App::uses('OAuthAppModel', 'OAuth.Model');
App::uses('String', 'Utility');
/**
* Client Model
*
* @property AccessToken $AccessToken
* @property AuthCode $AuthCode
* @property RefreshToken $RefreshToken
*/
class Client extends OAuthAppModel {
/**
* Primary key field
*
* @var string
*/
public $primaryKey = 'client_id';
/**
* Display field
*
* @var string
*/
public $displayField = 'client_id';
/**
* Secret to distribute when using addClient
*
* @var type
*/
protected $addClientSecret = false;
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'client_id' => array(
'isUnique' => array(
'rule' => array('isUnique'),
),
'notempty' => array(
'rule' => array('notempty'),
),
),
'redirect_uri' => array(
'notempty' => array(
'rule' => array('notempty'),
),
),
);
public $actsAs = array(
'OAuth.HashedField' => array(
'fields' => array(
'client_secret'
),
),
);
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'AccessToken' => array(
'className' => 'OAuth.AccessToken',
'foreignKey' => 'client_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'AuthCode' => array(
'className' => 'OAuth.AuthCode',
'foreignKey' => 'client_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'RefreshToken' => array(
'className' => 'OAuth.RefreshToken',
'foreignKey' => 'client_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
/**
* AddClient
*
* Convinience function for adding client, will create a uuid client_id and random secret
*
* @param mixed $data Either an array (e.g. $controller->request->data) or string redirect_uri
* @return booleen Success of failure
*/
public function add($data = null) {
$this->data['Client'] = array();
if (is_array($data) && is_array($data['Client']) && array_key_exists('redirect_uri', $data['Client'])) {
$this->data['Client']['redirect_uri'] = $data['Client']['redirect_uri'];
} elseif (is_string($data)) {
$this->data['Client']['redirect_uri'] = $data;
} else {
return false;
}
/**
* in case you have additional fields in the clients table such as name, description etc
* and you are using $data['Client']['name'], etc to save
**/
if (is_array($data['Client'])) {
$this->data['Client'] = array_merge($data['Client'], $this->data['Client']);
}
//You may wish to change this
$this->data['Client']['client_id'] = base64_encode(uniqid() . substr(uniqid(), 11, 2)); // e.g. NGYcZDRjODcxYzFkY2Rk (seems popular format)
//$this->data['Client']['client_id'] = uniqid(); // e.g. 4f3d4c8602346
//$this->data['Client']['client_id'] = str_replace('.', '', uniqid('', true)); // e.g. 4f3d4c860235a529118898
//$this->data['Client']['client_id'] = str_replace('-', '', String::uuid()); // e.g. 4f3d4c80cb204b6a8e580a006f97281a
$this->addClientSecret = $this->newClientSecret();
$this->data['Client']['client_secret'] = $this->addClientSecret;
return $this->save($this->data);
}
/**
* Create a new, pretty (as in moderately, not beautiful - that can't be guaranteed ;-) random client secret
*
* @return string
*/
public function newClientSecret() {
$length = 40;
$chars = '@#!%*+/-=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$str = '';
$count = strlen($chars);
while ($length--) {
$str .= $chars[mt_rand(0, $count - 1)];
}
return OAuthComponent::hash($str);
}
public function afterSave($created, $options = array()) {
if ($this->addClientSecret) {
$this->data['Client']['client_secret'] = $this->addClientSecret;
}
return true;
}
}