Skip to content

Instantly share code, notes, and snippets.

@CHH
Created September 28, 2011 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CHH/1247738 to your computer and use it in GitHub Desktop.
Save CHH/1247738 to your computer and use it in GitHub Desktop.
PHP BrowserID verification function
<?php
/**
* Verifies the given BrowserID assertion
*
* @throws \UnexpectedValueException on error
*
* @param string $assertion Assertion provided by navigator.id.getVerifiedEmail()
* @param string $audience Host Name, defaults to $_SERVER['HTTP_HOST']
* @param string $verifyService URL of the verification webservice, defaults BrowserID's
* @return object The decoded JSON returned by the verify service
*/
function browserid_verify_assertion($assertion, $audience = null, $verifyService = 'https://browserid.org/verify')
{
if (null === $audience) {
$audience = $_SERVER['HTTP_HOST'];
}
$query = http_build_query(array(
'assertion' => $assertion,
'audience' => $audience
));
$opts = array(
'http' => array(
'content' => $query,
'header' => 'Content-Type: application/x-www-form-urlencoded',
'method' => 'POST'
)
);
$ctx = stream_context_create($opts);
$contents = file_get_contents($verifyService, false, $ctx);
if (!$contents) {
throw new \UnexpectedValueException(sprintf(
"Verify Service %s did not return a response", $verifyService
));
}
$data = json_decode($contents);
if (isset($data->status) and $data->status == 'failure') {
throw new \UnexpectedValueException(sprintf(
'The Verify Service returned an error: %s', $data->reason
));
}
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment