]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OpenID/extlib/teams-extension.php
OpenID access control options: trusted provider URL, Launchpad team restrictions...
[quix0rs-gnu-social.git] / plugins / OpenID / extlib / teams-extension.php
1 <?php
2 /*
3  *  Wordpress Teams plugin
4  *  Copyright (C) 2009-2010 Canonical Ltd.
5  *
6  *  This program is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU Affero General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Affero General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Affero General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /**
21  * Provides an example OpenID extension to query user team/group membership
22  *
23  * This code is based on code supplied with the openid library for simple
24  * registration data.
25  */
26
27 /**
28  * Require the Message implementation.
29  */
30 require_once 'Auth/OpenID/Message.php';
31 require_once 'Auth/OpenID/Extension.php';
32
33 /**
34  * The team/group extension base class
35  */
36 class Auth_OpenID_TeamsExtension extends Auth_OpenID_Extension {
37   var $ns_uri = 'http://ns.launchpad.net/2007/openid-teams';
38   var $ns_alias = 'lp';
39   var $request_field = 'query_membership';
40   var $response_field = 'is_member';
41   
42   /**
43    * Get the string arguments that should be added to an OpenID
44    * message for this extension.
45    */
46   function getExtensionArgs() {
47     $args = array();
48
49     if ($this->_teams) {
50       $args[$this->request_field] = implode(',', $this->_teams);
51     }
52
53     return $args;
54   }
55
56   /**
57    * Add the arguments from this extension to the provided message.
58    *
59    * Returns the message with the extension arguments added.
60    */
61   function toMessage(&$message) {
62     if ($message->namespaces->addAlias($this->ns_uri, $this->ns_alias) === null) {
63       if ($message->namespaces->getAlias($this->ns_uri) != $this->ns_alias) {
64         return null;
65       }
66     }
67
68     $message->updateArgs($this->ns_uri, $this->getExtensionArgs());
69     return $message;
70   }
71   
72   /**
73    * Extract the team/group namespace URI from the given OpenID message.
74    * Handles OpenID 1 and 2.
75    *
76    * $message: The OpenID message from which to parse team/group data.
77    * This may be a request or response message.
78    *
79    * Returns the sreg namespace URI for the supplied message.
80    *
81    * @access private
82    */
83   function _getExtensionNS(&$message) {
84     $alias = null;
85     $found_ns_uri = null;
86
87     // See if there exists an alias for the namespace
88     $alias = $message->namespaces->getAlias($this->ns_uri);
89     
90     if ($alias !== null) {
91       $found_ns_uri = $this->ns_uri;
92     }
93
94     if ($alias === null) {
95       // There is no alias for this extension, so try to add one.
96       $found_ns_uri = Auth_OpenID_TYPE_1_0;
97       
98       if ($message->namespaces->addAlias($this->ns_uri, $this->ns_alias) === null) {
99         // An alias for the string 'lp' already exists, but
100         // it's defined for something other than team/group membership
101         return null;
102       }
103     }
104     
105     return $found_ns_uri;
106   }
107 }
108
109 /**
110  * The team/group extension request class
111  */
112 class Auth_OpenID_TeamsRequest extends Auth_OpenID_TeamsExtension {
113   function __init($teams) {
114     if (!is_array($teams)) {
115       if (!empty($teams)) {
116         $teams = explode(',', $teams);
117       } else {
118         $teams = Array();
119       }
120     }
121     
122     $this->_teams = $teams;
123   }
124   
125   function Auth_OpenID_TeamsRequest($teams) {
126     $this->__init($teams);
127   }
128 }
129
130 /**
131  * The team/group extension response class
132  */
133 class Auth_OpenID_TeamsResponse extends Auth_OpenID_TeamsExtension {
134   var $_teams = array();
135   
136   function __init(&$resp, $signed_only=true) {
137     $this->ns_uri = $this->_getExtensionNS($resp->message);
138     
139     if ($signed_only) {
140       $args = $resp->getSignedNS($this->ns_uri);
141     } else {
142       $args = $resp->message->getArgs($this->ns_uri);
143     }
144     
145     if ($args === null) {
146       return null;
147     }
148     
149     // An OpenID 2.0 response will handle the namespaces
150     if (in_array($this->response_field, array_keys($args)) && !empty($args[$this->response_field])) {
151       $this->_teams = explode(',', $args[$this->response_field]);
152     }
153     
154     // Piggybacking on a 1.x request, however, won't so the field name will
155     // be different
156     elseif (in_array($this->ns_alias.'.'.$this->response_field, array_keys($args)) && !empty($args[$this->ns_alias.'.'.$this->response_field])) {
157       $this->_teams = explode(',', $args[$this->ns_alias.'.'.$this->response_field]);
158     }
159   }
160   
161   function Auth_OpenID_TeamsResponse(&$resp, $signed_only=true) {
162     $this->__init($resp, $signed_only);
163   }
164   
165   /**
166    * Get the array of teams the user is a member of
167    *
168    * @return array
169    */
170   function getTeams() {
171     return $this->_teams;
172   }
173 }
174
175 ?>