1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AdvancedLearning\Oauth2Server\Models; |
4
|
|
|
|
5
|
|
|
use function base64_encode; |
6
|
|
|
use SilverStripe\ORM\DataObject; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Stores ClientEntity information. |
10
|
|
|
* |
11
|
|
|
* @package AdvancedLearning\Oauth2Server\Models |
12
|
|
|
* |
13
|
|
|
* @property string $Grants |
14
|
|
|
* @property string $Name |
15
|
|
|
* @property string $Secret |
16
|
|
|
* @property string $Identifier |
17
|
|
|
*/ |
18
|
|
|
class Client extends DataObject |
19
|
|
|
{ |
20
|
|
|
private static $table_name = 'OauthClient'; |
|
|
|
|
21
|
|
|
|
22
|
|
|
private static $db = [ |
|
|
|
|
23
|
|
|
'Name' => 'Varchar(100)', |
24
|
|
|
'Grants' => 'Varchar(255)', |
25
|
|
|
'Secret' => 'Varchar(255)', |
26
|
|
|
'Identifier' => 'Varchar(255)' |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
private static $summary_fields = [ |
|
|
|
|
30
|
|
|
'Name' |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Checks whether this ClientEntity has the given grant type. |
35
|
|
|
* |
36
|
|
|
* @param string $grantType The grant type to check. |
37
|
|
|
* |
38
|
|
|
* @return boolean |
39
|
|
|
*/ |
40
|
|
|
public function hasGrantType($grantType) |
41
|
|
|
{ |
42
|
|
|
$grants = explode(',', $this->Grants); |
43
|
|
|
|
44
|
|
|
return !empty($grants) && in_array($grantType, $grants); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* On before write. Generate a secret if we don't have one. |
49
|
|
|
*/ |
50
|
|
|
public function onBeforeWrite() |
51
|
|
|
{ |
52
|
|
|
parent::onBeforeWrite(); |
53
|
|
|
|
54
|
|
|
if (empty($this->Secret)) { |
55
|
|
|
$this->Secret = $this->generateSecret(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (empty($this->Identifier)) { |
59
|
|
|
$this->Identifier = $this->generateSecret(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Generate a random secret. |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
protected function generateSecret() |
69
|
|
|
{ |
70
|
|
|
return base64_encode(random_bytes(32)); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|