forked from psignoret/aad-sso-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthorizationHelper.php
227 lines (190 loc) · 6.44 KB
/
AuthorizationHelper.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/**
* A helper class used to request authorization and access tokens Azure Active Directory.
*/
class AADSSO_AuthorizationHelper
{
/**
* @var string List of allowed algorithms. Currently, only RS256 is allowed and expected from AAD.
*/
private static $allowed_algorithms = array( 'RS256' );
/**
* Gets the authorization URL used to makes the authorization request.
*
* @param \AADSSO_Settings $settings The settings to use.
* @param string $antiforgery_id The value to use as the nonce.
*
* @return string The authorization URL.
*/
public static function get_authorization_url( $settings, $antiforgery_id ) {
$auth_url = $settings->authorization_endpoint . '?'
. http_build_query( array(
'response_type' => 'code',
'scope' => 'openid',
'domain_hint' => $settings->org_domain_hint,
'client_id' => $settings->client_id,
'resource' => $settings->graph_endpoint,
'redirect_uri' => $settings->redirect_uri,
'state' => $antiforgery_id,
'nonce' => $antiforgery_id,
) );
return $auth_url;
}
/**
* Exchanges an Authorization Code and obtains an Access Token and an ID Token.
*
* @param string $code The authorization code.
* @param \AADSSO_Settings $settings The settings to use.
*
* @return mixed The decoded authorization result.
*/
public static function get_access_token( $code, $settings ) {
// Construct the body for the access token request
$authentication_request_body = http_build_query(
array(
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => $settings->redirect_uri,
'resource' => $settings->graph_endpoint,
'client_id' => $settings->client_id,
'client_secret' => $settings->client_secret
)
);
return self::get_and_process_access_token( $authentication_request_body, $settings );
}
/**
* Makes the request for the access token and some does some basic processing of the result.
*
* @param array $authentication_request_body The body to use in the Authentication Request.
* @param \AADSSO_Settings $settings The settings to use.
*
* @return mixed The decoded authorization result.
*/
public static function get_and_process_access_token( $authentication_request_body, $settings ) {
// Post the authorization code to the STS and get back the access token
$response = wp_remote_post( $settings->token_endpoint, array(
'body' => $authentication_request_body
) );
if( is_wp_error( $response ) ) {
return new WP_Error( $response->get_error_code(), $response->get_error_message() );
}
$output = wp_remote_retrieve_body( $response );
// Decode the JSON response from the STS. If all went well, this will contain the access
// token and the id_token (a JWT token telling us about the current user)
$result = json_decode( $output );
if ( isset( $result->access_token ) ) {
// Add the token information to the session so that we can use it later
// TODO: these probably shouldn't be in SESSION...
$_SESSION['aadsso_token_type'] = $result->token_type;
$_SESSION['aadsso_access_token'] = $result->access_token;
}
return $result;
}
/**
* Decodes an id_token value returned
*
* @param string $id_token The JWT token
* @param \AADSSO_Settings $settings The settings to use.
*
* @return mixed The decoded authorization result.
*/
private static function decode_token( $id_token, $settings) {
$jwt = null;
$last_exception = null;
// TODO: cache the keys
$jwks = wp_remote_retrieve_body( wp_remote_get( $settings->jwks_uri ) );
$discovery = json_decode( $jwks );
if ( null == $discovery->keys ) {
throw new DomainException( 'jwks_uri does not contain the keys attribute' );
}
foreach ( $discovery->keys as $key ) {
try {
if ( null == $key->x5c ) {
throw new DomainException( 'key does not contain the x5c attribute' );
}
$key_der = $key->x5c[0];
/* Per section 4.7 of the current JWK draft [1], the 'x5c' property will be the
* DER-encoded value of the X.509 certificate. PHP's openssl functions all require
* a PEM-encoded value.
*/
$key_pem = chunk_split( $key_der, 64, "\n" );
$key_pem = "-----BEGIN CERTIFICATE-----\n"
. $key_pem
. "-----END CERTIFICATE-----\n";
// This throws an exception if the id_token cannot be validated.
$jwt = \AADSSO\Firebase\JWT\JWT::decode( $id_token, $key_pem, self::$allowed_algorithms );
break;
} catch ( Exception $e ) {
$last_exception = $e;
}
}
if ( null == $jwt ) {
throw $last_exception;
}
return $jwt;
}
/**
* Validates claims of bearer_token
*
* @param string $jwt The JWT token
* @param \AADSSO_Settings $settings The settings to use.
*
*/
private static function validate_token_claims( $jwt, $settings){
$audience = $jwt->aud;
if ($audience != $settings->resource_id) {
throw new \AADSSO\Firebase\JWT\SignatureInvalidException('Invalid Audience');
}
// The Issuer Identifier for the OpenID Provider MUST exactly match the value of the iss (issuer) Claim.
$iss_token = $jwt->iss;
$iss_metadata = $settings->issuer;
if ($iss_token != $iss_metadata) {
throw new \AADSSO\Firebase\JWT\SignatureInvalidException('Signature invalid');
}
}
/**
* Validates signature and claims of a bearer_token value returned
*
* @param string $bearer_token The bearer token
* @param \AADSSO_Settings $settings The settings to use.
*
* @return mixed The decoded authorization result.
*/
public static function validate_bearer_token( $bearer_token, $settings) {
$jwt = null;
$last_exception = null;
try{
$jwt = self::decode_token( $bearer_token, $settings);
self::validate_token_claims( $jwt, $settings);
}
catch ( Exception $e ) {
throw $e;
}
return $jwt;
}
/**
* Decodes and validates an id_token value returned
*
* @param string $id_token The token
* @param \AADSSO_Settings $settings The settings to use.
*
* @return mixed The decoded authorization result.
*/
public static function validate_id_token( $id_token, $settings, $antiforgery_id) {
$jwt = null;
$last_exception = null;
try{
$jwt = self::decode_token( $id_token, $settings);
}
catch ( Exception $e ) {
$last_exception = $e;
}
if ( null == $jwt ) {
throw $last_exception;
}
if ( $jwt->nonce != $antiforgery_id ) {
throw new DomainException( sprintf( 'Nonce mismatch. Expecting %s', $antiforgery_id ) );
}
return $jwt;
}
}