Ajout intégration de l'hybridAuthentification encore en état de code brut

Pas de tests

TODO: aller chercher les token et les id des authentifcation pour google, github, facebook, twitter ... Reste a tester et à décider pour les autres types d'authentification.
This commit is contained in:
Emmanuel ROY 2019-09-26 04:27:05 +02:00
commit 41f2c5820b
14 changed files with 355 additions and 1 deletions

View file

@ -3,6 +3,8 @@
namespace MVC\Classe;
define( "CONTROLLERS_PATH" , APPLICATION_PATH . DIRECTORY_SEPARATOR . "include" . DIRECTORY_SEPARATOR . "controlleurs");
define("CONFIG_PATH", APPLICATION_PATH . DIRECTORY_SEPARATOR . "config");
define("LOG_PATH", APPLICATION_PATH . DIRECTORY_SEPARATOR . "logs");
require APPLICATION_PATH . DIRECTORY_SEPARATOR . "parameters.php";

View file

@ -0,0 +1,20 @@
<?php
namespace MVC\Classe;
class Session
{
static public function isRegistered()
{
if (isset($_SESSION['userProfile'])) {
return;
} else {
header("location : " . Url::link_rewrite(false, 'error', []));
die('Ooops, something was wrong...');
}
}
}

View file

@ -85,4 +85,33 @@ class Url
$this->page = $page;
}
static public function link_rewrite($isControlPatern, $page, $params = array())
{
if ($isControlPatern) {
return self::controlLink_rewrite($page, $params);
} else {
return self::link_rewrite_slashParam($page, $params);
}
}
static private function link_rewrite_slashParam($page, $params = array())
{
$stringParams = '';
foreach ($params as $key => $values) {
$stringParams .= "/" . $key . "/" . $values;
}
return '/' . $page . $stringParams;
}
static private function controlLink_rewrite($page, $params = array())
{
$stringParams = '';
foreach ($params as $key => $values) {
$stringParams .= "/" . $key . "/" . $values;
}
return '/' . 'control' . '/' . $page . $stringParams;
}
}

View file

@ -0,0 +1,50 @@
<?php
/**
* Build a configuration array to pass to `Hybridauth\Hybridauth`
*
*/
$config = [
//Location where to redirect users once they authenticate with a provider
'callback' => \MVC\Url::link_rewrite(false, 'accueil', []),
//Providers specifics
'providers' => [
'GitHub' => [
'enabled' => true,
'keys' => ['id' => '', 'secret' => ''],
],
'Google' => [
'enabled' => true,
'keys' => ['id' => '', 'secret' => ''],
],
'Facebook' => [
'enabled' => true,
'keys' => ['id' => '', 'secret' => ''],
],
'Twitter' => [
'enabled' => true,
'keys' => ['key' => '', 'secret' => ''],
]
],
//optional : set debug mode
'debug_mode' => true,
// Path to file writeable by the web server. Required if 'debug_mode' is not false
'debug_file' => LOG_PATH . DIRECTORY_SEPARATOR . 'hybridauth.log',
/* optional : customize Curl settings
// for more information on curl, refer to: http://www.php.net/manual/fr/function.curl-setopt.php
'curl_options' => [
// setting custom certificates
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_CAINFO => '/path/to/your/certificate.crt',
// set a valid proxy ip address
CURLOPT_PROXY => '*.*.*.*:*',
// set a custom user agent
CURLOPT_USERAGENT => ''
] */
];

View file

@ -0,0 +1,9 @@
<?php
require CONFIG_PATH . DIRECTORY_SEPARATOR . "authentification-config-example.php";
$hybridauth = new Hybridauth\Hybridauth($config);
$adapters = $hybridauth->getConnectedAdapters();
$templateData['hybridauth'] = $hybridauth;
$templateData['adapters'] = $adapters;

View file

@ -0,0 +1,18 @@
<?php
\MVC\Session::isregistered();
require CONFIG_PATH . DIRECTORY_SEPARATOR . "authentification-config-example.php";
$hybridauth = new Hybridauth\Hybridauth($config);
$adapters = $hybridauth->getConnectedAdapters();
$templateData['adapters'] = $adapters;
$templateData['extractedData'] = [
'token' => $_SESSION['userToken'],
'identifier' => $_SESSION['userProfile']->identifier,
'email' => $_SESSION['userProfile']->email,
'first_name' => $_SESSION['userProfile']->firstName,
'last_name' => $_SESSION['userProfile']->lastName,
'photoURL' => strtok($_SESSION['userProfile']->photoURL, '?'),
];

View file

@ -0,0 +1,4 @@
name : authentification
page_title: Hybrid Authentification de l'application
description : La page d'authentification
params : params

View file

@ -0,0 +1,4 @@
name : compte
page_title: Compte(s) utilisateurs de l'application
description : La page d'accès privé
params : params

View file

@ -1,9 +1,24 @@
<html>
<head>
<title>{{$page_title}}</title>
<meta name="description" lang="fr" content="{{$description}}" />
<meta name="description" lang="fr" content="{{$description}}"/>
@section('top-css')
@endsection
@section('top-javascript')
@endsection
</head>
<body>
@yield('body')
@section('bottom-javascript')
@endsection
</body>
</html>

