Skip to content

Commit

Permalink
Merge pull request #36 from kasperrt/master
Browse files Browse the repository at this point in the history
Added dataporten as service
  • Loading branch information
splitbrain committed Jun 28, 2016
2 parents 518d0e6 + 768eb04 commit c2e7b37
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
30 changes: 30 additions & 0 deletions classes/DataportenAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace OAuth\Plugin;

use OAuth\OAuth2\Service\Dataporten;

class DataportenAdapter 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();

$result = $JSON->decode($this->oAuth->request('https://auth.dataporten.no/userinfo'));
$result_grous = $JSON->decode($this->oAuth->request('https://groups-api.dataporten.no/groups/me/groups'));

$data['user'] = $result['user']['userid'];
$data['name'] = $result['user']['name'];
$data['mail'] = $result['user']['email'];

return $data;
}

}
2 changes: 2 additions & 0 deletions conf/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
$conf['github-secret'] = '';
$conf['google-key'] = '';
$conf['google-secret'] = '';
$conf['dataporten-key'] = '';
$conf['dataporten-secret'] = '';
$conf['keycloak-key'] = '';
$conf['keycloak-secret'] = '';
$conf['keycloak-authurl'] = 'https://keycloak.example.com/auth/realms/{realm}/protocol/openid-connect/auth';
Expand Down
3 changes: 3 additions & 0 deletions conf/metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public function html(&$plugin, $echo = false) {
$meta['github-secret'] = array('string');
$meta['google-key'] = array('string');
$meta['google-secret'] = array('string');
$meta['dataporten-key'] = array('string');
$meta['dataporten-secret'] = array('string');
$meta['keycloak-key'] = array('string');
$meta['keycloak-secret'] = array('string');
$meta['keycloak-authurl'] = array('string');
Expand All @@ -53,6 +55,7 @@ public function html(&$plugin, $echo = false) {
'',
'Auth0',
'Google',
'Dataporten',
'Facebook',
'Github',
'Yahoo',
Expand Down
2 changes: 2 additions & 0 deletions lang/en/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
$lang['github-secret'] = 'The Client Secret of your registered <a href="https://github.com/settings/applications">Github application</a>';
$lang['google-key'] = 'The Client ID of your registered <a href="https://console.developers.google.com/project">Google Project</a> (see Credentials Screen)';
$lang['google-secret'] = 'The Client Secret of your registered <a href="https://console.developers.google.com/project">Google Project</a> (see Credentials Screen)';
$lang['dataporten-key'] = 'The Client ID of your registered <a href="https://dashboard.dataporten.no">Dataporten application</a>';
$lang['dataporten-secret'] = 'The Client Secret of your registered <a href="https://dashboard.dataporten.no">Dataporten application</a>';
$lang['keycloak-key'] = 'The resource id of your Keycloak application.';
$lang['keycloak-secret'] = 'The Secret of your Keycloak Application.';
$lang['keycloak-authurl'] = 'The authorization endpoint URL of your Keycloak setup.';
Expand Down
63 changes: 63 additions & 0 deletions phpoauthlib/src/OAuth/OAuth2/Service/Dataporten.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace OAuth\OAuth2\Service;

use OAuth\OAuth2\Token\StdOAuth2Token;
use OAuth\Common\Http\Exception\TokenResponseException;
use OAuth\OAuth2\Service\Exception\InvalidAccessTypeException;
use OAuth\Common\Http\Uri\Uri;

class Dataporten extends AbstractService
{

/**
* {@inheritdoc}
*/
public function getAuthorizationEndpoint()
{
return new Uri('https://auth.dataporten.no/oauth/authorization');
}

/**
* {@inheritdoc}
*/
public function getAccessTokenEndpoint()
{
return new Uri('https://auth.dataporten.no/oauth/token');
}

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

/**
* {@inheritdoc}
*/
protected function parseAccessTokenResponse($responseBody)
{
$data = json_decode($responseBody, true);

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']);
$token->setLifetime($data['expires_in']);

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

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

$token->setExtraParams($data);

return $token;
}
}

0 comments on commit c2e7b37

Please sign in to comment.