1
|
|
|
<?php namespace nyx\auth\id; |
2
|
|
|
|
3
|
|
|
// Internal dependencies |
4
|
|
|
use nyx\auth; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Base Identity |
8
|
|
|
* |
9
|
|
|
* @package Nyx\Auth |
10
|
|
|
* @version 0.1.0 |
11
|
|
|
* @author Michal Chojnacki <[email protected]> |
12
|
|
|
* @copyright 2012-2017 Nyx Dev Team |
13
|
|
|
* @link https://github.com/unyx/nyx |
14
|
|
|
* @todo Private access to some properties (ID at the very least? Type is currently not enforced on assignment). |
15
|
|
|
*/ |
16
|
|
|
abstract class Identity implements interfaces\Identity |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string The fully qualified class name of this Identity's Provider. |
20
|
|
|
*/ |
21
|
|
|
protected static $provider; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string The unique identifier of the Identity specific to its Provider. |
25
|
|
|
*/ |
26
|
|
|
protected $id; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string The username/nickname/display name bound to the Identity. |
30
|
|
|
*/ |
31
|
|
|
protected $username; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string The full name bound to the Identity. |
35
|
|
|
*/ |
36
|
|
|
protected $name; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string The e-mail address bound to the Identity. |
40
|
|
|
*/ |
41
|
|
|
protected $email; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string The avatar URI bound to the Identity. |
45
|
|
|
*/ |
46
|
|
|
protected $avatar; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array The raw data about the Identity the Provider made available. |
50
|
|
|
*/ |
51
|
|
|
protected $raw; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var array The access tokens associated with the Identity. |
55
|
|
|
*/ |
56
|
|
|
protected $tokens = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Instantiates a Provider of the appropriate type specific to this Identity. |
60
|
|
|
* |
61
|
|
|
* Utility factory method for requesting consumer data when a specific Provider is needed, without having to |
62
|
|
|
* worry about the underlying implementation and protocols used to access that data. |
63
|
|
|
* |
64
|
|
|
* @param string $clientId The consumer's (application/client) identifier. |
65
|
|
|
* @param string $clientSecret The consumer's (application/client) secret. |
66
|
|
|
* @param string $redirectUrl The consumer's (application/client) callback URL. |
67
|
|
|
* @return interfaces\Provider The created Provider instance. |
68
|
|
|
* @throws \LogicException When the Identity implementation did not define its static $provider property. |
69
|
|
|
*/ |
70
|
|
|
public static function provider(string $clientId, string $clientSecret, string $redirectUrl) : interfaces\Provider |
71
|
|
|
{ |
72
|
|
|
if (!isset(static::$provider)) { |
73
|
|
|
throw new \LogicException('This identity does not appear to have a defined Provider class.'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return new static::$provider(new credentials\Client($clientId, $clientSecret, $redirectUrl)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Creates a new Identity instance. |
81
|
|
|
* |
82
|
|
|
* @param auth\interfaces\Token $token The access Token used to retrieve the data about the Identity. |
83
|
|
|
* @param array $data The raw data about the Identity the Provider made available. |
84
|
|
|
* @throws \InvalidArgumentException When the identifier was not mapped to the 'id' property beforehand |
85
|
|
|
* and could not be found in the raw data either. |
86
|
|
|
*/ |
87
|
|
|
public function __construct(auth\interfaces\Token $token, array $data) |
88
|
|
|
{ |
89
|
|
|
// Identifiers are required, all other data is optional. |
90
|
|
|
// The property check is put in place to allow child classes to map the identifier to the property |
91
|
|
|
// before calling the base constructor, in case the identifier is not available under the "id" key |
92
|
|
|
// in the raw data (and we don't want to rely on this convention globally). |
93
|
|
|
if (!isset($this->id)) { |
94
|
|
|
if (!isset($data['id'])) { |
95
|
|
|
throw new \InvalidArgumentException('The Identity is missing an unique identifier.'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->id = $data['id']; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->tokens[] = $token; |
102
|
|
|
$this->raw = $data; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritDoc} |
107
|
|
|
*/ |
108
|
|
|
public function getId() : string |
109
|
|
|
{ |
110
|
|
|
return $this->id; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritDoc} |
115
|
|
|
*/ |
116
|
|
|
public function getUsername() : ?string |
117
|
|
|
{ |
118
|
|
|
return $this->username; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritDoc} |
123
|
|
|
*/ |
124
|
|
|
public function getName() : ?string |
125
|
|
|
{ |
126
|
|
|
return $this->name; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritDoc} |
131
|
|
|
*/ |
132
|
|
|
public function getEmail() : ?string |
133
|
|
|
{ |
134
|
|
|
return $this->email; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritDoc} |
139
|
|
|
*/ |
140
|
|
|
public function getAvatar() : ?string |
141
|
|
|
{ |
142
|
|
|
return $this->avatar; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Returns the raw data about the Identity the Provider made available. |
147
|
|
|
* |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
|
|
public function getRaw() : array |
151
|
|
|
{ |
152
|
|
|
return $this->raw; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|