Skip to content

Commit

Permalink
Added Auth0 as an Oauth2 IdP (Oauth2)
Browse files Browse the repository at this point in the history
  • Loading branch information
glena committed Feb 1, 2016
1 parent 838277f commit 69d9f82
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 1 deletion.
46 changes: 46 additions & 0 deletions classes/Auth0Adapter.php
@@ -0,0 +1,46 @@
<?php

namespace OAuth\Plugin;

use OAuth\OAuth2\Service\Auth0;

class Auth0Adapter extends AbstractAdapter {

/**
* Retrieve the user's data
*
* The array needs to contain at least 'user', 'email', 'name' and optional 'grps'
*
* @return array
*/
public function getUser() {
$JSON = new \JSON(JSON_LOOSE_TYPE);
$data = array();

$response = $this->oAuth->request('/userinfo');
$result = $JSON->decode($response);

if( !empty($result['username']) )
{
$data['user'] = $result['username'];
}
else
{
$data['user'] = isset($result['name']) ? $result['name'] : $result['email'];
}
$data['name'] = isset($result['name']) ? $result['name'] : $result['email'];
$data['mail'] = $result['email'];

return $data;
}

/**
* Access to user and his email addresses
*
* @return array
*/
public function getScope() {
return array(Auth0::SCOPE_OPENID);
}

}
3 changes: 3 additions & 0 deletions conf/default.php
Expand Up @@ -5,6 +5,9 @@
* @author Andreas Gohr <andi@splitbrain.org>
*/

$conf['auth0-key'] = '';
$conf['auth0-secret'] = '';
$conf['auth0-domain'] = '';
$conf['custom-redirectURI'] = '';
$conf['facebook-key'] = '';
$conf['facebook-secret'] = '';
Expand Down
4 changes: 4 additions & 0 deletions conf/metadata.php
Expand Up @@ -26,6 +26,9 @@ public function html(&$plugin, $echo = false) {
}

$meta['info'] = array('plugin_oauth');
$meta['auth0-key'] = array('string');
$meta['auth0-secret'] = array('string');
$meta['auth0-domain'] = array('string');
$meta['custom-redirectURI'] = array('string','_caution' => 'warning');
$meta['facebook-key'] = array('string');
$meta['facebook-secret'] = array('string');
Expand All @@ -43,6 +46,7 @@ public function html(&$plugin, $echo = false) {
$meta['singleService'] = array('multichoice',
'_choices' => array(
'',
'Auth0',
'Google',
'Facebook',
'Github',
Expand Down
2 changes: 1 addition & 1 deletion helper.php
Expand Up @@ -20,7 +20,7 @@ class helper_plugin_oauth extends DokuWiki_Plugin {
public function loadService(&$servicename) {
$id = getID(); // $ID isn't set in trustExternal, yet

$servicename = preg_replace('/[^a-zA-Z_]+/', '', $servicename);
$servicename = preg_replace('/[^a-zA-Z0-9_]+/', '', $servicename);
if(!$servicename) return null;

require_once(__DIR__.'/phpoauthlib/src/OAuth/bootstrap.php');
Expand Down
3 changes: 3 additions & 0 deletions lang/en/settings.php
Expand Up @@ -8,6 +8,9 @@

$lang['info'] = 'Redirect URI to use when configuring the applications';
$lang['custom-redirectURI'] = 'Use the following custom redirect URI';
$lang['auth0-key'] = 'The Client ID of your registered <a href="https://manage.auth0.com/#/applications">Auth0 application</a>';
$lang['auth0-secret'] = 'The Client Secret of your registered <a href="https://manage.auth0.com/#/applications">Auth0 application</a>';
$lang['auth0-domain'] = 'The Domain of your registered <a href="https://manage.auth0.com/#/applications">Auth0 account</a>';
$lang['facebook-key'] = 'The App ID of your registered <a href="https://developers.facebook.com/apps">Facebook application</a>';
$lang['facebook-secret'] = 'The App Secret of your registered <a href="https://developers.facebook.com/apps">Facebook application</a>';
$lang['github-key'] = 'The Client ID of your registered <a href="https://github.com/settings/applications">Github application</a>';
Expand Down
103 changes: 103 additions & 0 deletions phpoauthlib/src/OAuth/OAuth2/Service/Auth0.php
@@ -0,0 +1,103 @@
<?php

namespace OAuth\OAuth2\Service;

use OAuth\Common\Exception\Exception;
use OAuth\OAuth2\Token\StdOAuth2Token;
use OAuth\Common\Http\Exception\TokenResponseException;
use OAuth\Common\Http\Uri\Uri;
use OAuth\Common\Consumer\CredentialsInterface;
use OAuth\Common\Http\Client\ClientInterface;
use OAuth\Common\Storage\TokenStorageInterface;
use OAuth\Common\Http\Uri\UriInterface;

class Auth0 extends AbstractService
{

const SCOPE_OPENID = 'openid';
protected $domain;

public function __construct(
CredentialsInterface $credentials,
ClientInterface $httpClient,
TokenStorageInterface $storage,
$scopes = array(),
UriInterface $baseApiUri = null
) {
parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);

$hlp = plugin_load('helper', 'oauth');
$this->domain = $hlp->getConf('auth0-domain');

if (null === $baseApiUri) {
$this->baseApiUri = new Uri("https://{$this->domain}/");
}
}

protected function getAuthorizationMethod()
{
return static::AUTHORIZATION_METHOD_HEADER_BEARER;
}

/**
* {@inheritdoc}
*/
public function getAuthorizationEndpoint()
{
return new Uri("https://{$this->domain}/authorize/");
}

/**
* {@inheritdoc}
*/
public function getAccessTokenEndpoint()
{
return new Uri("https://{$this->domain}/oauth/token/");
}

/**
* {@inheritdoc}
*/
protected function parseAccessTokenResponse($responseBody)
{
$JSON = new \JSON(JSON_LOOSE_TYPE);
$data = $JSON->decode($responseBody);

if (null === $data || !is_array($data)) {
throw new TokenResponseException('Unable to parse response.');
} elseif (isset($data['error'])) {
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
}

$token = new StdOAuth2Token();
$token->setAccessToken($data['access_token']);

if (isset($data['expires'])) {
$token->setLifeTime($data['expires']);
}

if (isset($data['refresh_token'])) {
$token->setRefreshToken($data['refresh_token']);
unset($data['refresh_token']);
}

unset($data['access_token']);
unset($data['expires']);

$token->setExtraParams($data);

return $token;
}

public function getDialogUri($dialogPath, array $parameters)
{
if (!isset($parameters['redirect_uri'])) {
throw new Exception("Redirect uri is mandatory for this request");
}

$parameters['client_id'] = $this->credentials->getConsumerId();
$baseUrl = "https://{$this->domain}/authorize/";
$query = http_build_query($parameters);
return new Uri($baseUrl . '?' . $query);
}
}
8 changes: 8 additions & 0 deletions style.less
Expand Up @@ -27,6 +27,14 @@
padding-left: (20px+24px);
}

a.plugin_oauth_Auth0 {
.plugin_oauth_button(#d0d2d3);
background-image: url(https://cdn.auth0.com/styleguide/1.0.0/img/badge.png);
padding-left: (20px+24px);
background-size: 22px 24px;
color:#5c666f;
}

a.plugin_oauth_Google {
.plugin_oauth_button(#DC4A38);
background-image: url(images/google.png);
Expand Down

0 comments on commit 69d9f82

Please sign in to comment.