1<?php
2
3/**
4 * Licensed to Jasig under one or more contributor license
5 * agreements. See the NOTICE file distributed with this work for
6 * additional information regarding copyright ownership.
7 *
8 * Jasig licenses this file to you under the Apache License,
9 * Version 2.0 (the "License"); you may not use this file except in
10 * compliance with the License. You may obtain a copy of the License at:
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 * PHP Version 7
21 *
22 * @file     CAS/ProxiedService/Abstract.php
23 * @category Authentication
24 * @package  PhpCAS
25 * @author   Adam Franco <afranco@middlebury.edu>
26 * @license  http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
27 * @link     https://wiki.jasig.org/display/CASC/phpCAS
28 */
29
30/**
31 * This class implements common methods for ProxiedService implementations included
32 * with phpCAS.
33 *
34 * @class    CAS_ProxiedService_Abstract
35 * @category Authentication
36 * @package  PhpCAS
37 * @author   Adam Franco <afranco@middlebury.edu>
38 * @license  http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
39 * @link     https://wiki.jasig.org/display/CASC/phpCAS
40 */
41abstract class CAS_ProxiedService_Abstract
42implements CAS_ProxiedService, CAS_ProxiedService_Testable
43{
44
45    /**
46     * The proxy ticket that can be used when making service requests.
47     * @var string $_proxyTicket;
48     */
49    private $_proxyTicket;
50
51    /**
52     * Register a proxy ticket with the Proxy that it can use when making requests.
53     *
54     * @param string $proxyTicket proxy ticket
55     *
56     * @return void
57     * @throws InvalidArgumentException If the $proxyTicket is invalid.
58     * @throws CAS_OutOfSequenceException If called after a proxy ticket has
59     *         already been initialized/set.
60     */
61    public function setProxyTicket ($proxyTicket)
62    {
63        if (empty($proxyTicket)) {
64            throw new CAS_InvalidArgumentException(
65                'Trying to initialize with an empty proxy ticket.'
66            );
67        }
68        if (!empty($this->_proxyTicket)) {
69            throw new CAS_OutOfSequenceException(
70                'Already initialized, cannot change the proxy ticket.'
71            );
72        }
73        $this->_proxyTicket = $proxyTicket;
74    }
75
76    /**
77     * Answer the proxy ticket to be used when making requests.
78     *
79     * @return string
80     * @throws CAS_OutOfSequenceException If called before a proxy ticket has
81     * already been initialized/set.
82     */
83    protected function getProxyTicket ()
84    {
85        if (empty($this->_proxyTicket)) {
86            throw new CAS_OutOfSequenceException(
87                'No proxy ticket yet. Call $this->initializeProxyTicket() to aquire the proxy ticket.'
88            );
89        }
90
91        return $this->_proxyTicket;
92    }
93
94    /**
95     * @var CAS_Client $_casClient;
96     */
97    private $_casClient;
98
99    /**
100     * Use a particular CAS_Client->initializeProxiedService() rather than the
101     * static phpCAS::initializeProxiedService().
102     *
103     * This method should not be called in standard operation, but is needed for unit
104     * testing.
105     *
106     * @param CAS_Client $casClient cas client
107     *
108     * @return void
109     * @throws CAS_OutOfSequenceException If called after a proxy ticket has
110     * already been initialized/set.
111     */
112    public function setCasClient (CAS_Client $casClient)
113    {
114        if (!empty($this->_proxyTicket)) {
115            throw new CAS_OutOfSequenceException(
116                'Already initialized, cannot change the CAS_Client.'
117            );
118        }
119
120        $this->_casClient = $casClient;
121    }
122
123    /**
124     * Fetch our proxy ticket.
125     *
126     * Descendent classes should call this method once their service URL is available
127     * to initialize their proxy ticket.
128     *
129     * @return void
130     * @throws CAS_OutOfSequenceException If called after a proxy ticket has
131     * already been initialized.
132     */
133    protected function initializeProxyTicket()
134    {
135        if (!empty($this->_proxyTicket)) {
136            throw new CAS_OutOfSequenceException(
137                'Already initialized, cannot initialize again.'
138            );
139        }
140        // Allow usage of a particular CAS_Client for unit testing.
141        if (empty($this->_casClient)) {
142            phpCAS::initializeProxiedService($this);
143        } else {
144            $this->_casClient->initializeProxiedService($this);
145        }
146    }
147
148}
149?>
150