-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathCrowd.php
More file actions
208 lines (177 loc) · 7.47 KB
/
Crowd.php
File metadata and controls
208 lines (177 loc) · 7.47 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/**
* PHP Client Library for Atlassian Crowd
*
* Copyright (C) 2008 Infinite Campus, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Crowd SecurityServer SOAP Client
*/
class Crowd {
private $crowd_client;
private $crowd_config;
private $crowd_app_token;
/**
* Create an application client using the passed in configuration parameters.
*/
function Crowd($config) {
$this->crowd_config = $config;
// Create the Crowd SOAP client
try {
$this->crowd_client = new SoapClient($this->crowd_config['service_url']);
} catch (SoapFault $fault) {
$faultcode = $fault->getCode();
$faultstring = $fault->getMessage();
echo "SOAP Fault: faultcode: {$faultcode}, faultstring: {$faultstring}";
throw new CrowdConnectionException("Unable to connect to Crowd. Verify the service_url property is defined and Crowd is running.");
}
}
/**
* Authenticates an application client to the Crowd security server.
*/
function authenticateApplication() {
$param = array('in0' => array('credential' => array('credential' => $this->crowd_config['app_credential']),
'name' => $this->crowd_config['app_name']));
try {
$resp = $this->crowd_client->authenticateApplication($param);
} catch (SoapFault $fault) {
$faultcode = $fault->getCode();
$faultstring = $fault->getMessage();
echo "SOAP Fault: faultcode: {$faultcode}, faultstring: {$faultstring}";
}
$this->crowd_app_token = $resp->out->token;
if ( empty($this->crowd_app_token) ) {
throw new CrowdLoginException("Unable to login to Crowd. Verify the app_name and app_credential properties are defined and valid.");
}
else {
return $this->crowd_app_token;
}
}
/**
* Authenticates a principal to the Crowd security server for the application client.
*/
function authenticatePrincipal($name, $credential, $user_agent, $remote_address) {
// Build the parameter used to authenticate the principal
$param = array('in0' => array('name' => $this->crowd_config['app_name'],
'token' => $this->crowd_app_token),
'in1' => array('application' => $this->crowd_config['app_name'],
'credential' => array('credential' => $credential),
'name' => $name,
'validationFactors' => array( array('name' => 'User-Agent',
'value' => $user_agent),
array('name' => 'remote_address',
'value' => $remote_address) ) ) );
// Attempt to authenticate the user (principal) via Crowd.
try {
$resp = $this->crowd_client->authenticatePrincipal($param);
} catch (SoapFault $fault) {
$faultcode = $fault->getCode();
$faultstring = $fault->getMessage();
echo "SOAP Fault: faultcode: {$faultcode}, faultstring: {$faultstring}";
return null;
}
// Get the principal's token
$princ_token = $resp->out;
return $princ_token;
}
/**
* Determines if the principal's current token is still valid in Crowd.
*/
function isValidPrincipalToken($princ_token, $user_agent, $remote_address) {
// Determine if the principal is still valid in Crowd
$param = array('in0' => array('name' => $this->crowd_config['app_name'],
'token' => $this->crowd_app_token),
'in1' => $princ_token,
'in2' => array( array('name' => 'User-Agent',
'value' => $user_agent),
array('name' => 'remote_address',
'value' => $remote_address) ) );
try {
$resp = $this->crowd_client->isValidPrincipalToken($param);
} catch (SoapFault $fault) {
$faultcode = $fault->getCode();
$faultstring = $fault->getMessage();
echo "SOAP Fault: faultcode: {$faultcode}, faultstring: {$faultstring}";
return '';
}
$valid_token = $resp->out;
return $valid_token;
}
/**
* Invalidates a token for for this princpal for all application clients in Crowd.
*/
function invalidatePrincipalToken($princ_token) {
// Invalidate the principal's token in Crowd
$param = array('in0' => array('name' => $this->crowd_config['app_name'],
'token' => $this->crowd_app_token),
'in1' => $princ_token);
try {
$resp = $this->crowd_client->invalidatePrincipalToken($param);
return true;
} catch (SoapFault $fault) {
$faultcode = $fault->getCode();
$faultstring = $fault->getMessage();
echo "SOAP Fault: faultcode: {$faultcode}, faultstring: {$faultstring}";
}
return false;
}
/**
* Finds a principal by token.
*/
function findPrincipalByToken($princ_token) {
$param = array('in0' => array('name' => $this->crowd_config['app_name'],
'token' => $this->crowd_app_token),
'in1' => $princ_token);
try {
$resp = $this->crowd_client->findPrincipalByToken($param);
return $resp->out;
} catch (SoapFault $fault) {
$faultcode = $fault->getCode();
$faultstring = $fault->getMessage();
echo "SOAP Fault: faultcode: {$faultcode}, faultstring: {$faultstring}";
return null;
}
}
/**
* Finds all of the groups the specified principal is in.
*/
function findGroupMemberships($princ_name) {
$param = array('in0' => array('name' => $this->crowd_config['app_name'],
'token' => $this->crowd_app_token),
'in1' => $princ_name);
try {
$resp = $this->crowd_client->findGroupMemberships($param);
return $resp->out;
} catch (SoapFault $fault) {
$faultcode = $fault->getCode();
$faultstring = $fault->getMessage();
echo "SOAP Fault: faultcode: {$faultcode}, faultstring: {$faultstring}";
return null;
}
}
}
/**
* Exception used to indicate a problem connecting to the Crowd server.
*/
class CrowdConnectionException extends Exception
{
}
/**
* Exception used to incidate a problem authenticating to the Crowd server.
*/
class CrowdLoginException extends Exception
{
}
?>