3 * Wordpress Teams plugin
4 * Copyright (C) 2009-2010 Canonical Ltd.
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.
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.
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/>.
21 * Provides an example OpenID extension to query user team/group membership
23 * This code is based on code supplied with the openid library for simple
28 * Require the Message implementation.
30 require_once 'Auth/OpenID/Message.php';
31 require_once 'Auth/OpenID/Extension.php';
34 * The team/group extension base class
36 class Auth_OpenID_TeamsExtension extends Auth_OpenID_Extension {
37 var $ns_uri = 'http://ns.launchpad.net/2007/openid-teams';
39 var $request_field = 'query_membership';
40 var $response_field = 'is_member';
43 * Get the string arguments that should be added to an OpenID
44 * message for this extension.
46 function getExtensionArgs() {
50 $args[$this->request_field] = implode(',', $this->_teams);
57 * Add the arguments from this extension to the provided message.
59 * Returns the message with the extension arguments added.
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) {
68 $message->updateArgs($this->ns_uri, $this->getExtensionArgs());
73 * Extract the team/group namespace URI from the given OpenID message.
74 * Handles OpenID 1 and 2.
76 * $message: The OpenID message from which to parse team/group data.
77 * This may be a request or response message.
79 * Returns the sreg namespace URI for the supplied message.
83 function _getExtensionNS(&$message) {
87 // See if there exists an alias for the namespace
88 $alias = $message->namespaces->getAlias($this->ns_uri);
90 if ($alias !== null) {
91 $found_ns_uri = $this->ns_uri;
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;
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
105 return $found_ns_uri;
110 * The team/group extension request class
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);
122 $this->_teams = $teams;
125 function Auth_OpenID_TeamsRequest($teams) {
126 $this->__init($teams);
131 * The team/group extension response class
133 class Auth_OpenID_TeamsResponse extends Auth_OpenID_TeamsExtension {
134 var $_teams = array();
136 function __init(&$resp, $signed_only=true) {
137 $this->ns_uri = $this->_getExtensionNS($resp->message);
140 $args = $resp->getSignedNS($this->ns_uri);
142 $args = $resp->message->getArgs($this->ns_uri);
145 if ($args === null) {
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]);
154 // Piggybacking on a 1.x request, however, won't so the field name will
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]);
161 function Auth_OpenID_TeamsResponse(&$resp, $signed_only=true) {
162 $this->__init($resp, $signed_only);
166 * Get the array of teams the user is a member of
170 function getTeams() {
171 return $this->_teams;