ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
LiveID.php
Go to the documentation of this file.
1<?php
2
11{
12
16 const STAGE_INIT = 'authwindowslive:init';
17
21 const AUTHID = 'authwindowslive:AuthId';
22
23 private $key;
24 private $secret;
25
26
35 public function __construct($info, $config)
36 {
37 assert(is_array($info));
38 assert(is_array($config));
39
40 // Call the parent constructor first, as required by the interface
41 parent::__construct($info, $config);
42
43 if (!array_key_exists('key', $config)) {
44 throw new Exception('LiveID authentication source is not properly configured: missing [key]');
45 }
46
47 $this->key = $config['key'];
48
49 if (!array_key_exists('secret', $config)) {
50 throw new Exception('LiveID authentication source is not properly configured: missing [secret]');
51 }
52
53 $this->secret = $config['secret'];
54 }
55
56
62 public function authenticate(&$state)
63 {
64 assert(is_array($state));
65
66 // we are going to need the authId in order to retrieve this authentication source later
68
69 $stateID = SimpleSAML_Auth_State::saveState($state, self::STAGE_INIT);
70
71 SimpleSAML\Logger::debug('authwindowslive auth state id = ' . $stateID);
72
73 // authenticate the user
74 // documentation at:
75 // https://azure.microsoft.com/en-us/documentation/articles/active-directory-v2-protocols-oauth-code/
76 $authorizeURL = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize'
77 . '?client_id=' . $this->key
78 . '&response_type=code'
79 . '&response_mode=query'
80 . '&redirect_uri=' . urlencode(SimpleSAML\Module::getModuleUrl('authwindowslive') . '/linkback.php')
81 . '&state=' . urlencode($stateID)
82 . '&scope=' . urlencode('openid https://graph.microsoft.com/user.read')
83 ;
84
86 }
87
88
94 public function finalStep(&$state)
95 {
97 "authwindowslive oauth: Using this verification code [".$state['authwindowslive:verification_code']."]"
98 );
99
100 // retrieve Access Token
101 // documentation at:
102 // https://azure.microsoft.com/en-us/documentation/articles/active-directory-v2-protocols-oauth-code/#request-an-access-token
103 $postData = 'client_id=' . urlencode($this->key)
104 . '&client_secret=' . urlencode($this->secret)
105 . '&scope=' . urlencode('https://graph.microsoft.com/user.read')
106 . '&grant_type=authorization_code'
107 . '&redirect_uri=' . urlencode(SimpleSAML\Module::getModuleUrl('authwindowslive') . '/linkback.php')
108 . '&code=' . urlencode($state['authwindowslive:verification_code']);
109
110 $context = array(
111 'http' => array(
112 'method' => 'POST',
113 'header' => 'Content-type: application/x-www-form-urlencoded',
114 'content' => $postData,
115 ),
116 );
117
118 $result = \SimpleSAML\Utils\HTTP::fetch('https://login.microsoftonline.com/common/oauth2/v2.0/token', $context);
119
120 $response = json_decode($result, true);
121
122 // error checking of $response to make sure we can proceed
123 if (!array_key_exists('access_token', $response)) {
124 throw new Exception(
125 '['.$response['error'].'] '.$response['error_description'].
126 "\r\nNo access_token returned - cannot proceed\r\n" . implode(', ', $response['error_codes'])
127 );
128 }
129
131 "authwindowslive: Got an access token from the OAuth service provider [".$response['access_token']."]"
132 );
133
134 // documentation at: http://graph.microsoft.io/en-us/docs/overview/call_api
135 $opts = array('http' => array('header' => "Accept: application/json\r\nAuthorization: Bearer ".
136 $response['access_token']."\r\n"));
137 $data = \SimpleSAML\Utils\HTTP::fetch('https://graph.microsoft.com/v1.0/me', $opts);
138 $userdata = json_decode($data, true);
139
140 // this is the simplest case
141 if (!array_key_exists('@odata.context', $userdata) || array_key_exists('error', $userdata)) {
142 throw new Exception(
143 'Unable to retrieve userdata from Microsoft Graph ['.$userdata['error']['code'].'] '.
144 $userdata['error']['message']
145 );
146 }
147 $attributes = array();
148 $attributes['windowslive_targetedID'] = array(
149 'https://graph.microsoft.com!'.(!empty($userdata['id']) ? $userdata['id'] : 'unknown')
150 );
151 foreach ($userdata as $key => $value) {
152 if (is_string($value)) {
153 $attributes['windowslive.' . $key] = array((string)$value);
154 }
155 }
156
157
158 SimpleSAML\Logger::debug('LiveID Returned Attributes: '. implode(", ", array_keys($attributes)));
159
160 $state['Attributes'] = $attributes;
161 }
162}
$result
if(!array_key_exists('stateid', $_REQUEST)) $state
Handle linkback() response from LinkedIn.
Definition: linkback.php:10
An exception for terminatinating execution or to throw for unit testing.
static debug($string)
Definition: Logger.php:211
static fetch($url, $context=array(), $getHeaders=false)
Helper function to retrieve a file or URL with proxy support, also supporting proxy basic authorizati...
Definition: HTTP.php:408
static redirectTrustedURL($url, $parameters=array())
This function redirects to the specified URL without performing any security checks.
Definition: HTTP.php:959
static saveState(&$state, $stage, $rawId=false)
Save the state.
Definition: State.php:194
__construct($info, $config)
Constructor for this authentication source.
Definition: LiveID.php:35
const AUTHID
The key of the AuthId field in the state.
Definition: LiveID.php:21
authenticate(&$state)
Log-in using LiveID platform.
Definition: LiveID.php:62
const STAGE_INIT
The string used to identify our states.
Definition: LiveID.php:16
if(array_key_exists('yes', $_REQUEST)) $attributes
Definition: getconsent.php:85
$config
Definition: bootstrap.php:15
$info
Definition: index.php:5
Attribute-related utility methods.
if($session===NULL) $postData
$response
$data
Definition: bench.php:6
$context
Definition: webdav.php:25