teams-extension.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. * Provides an example OpenID extension to query user team/group membership
  21. *
  22. * This code is based on code supplied with the openid library for simple
  23. * registration data.
  24. */
  25. /**
  26. * Require the Message implementation.
  27. */
  28. require_once 'Auth/OpenID/Message.php';
  29. require_once 'Auth/OpenID/Extension.php';
  30. /**
  31. * The team/group extension base class
  32. */
  33. class Auth_OpenID_TeamsExtension extends Auth_OpenID_Extension {
  34. var $ns_uri = 'http://ns.launchpad.net/2007/openid-teams';
  35. var $ns_alias = 'lp';
  36. var $request_field = 'query_membership';
  37. var $response_field = 'is_member';
  38. /**
  39. * Get the string arguments that should be added to an OpenID
  40. * message for this extension.
  41. */
  42. function getExtensionArgs() {
  43. $args = array();
  44. if ($this->_teams) {
  45. $args[$this->request_field] = implode(',', $this->_teams);
  46. }
  47. return $args;
  48. }
  49. /**
  50. * Add the arguments from this extension to the provided message.
  51. *
  52. * Returns the message with the extension arguments added.
  53. */
  54. function toMessage(&$message) {
  55. if ($message->namespaces->addAlias($this->ns_uri, $this->ns_alias) === null) {
  56. if ($message->namespaces->getAlias($this->ns_uri) != $this->ns_alias) {
  57. return null;
  58. }
  59. }
  60. $message->updateArgs($this->ns_uri, $this->getExtensionArgs());
  61. return $message;
  62. }
  63. /**
  64. * Extract the team/group namespace URI from the given OpenID message.
  65. * Handles OpenID 1 and 2.
  66. *
  67. * $message: The OpenID message from which to parse team/group data.
  68. * This may be a request or response message.
  69. *
  70. * Returns the sreg namespace URI for the supplied message.
  71. *
  72. * @access private
  73. */
  74. function _getExtensionNS(&$message) {
  75. $alias = null;
  76. $found_ns_uri = null;
  77. // See if there exists an alias for the namespace
  78. $alias = $message->namespaces->getAlias($this->ns_uri);
  79. if ($alias !== null) {
  80. $found_ns_uri = $this->ns_uri;
  81. }
  82. if ($alias === null) {
  83. // There is no alias for this extension, so try to add one.
  84. $found_ns_uri = Auth_OpenID_TYPE_1_0;
  85. if ($message->namespaces->addAlias($this->ns_uri, $this->ns_alias) === null) {
  86. // An alias for the string 'lp' already exists, but
  87. // it's defined for something other than team/group membership
  88. return null;
  89. }
  90. }
  91. return $found_ns_uri;
  92. }
  93. }
  94. /**
  95. * The team/group extension request class
  96. */
  97. class Auth_OpenID_TeamsRequest extends Auth_OpenID_TeamsExtension {
  98. function __init($teams) {
  99. if (!is_array($teams)) {
  100. if (!empty($teams)) {
  101. $teams = explode(',', $teams);
  102. } else {
  103. $teams = Array();
  104. }
  105. }
  106. $this->_teams = $teams;
  107. }
  108. function Auth_OpenID_TeamsRequest($teams) {
  109. $this->__init($teams);
  110. }
  111. }
  112. /**
  113. * The team/group extension response class
  114. */
  115. class Auth_OpenID_TeamsResponse extends Auth_OpenID_TeamsExtension {
  116. var $_teams = array();
  117. function __init(&$resp, $signed_only=true) {
  118. $this->ns_uri = $this->_getExtensionNS($resp->message);
  119. if ($signed_only) {
  120. $args = $resp->getSignedNS($this->ns_uri);
  121. } else {
  122. $args = $resp->message->getArgs($this->ns_uri);
  123. }
  124. if ($args === null) {
  125. return null;
  126. }
  127. // An OpenID 2.0 response will handle the namespaces
  128. if (in_array($this->response_field, array_keys($args)) && !empty($args[$this->response_field])) {
  129. $this->_teams = explode(',', $args[$this->response_field]);
  130. }
  131. // Piggybacking on a 1.x request, however, won't so the field name will
  132. // be different
  133. elseif (in_array($this->ns_alias.'.'.$this->response_field, array_keys($args)) && !empty($args[$this->ns_alias.'.'.$this->response_field])) {
  134. $this->_teams = explode(',', $args[$this->ns_alias.'.'.$this->response_field]);
  135. }
  136. }
  137. function Auth_OpenID_TeamsResponse(&$resp, $signed_only=true) {
  138. $this->__init($resp, $signed_only);
  139. }
  140. /**
  141. * Get the array of teams the user is a member of
  142. *
  143. * @return array
  144. */
  145. function getTeams() {
  146. return $this->_teams;
  147. }
  148. }
  149. ?>