-
Notifications
You must be signed in to change notification settings - Fork 45
/
OAuthWithAuthorizationCode.php
160 lines (136 loc) · 4.61 KB
/
OAuthWithAuthorizationCode.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
<?php
namespace Microsoft\BingAds\Auth;
use Exception;
abstract class OAuthWithAuthorizationCode extends OAuthAuthorization {
private $oauthService;
/**
* An opaque value used by the client to maintain state between the request and callback.
* @var string
*/
public $State;
/**
* Your application's registered client secret.
* @var string
*/
public $ClientSecret;
public function __construct() {
parent::__construct();
$this->oauthService = new UriOAuthService(null);
}
/**
* Includes the client secret.
*
* @param string $clientSecret
* @return OAuthWithAuthorizationCode this builder
*/
public function withClientSecret($clientSecret) {
$this->ClientSecret = $clientSecret;
return $this;
}
/**
* Includes the state.
*
* @param string $state
* @return OAuthWithAuthorizationCode this builder
*/
public function withState($state) {
$this->State = $state;
return $this;
}
/**
* Gets the Microsoft Account authorization endpoint where the user should be navigated to give their consent.
*
* @return string
*/
public function GetAuthorizationEndpoint(){
$oauthUrlParameters = new OAuthUrlParameters();
$oauthUrlParameters->ClientId = $this->ClientId;
$oauthUrlParameters->ResponseType = "code";
$oauthUrlParameters->RedirectUri = $this->RedirectUri;
$oauthUrlParameters->State = $this->State;
return UriOAuthService::GetAuthorizationEndpoint(
$oauthUrlParameters,
$this->Environment,
$this->RequireLiveConnect,
$this->Tenant);
}
/**
* Retrieves OAuth access and refresh tokens from the Microsoft Account authorization service
* using the specified authorization response redirect Uri.
*
* @param string $responseUri
* @param array $additionalParams
*
* @return OAuthTokens
* @throws Exception
* @throws OAuthTokenRequestException
*/
public function RequestOAuthTokensByResponseUri($responseUri, $additionalParams=null)
{
if ($responseUri == null)
{
throw new Exception("The ResponseUri is not set. ");
}
$parsed_url = parse_url($responseUri);
parse_str($parsed_url["query"], $queryParts);
if (array_key_exists("error", $queryParts))
{
$errorName = $queryParts['error'];
$errorDesc = $queryParts['error_description'];
throw (new OAuthTokenRequestException())
->withError($errorName)
->withDescription($errorDesc);
}
if (!array_key_exists("code", $queryParts))
{
throw new Exception("UriDoesntContainCode");
}
$code = $queryParts["code"];
$oauthRequestParameters = (new OAuthRequestParameters())
->withClientId($this->ClientId)
->withClientSecret($this->ClientSecret)
->withRedirectUri($this->RedirectUri)
->withGrantType("authorization_code")
->withGrantParamName("code")
->withGrantValue($code);
$this->OAuthTokens = $this->oauthService->GetAccessTokens(
$oauthRequestParameters,
$this->Environment,
$this->RequireLiveConnect,
$this->Tenant,
$additionalParams
);
return $this->OAuthTokens;
}
/**
* Retrieves OAuth access and refresh tokens from the Microsoft Account authorization service
* using the specified refresh token.
*
* @param string $refreshToken
* @param array $additionalParams
*
* @return OAuthTokens
* @throws Exception
*/
public function RequestOAuthTokensByRefreshToken($refreshToken, $additionalParams=null)
{
if ($refreshToken == null)
{
throw new Exception("The RefreshToken is not set. ");
}
$oauthRequestParameters = (new OAuthRequestParameters())
->withClientId($this->ClientId)
->withClientSecret($this->ClientSecret)
->withGrantType("refresh_token")
->withGrantParamName("refresh_token")
->withGrantValue($refreshToken);
$this->OAuthTokens = $this->oauthService->GetAccessTokens(
$oauthRequestParameters,
$this->Environment,
$this->RequireLiveConnect,
$this->Tenant,
$additionalParams
);
return $this->OAuthTokens;
}
}