summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Auth/OpenID/Message.php77
-rw-r--r--Tests/Auth/OpenID/Consumer.php3
-rw-r--r--Tests/Auth/OpenID/Message.php61
-rw-r--r--Tests/Auth/OpenID/Server.php10
4 files changed, 106 insertions, 45 deletions
diff --git a/Auth/OpenID/Message.php b/Auth/OpenID/Message.php
index 8a02ab2..74f63b0 100644
--- a/Auth/OpenID/Message.php
+++ b/Auth/OpenID/Message.php
@@ -270,6 +270,7 @@ class Auth_OpenID_NamespaceMap {
{
$this->alias_to_namespace = new Auth_OpenID_Mapping();
$this->namespace_to_alias = new Auth_OpenID_Mapping();
+ $this->implicit_namespaces = array();
}
function getAlias($namespace_uri)
@@ -299,7 +300,12 @@ class Auth_OpenID_NamespaceMap {
return $this->namespace_to_alias->items();
}
- function addAlias($namespace_uri, $desired_alias)
+ function isImplicit($namespace_uri)
+ {
+ return in_array($namespace_uri, $this->implicit_namespaces);
+ }
+
+ function addAlias($namespace_uri, $desired_alias, $implicit=false)
{
// Add an alias from this namespace URI to the desired alias
global $Auth_OpenID_OPENID_PROTOCOL_FIELDS;
@@ -345,6 +351,9 @@ class Auth_OpenID_NamespaceMap {
$this->alias_to_namespace->set($desired_alias, $namespace_uri);
$this->namespace_to_alias->set($namespace_uri, $desired_alias);
+ if ($implicit) {
+ array_push($this->implicit_namespaces, $namespace_uri);
+ }
return $desired_alias;
}
@@ -517,10 +526,6 @@ class Auth_OpenID_Message {
$openid_ns_uri =
$this->namespaces->getNamespaceURI(Auth_OpenID_NULL_NAMESPACE);
- if ($openid_ns_uri === null) {
- $openid_ns_uri = Auth_OpenID_OPENID1_NS;
- }
-
$this->setOpenIDNamespace($openid_ns_uri);
// Actually put the pairs into the appropriate namespaces
@@ -528,23 +533,12 @@ class Auth_OpenID_Message {
list($ns_alias, $ns_key, $value) = $triple;
$ns_uri = $this->namespaces->getNamespaceURI($ns_alias);
if ($ns_uri === null) {
- // Only try to map an alias to a default if it's an
- // OpenID 1.x message.
- if ($openid_ns_uri == Auth_OpenID_OPENID1_NS) {
- foreach ($Auth_OpenID_registered_aliases
- as $alias => $uri) {
- if ($alias == $ns_alias) {
- $ns_uri = $uri;
- break;
- }
- }
- }
-
+ $ns_uri = $this->_getDefaultNamespace($ns_alias);
if ($ns_uri === null) {
- $ns_uri = $openid_ns_uri;
+ $ns_uri = Auth_OpenID_OPENID_NS;
$ns_key = sprintf('%s.%s', $ns_alias, $ns_key);
} else {
- $this->namespaces->addAlias($ns_uri, $ns_alias);
+ $this->namespaces->addAlias($ns_uri, $ns_alias, true);
}
}
@@ -554,15 +548,32 @@ class Auth_OpenID_Message {
return true;
}
- function setOpenIDNamespace($openid_ns_uri)
+ function _getDefaultNamespace($mystery_alias)
+ {
+ global $Auth_OpenID_registered_aliases;
+ if ($this->isOpenID1()) {
+ return @$Auth_OpenID_registered_aliases[$mystery_alias];
+ }
+ return null;
+ }
+
+ function setOpenIDNamespace($openid_ns_uri=null)
{
+ if ($openid_ns_uri === null) {
+ $openid_ns_uri = Auth_OpenID_OPENID1_NS;
+ $implicit = true;
+ } else {
+ $implicit = false;
+ }
+
if (!in_array($openid_ns_uri, $this->allowed_openid_namespaces)) {
// raise ValueError('Invalid null namespace: %r' % (openid_ns_uri,))
return false;
}
$this->namespaces->addAlias($openid_ns_uri,
- Auth_OpenID_NULL_NAMESPACE);
+ Auth_OpenID_NULL_NAMESPACE,
+ $implicit);
$this->_openid_ns_uri = $openid_ns_uri;
}
@@ -593,25 +604,15 @@ class Auth_OpenID_Message {
// Add namespace definitions to the output
foreach ($this->namespaces->iteritems() as $pair) {
list($ns_uri, $alias) = $pair;
-
+ if ($this->namespaces->isImplicit($ns_uri)) {
+ continue;
+ }
if ($alias == Auth_OpenID_NULL_NAMESPACE) {
- if ($ns_uri != Auth_OpenID_OPENID1_NS) {
- $args['openid.ns'] = $ns_uri;
- } else {
- // drop the default null namespace
- // definition. This potentially changes a message
- // since we have no way of knowing whether it was
- // explicitly specified at the time the message
- // was parsed. The vast majority of the time, this
- // will be the right thing to do. Possibly this
- // could look in the signed list.
- }
+ $ns_key = 'openid.ns';
} else {
- if ($this->getOpenIDNamespace() != Auth_OpenID_OPENID1_NS) {
- $ns_key = 'openid.ns.' . $alias;
- $args[$ns_key] = $ns_uri;
- }
+ $ns_key = 'openid.ns.' . $alias;
}
+ $args[$ns_key] = $ns_uri;
}
foreach ($this->args->items() as $pair) {
@@ -892,4 +893,4 @@ class Auth_OpenID_Message {
}
}
-?> \ No newline at end of file
+?>
diff --git a/Tests/Auth/OpenID/Consumer.php b/Tests/Auth/OpenID/Consumer.php
index 0605afc..7af9fb7 100644
--- a/Tests/Auth/OpenID/Consumer.php
+++ b/Tests/Auth/OpenID/Consumer.php
@@ -227,7 +227,8 @@ class Tests_Auth_OpenID_Consumer extends PHPUnit_TestCase {
$expected = array(
'openid.mode' => $mode,
'openid.identity' => $delegate_url,
- 'openid.trust_root' => $trust_root
+ 'openid.trust_root' => $trust_root,
+ 'openid.ns' => Auth_OpenID_OPENID1_NS
);
if ($consumer->_use_assocs) {
diff --git a/Tests/Auth/OpenID/Message.php b/Tests/Auth/OpenID/Message.php
index 633955c..325bce9 100644
--- a/Tests/Auth/OpenID/Message.php
+++ b/Tests/Auth/OpenID/Message.php
@@ -324,6 +324,7 @@ class Tests_Auth_OpenID_OpenID1Message extends MessageTest {
{
$this->assertEquals($this->msg->getOpenIDNamespace(),
Auth_OpenID_OPENID1_NS);
+ $this->assertTrue($this->msg->namespaces->isImplicit(Auth_OpenID_OPENID1_NS));
}
function test_getKeyOpenID()
@@ -545,13 +546,69 @@ class Tests_Auth_OpenID_OpenID1Message extends MessageTest {
}
}
-class Tests_Auth_OpenID_OpenID1ExplicitMessage extends Tests_Auth_OpenID_OpenID1Message {
+class Tests_Auth_OpenID_OpenID1ExplicitMessage extends MessageTest {
function setUp()
{
$this->msg = Auth_OpenID_Message::fromPostArgs(array('openid.mode' => 'error',
'openid.error' => 'unit test',
'openid.ns' => Auth_OpenID_OPENID1_NS));
}
+
+ function test_isOpenID1()
+ {
+ $this->assertTrue($this->msg->isOpenID1());
+ $this->assertFalse($this->msg->namespaces->isImplicit(Auth_OpenID_OPENID1_NS));
+ }
+
+ function test_isOpenID2()
+ {
+ $this->assertFalse($this->msg->isOpenID2());
+ }
+
+ function test_toPostArgs()
+ {
+ $this->assertEquals($this->msg->toPostArgs(),
+ array('openid.mode' => 'error',
+ 'openid.error' => 'unit test',
+ 'openid.ns' => Auth_OpenID_OPENID1_NS));
+ }
+
+ function test_toArgs()
+ {
+ $this->assertEquals($this->msg->toArgs(),
+ array('mode' => 'error',
+ 'error' => 'unit test',
+ 'ns' => Auth_OpenID_OPENID1_NS));
+ }
+
+ function test_toKVForm()
+ {
+ $this->assertEquals($this->msg->toKVForm(),
+ "error:unit test\nmode:error\nns:".
+ Auth_OpenID_OPENID1_NS."\n");
+ }
+
+ function test_toURLEncoded()
+ {
+ $this->assertEquals($this->msg->toURLEncoded(),
+ 'openid.error=unit+test&openid.mode=error&openid.ns=http%3A%2F%2Fopenid.net%2Fsignon%2F1.0');
+ }
+
+ function test_toURL()
+ {
+ $base_url = 'http://base.url/';
+ $actual = $this->msg->toURL($base_url);
+ $actual_base = substr($actual, 0, strlen($base_url));
+ $this->assertEquals($actual_base, $base_url);
+ $this->assertEquals($actual[strlen($base_url)], '?');
+ $query = substr($actual, strlen($base_url) + 1);
+
+ $parsed = Auth_OpenID::parse_str($query);
+
+ $this->assertEquals($parsed, array('openid.mode' => 'error',
+ 'openid.error' => 'unit test',
+ 'openid.ns' => Auth_OpenID_OPENID1_NS));
+ }
}
class Tests_Auth_OpenID_OpenID2Message extends MessageTest {
@@ -1088,4 +1145,4 @@ $Tests_Auth_OpenID_Message_other = array(
new Tests_Auth_OpenID_GeneralMessageTest()
);
-?> \ No newline at end of file
+?>
diff --git a/Tests/Auth/OpenID/Server.php b/Tests/Auth/OpenID/Server.php
index a8af901..31ca542 100644
--- a/Tests/Auth/OpenID/Server.php
+++ b/Tests/Auth/OpenID/Server.php
@@ -75,7 +75,8 @@ class Tests_Auth_OpenID_Test_ServerError extends PHPUnit_TestCase {
$this->assertTrue($e->hasReturnTo());
$expected_args = array(
'openid.mode' => 'error',
- 'openid.error' => 'plucky');
+ 'openid.error' => 'plucky',
+ 'openid.ns' => Auth_OpenID_OPENID1_NS);
$encoded = $e->encodeToURL();
if (Auth_OpenID_isError($encoded)) {
@@ -154,7 +155,8 @@ class Tests_Auth_OpenID_Test_ServerError extends PHPUnit_TestCase {
$e = new Auth_OpenID_ServerError($args, "plucky");
$this->assertTrue($e->hasReturnTo());
$expected_args = array('openid.mode' => 'error',
- 'openid.error' => 'plucky');
+ 'openid.error' => 'plucky',
+ 'openid.ns' => Auth_OpenID_OPENID1_NS);
$this->assertTrue($e->whichEncoding() == Auth_OpenID_ENCODE_URL);
@@ -1249,7 +1251,7 @@ class Tests_Auth_OpenID_CheckID extends PHPUnit_TestCase {
$answer = $this->request->answer(false, $server_url);
$this->assertEquals($answer->request, $this->request);
- $this->assertEquals(count($answer->fields->toPostArgs()), 2);
+ $this->assertEquals(count($answer->fields->toPostArgs()), 3);
$this->assertEquals($answer->fields->getOpenIDNamespace(),
Auth_OpenID_OPENID1_NS);
$this->assertEquals($answer->fields->getArg(Auth_OpenID_OPENID_NS, 'mode'),
@@ -2238,4 +2240,4 @@ class Tests_Auth_OpenID_Server extends PHPUnit_TestSuite {
}
}
-?> \ No newline at end of file
+?>