1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* Authentication Class
|
4
|
|
|
*/
|
5
|
|
|
|
6
|
|
|
namespace Twigger\UnionCloud\API\Auth;
|
7
|
|
|
|
8
|
|
|
use Twigger\UnionCloud\API\Configuration;
|
9
|
|
|
use Twigger\UnionCloud\API\Exception\Authentication\AuthenticationParameterMissing;
|
10
|
|
|
use Twigger\UnionCloud\API\Exception\Authentication\AuthenticatorMustExtendIAuthenticator;
|
11
|
|
|
use Twigger\UnionCloud\API\Exception\Authentication\AuthenticatorNotFound;
|
12
|
|
|
|
13
|
|
|
/**
|
14
|
|
|
* Thanks to a changeover in the authentication method
|
15
|
|
|
* for the UnionCloud API in late 2018, this package aims
|
16
|
|
|
* to allow for different authenticators to be plugged in.
|
17
|
|
|
*
|
18
|
|
|
* This is a wrapper which controls an authenticator. A
|
19
|
|
|
* class is an authenticator if it extends IAuthenticator.
|
20
|
|
|
*
|
21
|
|
|
* Class Authentication
|
22
|
|
|
*
|
23
|
|
|
* @package Twigger\UnionCloud\API\Core\Authentications
|
24
|
|
|
*/
|
25
|
|
|
class Authentication
|
26
|
|
|
{
|
27
|
|
|
|
28
|
|
|
/**
|
29
|
|
|
* Implementation of the IAuthenticator interface.
|
30
|
|
|
*
|
31
|
|
|
* This will be used to authenticate the API
|
32
|
|
|
*
|
33
|
|
|
* @var IAuthenticator
|
34
|
|
|
*/
|
35
|
|
|
private $authenticator;
|
36
|
|
|
|
37
|
|
|
/**
|
38
|
|
|
* Authentication constructor.
|
39
|
|
|
*
|
40
|
|
|
* Creates and populates an authenticator if possible.
|
41
|
|
|
*
|
42
|
|
|
* @param array $authParams
|
43
|
|
|
* @param IAuthenticator|string $authenticator
|
44
|
|
|
*
|
45
|
|
|
* @throws AuthenticationParameterMissing
|
46
|
|
|
* @throws AuthenticatorNotFound
|
47
|
|
|
*/
|
48
|
|
|
public function __construct($authParams = null, $authenticator = null)
|
49
|
|
|
{
|
50
|
|
|
if (is_array($authParams))
|
51
|
|
|
{
|
52
|
|
|
// Find the authenticator class
|
53
|
|
|
if ($authenticator === null) {
|
54
|
|
|
$this->authenticator = new v0Authenticator();
|
55
|
|
|
} elseif ($authenticator instanceof IAuthenticator) {
|
56
|
|
|
$this->authenticator = $authenticator;
|
57
|
|
|
} else {
|
58
|
|
|
throw new AuthenticatorNotFound();
|
59
|
|
|
}
|
60
|
|
|
|
61
|
|
|
// Validate and set the parameters
|
62
|
|
|
if (!$this->authenticator->validateParameters($authParams))
|
63
|
|
|
{
|
64
|
|
|
throw new AuthenticationParameterMissing();
|
65
|
|
|
}
|
66
|
|
|
$this->authenticator->setParameters($authParams);
|
67
|
|
|
}
|
68
|
|
|
}
|
69
|
|
|
|
70
|
|
|
/**
|
71
|
|
|
* Manually set the Authenticator
|
72
|
|
|
*
|
73
|
|
|
* @param IAuthenticator $authenticator The authenticator to use for authentication
|
74
|
|
|
*
|
75
|
|
|
* @throws AuthenticatorMustExtendIAuthenticator
|
76
|
|
|
*
|
77
|
|
|
* @return void
|
78
|
|
|
*/
|
79
|
|
|
public function setAuthenticator($authenticator)
|
80
|
|
|
{
|
81
|
|
|
if ($authenticator instanceof IAuthenticator)
|
82
|
|
|
{
|
83
|
|
|
$this->authenticator = $authenticator;
|
84
|
|
|
return;
|
85
|
|
|
}
|
86
|
|
|
|
87
|
|
|
throw new AuthenticatorMustExtendIAuthenticator();
|
88
|
|
|
}
|
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/**
|
92
|
|
|
* Add authentication options to a GuzzleHTTP request option array.
|
93
|
|
|
*
|
94
|
|
|
* @param array $options
|
95
|
|
|
* @param Configuration $configuration
|
96
|
|
|
*
|
97
|
|
|
* @return array Guzzle HTTP options with authentication options
|
98
|
|
|
*/
|
99
|
|
|
public function addAuthentication($options, $configuration)
|
100
|
|
|
{
|
101
|
|
|
if ($this->authenticator->needsRefresh())
|
102
|
|
|
{
|
103
|
|
|
$this->authenticator->authenticate($configuration->getBaseURL());
|
104
|
|
|
}
|
105
|
|
|
return $this->authenticator->addAuthentication($options);
|
106
|
|
|
}
|
107
|
|
|
|
108
|
|
|
/**
|
109
|
|
|
* Check the authenticator has been loaded and is ready to be used.
|
110
|
|
|
*
|
111
|
|
|
* @return bool
|
112
|
|
|
*/
|
113
|
|
|
public function hasAuthentication()
|
114
|
|
|
{
|
115
|
|
|
if (!$this->authenticator instanceof IAuthenticator)
|
116
|
|
|
{
|
117
|
|
|
return false;
|
118
|
|
|
}
|
119
|
|
|
return true;
|
120
|
|
|
}
|
121
|
|
|
|
122
|
|
|
|
123
|
|
|
} |