Skip to content

Commit

Permalink
MSFTMPP-186: Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmcq committed Jun 2, 2015
1 parent 020f503 commit 09c57c3
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 79 deletions.
1 change: 0 additions & 1 deletion local/o365/classes/oauth2/token.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ public static function instance($userid, $resource, $clientdata, $httpclient) {
public static function get_for_new_resource($userid, $resource, $clientdata, $httpclient) {
$aadgraphtoken = static::instance($userid, 'https://graph.windows.net', $clientdata, $httpclient);
if (!empty($aadgraphtoken)) {
$aadgraphtoken->refresh();
$params = [
'client_id' => $clientdata->get_clientid(),
'client_secret' => $clientdata->get_clientsecret(),
Expand Down
78 changes: 0 additions & 78 deletions local/o365/classes/observers.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,6 @@ public static function handle_oidc_user_authed(\auth_oidc\event\user_authed $eve
* @return bool Success/Failure.
*/
public static function handle_oidc_user_connected(\auth_oidc\event\user_connected $event) {
// Get additional tokens for the user.
$eventdata = $event->get_data();
if (!empty($eventdata['other']['username']) && !empty($eventdata['userid'])) {
$tokenresult = static::get_additional_tokens_for_user($eventdata['other']['username'], $eventdata['userid']);
}

return true;
}

Expand Down Expand Up @@ -140,7 +134,6 @@ public static function handle_oidc_user_loggedin(\auth_oidc\event\user_loggedin
$eventdata = $event->get_data();
if (!empty($eventdata['other']['username']) && !empty($eventdata['userid'])) {
static::get_additional_user_info($eventdata['userid']);
static::get_additional_tokens_for_user($eventdata['other']['username'], $eventdata['userid']);
}

return true;
Expand Down Expand Up @@ -209,77 +202,6 @@ public static function get_additional_user_info($userid) {
return false;
}

/**
* Get additional tokens for a given user.
*
* @param string $username The username of the user to fetch OpenID Connect tokens for.
* @param int $userid The ID of the user to store the new tokens for.
* @return bool Success/Failure.
*/
public static function get_additional_tokens_for_user($username, $userid) {
global $DB;

// Auth_oidc config gives us the client credentials and token endpoint.
$oidcconfig = get_config('auth_oidc');
if (empty($oidcconfig)) {
return false;
}
if (empty($oidcconfig->clientid) || empty($oidcconfig->clientsecret) || empty($oidcconfig->tokenendpoint)) {
return false;
}

// The token record created/updated on login by auth_oidc.
$oidctokenrec = $DB->get_record('auth_oidc_token', ['username' => $username]);
if (empty($oidctokenrec) || empty($oidctokenrec->refreshtoken)) {
return false;
}

// Assemble resources.
$resources = [\local_o365\rest\calendar::get_resource()];
if (\local_o365\rest\onedrive::is_configured() !== false) {
$resources[] = \local_o365\rest\onedrive::get_resource();
}
if (\local_o365\rest\sharepoint::is_configured() !== false) {
$resources[] = \local_o365\rest\sharepoint::get_resource();
}

foreach ($resources as $resource) {
// Request token.
$httpclient = new \local_o365\httpclient();
$params = [
'client_id' => $oidcconfig->clientid,
'client_secret' => $oidcconfig->clientsecret,
'grant_type' => 'refresh_token',
'refresh_token' => $oidctokenrec->refreshtoken,
'resource' => $resource,
];
$tokenresult = $httpclient->post($oidcconfig->tokenendpoint, $params);
$tokenresult = @json_decode($tokenresult, true);
if (empty($tokenresult) || !is_array($tokenresult)) {
return false;
}

// Create/update the stored token record.
$o365tokenrec = $DB->get_record('local_o365_token', ['user_id' => $userid, 'resource' => $resource]);
if (!empty($o365tokenrec)) {
$o365tokenrec->scope = $tokenresult['scope'];
$o365tokenrec->token = $tokenresult['access_token'];
$o365tokenrec->expiry = $tokenresult['expires_on'];
$o365tokenrec->refreshtoken = $tokenresult['refresh_token'];
$DB->update_record('local_o365_token', $o365tokenrec);
} else {
$o365tokenrec = new \stdClass;
$o365tokenrec->user_id = $userid;
$o365tokenrec->resource = $tokenresult['resource'];
$o365tokenrec->scope = $tokenresult['scope'];
$o365tokenrec->token = $tokenresult['access_token'];
$o365tokenrec->expiry = $tokenresult['expires_on'];
$o365tokenrec->refreshtoken = $tokenresult['refresh_token'];
$o365tokenrec->id = $DB->insert_record('local_o365_token', $o365tokenrec);
}
}
}

/**
* Construct a calendar API client using the system API user.
*
Expand Down

0 comments on commit 09c57c3

Please sign in to comment.