-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.php
More file actions
128 lines (108 loc) · 3.88 KB
/
agent.php
File metadata and controls
128 lines (108 loc) · 3.88 KB
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
<?php
/**
* A-Select (Agent) functionality for communicating with an A-Select Server.
*
* This code can be used for A-Select user authentication in stand-alone
* PHP applications, without requiring running an additional (Java-based)
* A-Select agent on the same host.
*
* @copyright 2008 SURFnet BV
* @version $Id: agent.php 144 2010-03-09 09:38:34Z hansz $
*/
include_once('utils.php');
/**
* Initiate authentication to A-Select server.
*/
function as_authenticate($cfg, $app_url = NULL, $remote_organization = NULL, $home_organization = NULL, $forced_logon = FALSE) {
$req = as_message_create(
array(
// NB: order is important here, when signing is enabled
'request' => 'authenticate',
'a-select-server' => $cfg['server']['server_id'],
'app_id' => $cfg['client']['app_id'],
'app_url' => isset($app_url) ? $app_url : as_get_self_url($_SERVER['REQUEST_URI']),
'forced_logon' => $forced_logon ? 'true' : 'false',
'remote_organization' => $remote_organization,
),
array_key_exists('key', $cfg['client']) ? $cfg['client']['key'] : NULL
);
// establish an authentication session with the server
$rsp = as_call($cfg['server']['url'], $req, array_key_exists('ssl', $cfg['server']) ? $cfg['server']['ssl'] : NULL);
if ($rsp['result_code'] != '0000') {
throw new Exception('Request on remote server returned error: ' . $rsp['_result']);
}
// redirect to the actual login page as returned by the A-Select server
$redirect = $rsp['as_url'] . '&' . as_message_create(
array(
'a-select-server' => $cfg['server']['server_id'],
'rid' => $rsp['rid'],
'home_organization' => $home_organization,
)
);
header('Location: ' . $redirect);
exit;
}
/**
* Handle browser return redirect from remote A-Select server.
*/
function as_authenticate_return($cfg) {
$result = array();
$credentials = $_GET['aselect_credentials'];
$rid = $_GET['rid'];
if ( (!isset($credentials)) || (!isset($rid)) ) {
throw new Exception('Error on return from login at remote server!');
}
$req = as_message_create(
array(
// NB: order is important here, when signing is enabled
'request' => 'verify_credentials',
'a-select-server' => $cfg['server']['server_id'],
'aselect_credentials' => $credentials,
'rid' => $rid,
),
array_key_exists('key', $cfg['client']) ? $cfg['client']['key'] : NULL
);
$rsp = as_call($cfg['server']['url'], $req, array_key_exists('ssl', $cfg['server']) ? $cfg['server']['ssl'] : NULL);
if ($rsp['result_code'] != '0000') {
if ($rsp['result_code'] == '0040') {
throw new Exception('Login cancelled.');
}
throw new Exception('Request on remote server (' . $cfg['server']['url'] . ') returned error: ' . $rsp['_result']);
}
$result['uid'] = $rsp['uid'];
$result['organization'] = $rsp['organization'];
if (array_key_exists('attributes', $rsp)) {
$decoded = base64_decode($rsp['attributes']);
$attributes = array();
foreach (explode('&', $decoded) as $parm) {
$tuple = explode('=', $parm);
$name = urldecode($tuple[0]);
if (substr($name, strlen($name) - 2, 2) == '[]') {
$name = substr($name, 0, strlen($name) - 2);
}
if (!array_key_exists($name, $attributes)) {
$attributes[$name] = array();
}
$attributes[$name][] = urldecode($tuple[1]);
}
$result['attributes'] = $attributes;
}
return $result;
}
/**
* Perform the actual authentication (called from applications)
*/
function as_process($cfg, $app_url = NULL, $remote_organization = NULL, $home_organization = NULL, $forced_logon = FALSE) {
$result = NULL;
if (!array_key_exists('aselect_credentials', $_GET)) {
as_authenticate($cfg, $app_url, $remote_organization, $home_organization, $forced_logon);
} else {
$result = as_authenticate_return($cfg);
}
return $result;
}
function as_logout($cfg) {
header('Location: ' . $cfg['server']['url'] . '?request=logout&app_id=' . $cfg['client']['app_id']);
exit;
}
?>