-
Notifications
You must be signed in to change notification settings - Fork 78
/
GraphHelper.php
123 lines (103 loc) · 3.41 KB
/
GraphHelper.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
<?php
/**
* A helper class used to make calls to Microsoft Graph API.
*/
class AADSSO_GraphHelper
{
/**
* @var \AADSSO_Settings The instance of AADSSO_Settings to use.
*/
public static $settings;
/**
* Gets the the Microsoft Graph API base URL to use.
*
* @return string The base URL to the Microsoft Graph API.
*/
public static function get_base_url() {
return self::$settings->graph_endpoint . '/' . self::$settings->graph_version;
}
/**
* Checks which of the given groups the given user is a member of.
*
* @return mixed The response to the checkMemberGroups request.
*/
public static function user_check_member_groups( $user_id, $group_ids ) {
$url = self::get_base_url() . '/users/' . $user_id . '/checkMemberGroups';
return self::post_request( $url, array(), array( 'groupIds' => $group_ids ) );
}
/**
* Gets the requested user.
*
* @return mixed The response to the user request.
*/
public static function get_user( $user_id ) {
$url = self::get_base_url() . '/users/' . $user_id;
return self::get_request( $url );
}
/**
* Issues a GET request to the Microsoft Graph API.
*
* @return mixed The decoded response.
*/
public static function get_request( $url, $query_params = array() ) {
// Build the full query URL, adding api-version if necessary
$query_params = http_build_query( $query_params );
$url = $url . '?' . $query_params;
$_SESSION['aadsso_last_request'] = array(
'method' => 'GET',
'url' => $url,
);
AADSSO::debug_log( 'GET ' . $url, 50 );
// Make the GET request
$response = wp_remote_get( $url, array(
'headers' => self::get_required_headers_and_settings(),
) );
return self::parse_and_log_response( $response );
}
/**
* Issues a POST request to the Microsoft Graph API.
*
* @return mixed The decoded response.
*/
public static function post_request( $url, $query_params = array(), $data = array() ) {
// Build the full query URL and encode the payload
$query_params = http_build_query( $query_params );
$url = $url . '?' . $query_params;
$payload = json_encode( $data );
AADSSO::debug_log( 'POST ' . $url, 50 );
AADSSO::debug_log( $payload, 99 );
// Make the POST request
$response = wp_remote_post( $url, array(
'body' => $payload,
'headers' => self::get_required_headers_and_settings(),
) );
return self::parse_and_log_response( $response );
}
/**
* Logs the HTTP response headers and body and returns the JSON-decoded body.
*
* @return mixed The decoded response.
*/
private static function parse_and_log_response( $response ) {
$response_headers = wp_remote_retrieve_headers( $response );
$response_body = wp_remote_retrieve_body( $response );
AADSSO::debug_log( 'Response headers: ' . json_encode( $response_headers ), 99 );
AADSSO::debug_log( 'Response body: ' . json_encode( $response_body ), 50 );
return json_decode( $response_body );
}
/**
* Returns an array with the required headers like authorization header, service version etc.
*
* @return array An associative array with the HTTP headers for Microsoft Graph API calls.
*/
private static function get_required_headers_and_settings()
{
// Generate the authentication header
return array(
'Authorization' => $_SESSION['aadsso_token_type'] . ' ' . $_SESSION['aadsso_access_token'],
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Prefer' => 'return-content',
);
}
}