Skip to content

Commit f1758ed

Browse files
committed
GO5 Auth Module
1 parent 61b3abb commit f1758ed

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

modules/go5auth/default-disable

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This file indicates that the default state of this module
2+
is disabled. To enable, create a file named enable in the
3+
same directory as this file.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
use GuzzleHttp\Client as HttpClient;
4+
use GuzzleHttp\Exception\GuzzleException as HttpClientException;
5+
6+
class sspmod_go5auth_Auth_Source_LoggedUser extends SimpleSAML_Auth_Source
7+
{
8+
/**
9+
* @var HttpClient
10+
*/
11+
private $httpClient;
12+
13+
public function __construct(array $info, array $config)
14+
{
15+
parent::__construct($info, $config);
16+
17+
$this->httpClient = new HttpClient;
18+
}
19+
20+
public function authenticate(&$state)
21+
{
22+
if (!array_key_exists('access_token', $_REQUEST)
23+
&& !array_key_exists('HTTP_AUTHORIZATION', $_SERVER)
24+
) {
25+
throw new SimpleSAML_Error_Exception('go5auth | error: access_token is required');
26+
}
27+
28+
$accessToken = isset($_REQUEST['access_token'])
29+
? $_REQUEST['access_token']
30+
: trim(substr($_SERVER['HTTP_AUTHORIZATION'], 7));
31+
32+
SimpleSAML_Logger::debug('go5auth | access_token: ' . $accessToken);
33+
$userInfo = $this->getUserInfo($accessToken);
34+
35+
if ($userInfo->data->attributes->status != 'active') {
36+
throw new SimpleSAML_Error_Exception('go5auth | error: inactive user ' . $userInfo->data->id);
37+
}
38+
39+
$userAttributes = [
40+
'email' => $userInfo->data->attributes->email,
41+
'employee-id' => $userInfo->data->attributes->{'employee-id'},
42+
'document' => $userInfo->data->attributes->{'document'},
43+
'document-type' => $userInfo->data->attributes->{'document-type'},
44+
'name' => $userInfo->data->attributes->{'name'},
45+
'last-name' => $userInfo->data->attributes->{'last-name'},
46+
];
47+
$state['Attributes'] = SimpleSAML_Utilities::parseAttributes($userAttributes);
48+
49+
SimpleSAML_Auth_Source::completeAuth($state);
50+
}
51+
52+
private function getUserInfo($token)
53+
{
54+
try {
55+
$tokenResponse = $this->httpClient->get(URL_PREFIX . '/oauth/token?access_token=' . $token);
56+
$tokenInfo = json_decode($tokenResponse->getBody()->getContents());
57+
$userResponse = $this->httpClient->get(
58+
BASE_URI_USER_SDK . '/users/' . $tokenInfo->user_id, [
59+
'headers' => [
60+
'x-go5-platform-id' => $tokenInfo->platform_id,
61+
'x-app-sdk' => 1,
62+
]]);
63+
64+
return json_decode($userResponse->getBody()->getContents());
65+
66+
} catch (HttpClientException $e) {
67+
throw new SimpleSAML_Error_Exception('go5auth | error: ' . $e->getMessage());
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)