ValidateResponse.php
2.63 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
<?php
/**
* CAS ticket validation response class.
*
* @version 1.2.0
* @since 1.2.0
*/
namespace Cassava\CAS\Response;
use Cassava\CAS;
use Cassava\Options;
use Cassava\Plugin;
/**
* Implements the CAS response for validation requests.
*
* @version 1.2.0
*/
class ValidateResponse extends BaseResponse {
/**
* XML success response to a CAS 2.0 validation request.
*
* @param \Cassava\CAS\Ticket $ticket Validated ticket.
* @param string $proxyGrantingTicket Generated Proxy-Granting Ticket (PGT) to return.
* @param array $proxies List of proxy URIs.
*
* @todo Throw exception on bad or no ticket.
*/
public function setTicket( CAS\Ticket $ticket, $proxyGrantingTicket = '', $proxies = array() ) {
$this->response = $this->createElement( 'authenticationSuccess' );
// Include login name:
$this->response->appendChild( $this->createElement( 'user', $ticket->user->user_login ) );
// Include user attributes:
$this->setUserAttributes( $ticket );
// Include Proxy-Granting Ticket in successful `/proxyValidate` responses:
if ( $proxyGrantingTicket ) {
$this->response->appendChild( $this->createElement(
'proxyGrantingTicket', $proxyGrantingTicket ) );
}
// Include proxies in successful `/proxyValidate` responses:
if ( ! empty( $proxies ) ) {
$xmlProxies = $this->createElement( 'proxies' );
foreach ($proxies as $proxy) {
$xmlProxies->appendChild( $this->createElement(
'proxy', $proxy ) );
}
$this->response->appendChild( $xmlProxies );
}
}
/**
* Add user attributes to the response.
*
* @param CAS\Ticket $ticket Validated ticket.
*
* @uses \apply_filters()
*/
protected function setUserAttributes( CAS\Ticket $ticket ) {
$attributeKeys = Options::get( 'attributes' );
$attributes = array();
foreach ( $attributeKeys as $key ) {
$attributes[ $key ] = implode( ',', (array) $ticket->user->get( $key ) );
}
/**
* Allows developers to change the list of (key, value) pairs before they're included
* in a `/serviceValidate` response.
*
* @param array $attributes List of attributes to output.
* @param WP_User $user Authenticated user.
*/
$attributes = \apply_filters( 'cas_server_validation_user_attributes', $attributes, $ticket->user );
if ( ! is_array( $attributes ) || empty( $attributes ) ) {
return;
}
$xmlAttributes = $this->createElement( 'attributes' );
foreach ($attributes as $key => $value) {
$xmlAttribute = $this->createElement( $key, $value );
$xmlAttributes->appendChild( $xmlAttribute );
}
$this->response->appendChild( $xmlAttributes );
}
}