View file

@ -0,0 +1,33 @@
@extends('body')
@section('sidebar')
@parent
<p>This is appended to the master sidebar.</p>
@endsection
@section('content')
<h1>Sign in</h1>
<ul>
@foreach ($hybridauth->getProviders() as $name)
@if (!isset($adapters[$name]))
<li>
<a href="#" onclick="javascript:auth_popup('{{ $name }}');">
Sign in with {{ $name }}
</a>
</li>
@endif
@endforeach
<ul>
@endsection
@section('top-javascript')
<script>
function auth_popup(provider) {
// replace 'path/to/hybridauth' with the real path to this script
var authWindow = window.open('/control/authentification-callback-example/provider/' + provider, 'authWindow', 'width=600,height=400,scrollbars=yes');
return false;
}
</script>
@endsection

View file

@ -0,0 +1,32 @@
@extends('body')
@section('sidebar')
@parent
<p>This is appended to the master sidebar.</p>
@endsection
@section('content')
<h1>Compte utilisateur</h1>
@foreach ($extractedData as $key => $value)
{{ $key }} :: {{ $value }}
@endforeach
@if ($adapters)
<h1>You are logged in:</h1>
<ul>
@foreach ($adapters as $name => $adapter)
<li>
<strong>{{$adapter->getUserProfile()->displayName }}</strong> from
<i>{{ $name }}</i>
<span>(<a href="{{$config['callback'] }}?logout={{ $name }}" ; ?>">Log Out</a>)</span>
</li>
@endforeach
</ul>
@endif
@endsection

View file

View file

@ -0,0 +1,81 @@
<?php
/**
* A simple example that shows how to use multiple providers, opening provider authentication in a pop-up.
*/
use Hybridauth\Hybridauth;
require CONFIG_PATH . DIRECTORY_SEPARATOR . "authentification-config-example.php";
try {
$hybridauth = new Hybridauth\Hybridauth($config);
$storage = new Hybridauth\Storage\Session();
$error = false;
//
// Event 1: User clicked SIGN-IN link
//
if (isset($url_params['provider'])) {
// Validate provider exists in the $config
if (in_array($url_params['provider'], $hybridauth->getProviders())) {
// Store the provider for the callback event
$storage->set('provider', $_GET['provider']);
} else {
$error = $_GET['provider'];
}
}
//
// Event 2: User clicked LOGOUT link
//
if (isset($url_params['logout'])) {
if (in_array($url_params['logout'], $hybridauth->getProviders())) {
// Disconnect the adapter
$adapter = $hybridauth->getAdapter($url_params['logout']);
$adapter->disconnect();
} else {
$error = $url_params['logout'];
}
}
//
// Handle invalid provider errors
//
if ($error) {
error_log('HybridAuth Error: Provider ' . json_encode($error) . ' not found or not enabled in $config');
// Close the pop-up window
echo "
<script>
window.opener.location.reload();
window.close();
</script>";
exit;
}
//
// Event 3: Provider returns via CALLBACK
//
if ($provider = $storage->get('provider')) {
$hybridauth->authenticate($provider);
$storage->set('provider', null);
// Retrieve the provider record
$adapter = $hybridauth->getAdapter($provider);
$userProfile = $adapter->getUserProfile();
$accessToken = $adapter->getAccessToken();
// Close pop-up window
echo "
<script>
window.opener.location.reload();
window.close();
</script>";
}
} catch (Exception $e) {
error_log($e->getMessage());
echo $e->getMessage();
}

View file

@ -0,0 +1,57 @@
<?php
require CONFIG_PATH . DIRECTORY_SEPARATOR . "authentification-config-example.php";
try {
//Feed configuration array to Hybridauth
$hybridauth = new \Hybridauth\Hybridauth($config);
//Then we can proceed and sign in with Twitter as an example. If you want to use a diffirent provider,
//simply replace 'Twitter' with 'Google' or 'Facebook'.
//Attempt to authenticate users with a Twitter provider
$adapter = $hybridauth->authenticate('Twitter');
//Returns a boolean of whether the user is connected with Twitter
$isConnected = $adapter->isConnected();
if ($isConnected == false) {
//Attempt to authenticate users with a Google provider
$adapter = $hybridauth->authenticate('Google');
$isConnected = $adapter->isConnected();
}
if ($isConnected == false) {
//Attempt to authenticate users with a Facebook provider
$adapter = $hybridauth->authenticate('Facebook');
$isConnected = $adapter->isConnected();
}
if ($isConnected == false) {
//Attempt to authenticate users with a Github provider
$adapter = $hybridauth->authenticate('Github');
$isConnected = $adapter->isConnected();
}
if ($isConnected) {
session_start();
//Retrieve the user's token
$token = $adapter->getAccessToken();
$_SESSION['accessToken'] = $token;
//Retrieve the user's profile
$userProfile = $adapter->getUserProfile();
$_SESSION['userProfile'] = $userProfile;
//Disconnect the adapter
$adapter->disconnect();
header("location:" . Url::link_rewrite(false, 'compte', []));
} else {
header("location:" . Url::link_rewrite(false, 'error', []));
}
} catch (\Exception $e) {
echo 'Oops, we ran into an issue! ' . $e->getMessage();
}