Commit 656c5601 authored by Pål Karlsrud's avatar Pål Karlsrud

Add the initial framework for the Dataporten OAuth2 provider.

parent c878ccf4
......@@ -279,7 +279,7 @@ class ClassLoader
*/
public function setApcuPrefix($apcuPrefix)
{
$this->apcuPrefix = function_exists('apcu_fetch') && ini_get('apc.enabled') ? $apcuPrefix : null;
$this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null;
}
/**
......@@ -377,7 +377,7 @@ class ClassLoader
$subPath = $class;
while (false !== $lastPos = strrpos($subPath, '\\')) {
$subPath = substr($subPath, 0, $lastPos);
$search = $subPath.'\\';
$search = $subPath . '\\';
if (isset($this->prefixDirsPsr4[$search])) {
$pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
foreach ($this->prefixDirsPsr4[$search] as $dir) {
......
......@@ -42,6 +42,7 @@ return array(
'Hybridauth\\Provider\\AOLOpenID' => $vendorDir . '/hybridauth/hybridauth/src/Provider/AOLOpenID.php',
'Hybridauth\\Provider\\Authentiq' => $vendorDir . '/hybridauth/hybridauth/src/Provider/Authentiq.php',
'Hybridauth\\Provider\\BitBucket' => $vendorDir . '/hybridauth/hybridauth/src/Provider/BitBucket.php',
'Hybridauth\\Provider\\Dataporten' => $vendorDir . '/hybridauth/hybridauth/src/Provider/Dataporten.php',
'Hybridauth\\Provider\\Discord' => $vendorDir . '/hybridauth/hybridauth/src/Provider/Discord.php',
'Hybridauth\\Provider\\Disqus' => $vendorDir . '/hybridauth/hybridauth/src/Provider/Disqus.php',
'Hybridauth\\Provider\\Dribbble' => $vendorDir . '/hybridauth/hybridauth/src/Provider/Dribbble.php',
......
......@@ -57,6 +57,7 @@ class ComposerStaticInit2fd16dd8fbb94748e1ada016e6921a0f
'Hybridauth\\Provider\\AOLOpenID' => __DIR__ . '/..' . '/hybridauth/hybridauth/src/Provider/AOLOpenID.php',
'Hybridauth\\Provider\\Authentiq' => __DIR__ . '/..' . '/hybridauth/hybridauth/src/Provider/Authentiq.php',
'Hybridauth\\Provider\\BitBucket' => __DIR__ . '/..' . '/hybridauth/hybridauth/src/Provider/BitBucket.php',
'Hybridauth\\Provider\\Dataporten' => __DIR__ . '/..' . '/hybridauth/hybridauth/src/Provider/Dataporten.php',
'Hybridauth\\Provider\\Discord' => __DIR__ . '/..' . '/hybridauth/hybridauth/src/Provider/Discord.php',
'Hybridauth\\Provider\\Disqus' => __DIR__ . '/..' . '/hybridauth/hybridauth/src/Provider/Disqus.php',
'Hybridauth\\Provider\\Dribbble' => __DIR__ . '/..' . '/hybridauth/hybridauth/src/Provider/Dribbble.php',
......
<?php
/*!
* Hybridauth
* https://hybridauth.github.io | https://github.com/hybridauth/hybridauth
* (c) 2017 Hybridauth authors | https://hybridauth.github.io/license.html
*/
namespace Hybridauth\Provider;
use Hybridauth\Exception\InvalidArgumentException;
use Hybridauth\Exception\UnexpectedApiResponseException;
use Hybridauth\Adapter\OAuth2;
use Hybridauth\Data;
use Hybridauth\User;
/**
* Dataporten OAuth2 provider adapter.
*
* Example:
*
* $config = [
* 'callback' => Hybridauth\HttpClient\Util::getCurrentUrl(),
* 'keys' => [ 'id' => '', 'secret' => '' ],
* 'scope' => 'email, profile'
* ];
*
* $adapter = new Hybridauth\Provider\Dataporten( $config );
*
* try {
* $adapter->authenticate();
*
* $userProfile = $adapter->getUserProfile();
* $tokens = $adapter->getAccessToken();
* $response = $adapter->setUserStatus("Hybridauth test message..");
* }
* catch( Exception $e ){
* echo $e->getMessage() ;
* }
*/
class Dataporten extends OAuth2
{
/**
* {@inheritdoc}
*/
protected $scope = 'email, profile, userid, openid';
/**
* {@inheritdoc}
*/
protected $apiBaseUrl = 'https://auth.dataporten.no/';
/**
* {@inheritdoc}
*/
protected $authorizeUrl = 'https://auth.dataporten.no/oauth/authorization';
/**
* {@inheritdoc}
*/
protected $accessTokenUrl = 'https://auth.dataporten.no/oauth/token';
/**
* {@inheritdoc}
*/
protected $apiDocumentation = 'https://docs.feide.no/developer_oauth/technical_details/';
/**
* {@inheritdoc}
*/
public function getUserProfile()
{
$response = $this->apiRequest('https://auth.dataporten.no/userinfo');
$data = new Data\Collection($response);
if (! $data->exists('user')) {
throw new UnexpectedApiResponseException('Provider API returned an unexpected response.');
}
$user = new Data\Collection($data->get('user'));
$userProfile = new User\Profile();
$userProfile->identifier = $user->get('userid');
$userProfile->displayName = $user->get('name');
$userProfile->email = $user->get('email');
return $userProfile;
}
}
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 36 36" xml:space="preserve"><ellipse fill="#ccc" cx="19.6" cy="17.1" rx="1.9" ry="2"/><ellipse fill="#ccc" cx="12.8" cy="17.1" rx="1.9" ry="2"/><path fill="#ccc" d="M28.5,0H3.8C1.7,0,0,1.7,0,3.7v24.3c0,2.1,1.7,3.7,3.8,3.7h21l-1-3.3l2.4,2.1l2.2,2l4,3.4v-7.9v-1.8V3.7 C32.3,1.7,30.6,0,28.5,0z M21.4,23.5c0,0-0.7-0.8-1.2-1.5c2.4-0.7,3.3-2.1,3.3-2.1c-0.8,0.5-1.5,0.8-2.1,1.1 c-0.9,0.4-1.8,0.6-2.7,0.8c-1.8,0.3-3.4,0.2-4.8,0c-1.1-0.2-2-0.5-2.7-0.8c-0.4-0.2-0.9-0.4-1.4-0.6c-0.1,0-0.1-0.1-0.2-0.1 c0,0-0.1,0-0.1,0c-0.3-0.2-0.5-0.3-0.5-0.3s0.9,1.4,3.2,2.1c-0.6,0.7-1.2,1.5-1.2,1.5c-4.1-0.1-5.6-2.7-5.6-2.7 c0-5.8,2.7-10.5,2.7-10.5c2.7-2,5.2-1.9,5.2-1.9l0.2,0.2C10.2,9.5,8.7,11,8.7,11s0.4-0.2,1.1-0.5c2-0.8,3.5-1.1,4.2-1.1 c0.1,0,0.2,0,0.3,0c1.1-0.1,2.4-0.2,3.7,0c1.8,0.2,3.6,0.7,5.6,1.7c0,0-1.5-1.4-4.6-2.3l0.3-0.3c0,0,2.5-0.1,5.2,1.9 c0,0,2.7,4.7,2.7,10.5C27.1,20.8,25.5,23.4,21.4,23.5z"/></svg>
<?php
namespace OCA\SocialLogin\Provider;
use Hybridauth\Adapter\OAuth2;
use Hybridauth\Data;
use Hybridauth\Exception\UnexpectedApiResponseException;
use Hybridauth\User;
class DataportenOAuth2 extends OAuth2
{
/**
* @return User\Profile
* @throws UnexpectedApiResponseException
* @throws \Hybridauth\Exception\HttpClientFailureException
* @throws \Hybridauth\Exception\HttpRequestFailedException
* @throws \Hybridauth\Exception\InvalidAccessTokenException
*/
public function getUserProfile()
{
$profileFields = array_filter(
array_map('trim', explode(',', $this->config->get('profile_fields'))),
function ($val) { return !empty($val); }
);
$profileUrl = $this->config->get('endpoints')->get('profile_url');
if (count($profileFields) > 0) {
$profileUrl .= (strpos($profileUrl, '?') !== false ? '&' : '?') . 'fields=' . implode(',', $profileFields);
}
$response = $this->apiRequest($profileUrl);
if (!isset($response->identifier) && isset($response->id)) {
$response->identifier = $response->id;
}
if (!isset($response->identifier) && isset($response->data->id)) {
$response->identifier = $response->data->id;
}
$data = new Data\Collection($response);
if (! $data->exists('identifier')) {
throw new UnexpectedApiResponseException('Provider API returned an unexpected response.');
}
$userProfile = new User\Profile();
foreach ($data->toArray() as $key => $value) {
if (property_exists($userProfile, $key)) {
$userProfile->$key = $value;
}
}
if (!empty($userProfile->email)) {
$userProfile->emailVerified = $userProfile->email;
}
return $userProfile;
}
}
......@@ -48,6 +48,7 @@ class AdminSettings implements ISettings
'google',
'facebook',
'twitter',
'dataporten',
'GitHub',
'discord',
];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment