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.
Emmanuel ROY authored on 26/09/2019 04:27:05... | ... |
@@ -3,6 +3,8 @@ |
3 | 3 |
namespace MVC\Classe; |
4 | 4 |
|
5 | 5 |
define( "CONTROLLERS_PATH" , APPLICATION_PATH . DIRECTORY_SEPARATOR . "include" . DIRECTORY_SEPARATOR . "controlleurs"); |
6 |
+define("CONFIG_PATH", APPLICATION_PATH . DIRECTORY_SEPARATOR . "config"); |
|
7 |
+define("LOG_PATH", APPLICATION_PATH . DIRECTORY_SEPARATOR . "logs"); |
|
6 | 8 |
|
7 | 9 |
require APPLICATION_PATH . DIRECTORY_SEPARATOR . "parameters.php"; |
8 | 10 |
|
9 | 11 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,20 @@ |
1 |
+<?php |
|
2 |
+ |
|
3 |
+ |
|
4 |
+namespace MVC\Classe; |
|
5 |
+ |
|
6 |
+ |
|
7 |
+class Session |
|
8 |
+{ |
|
9 |
+ |
|
10 |
+ static public function isRegistered() |
|
11 |
+ { |
|
12 |
+ if (isset($_SESSION['userProfile'])) { |
|
13 |
+ return; |
|
14 |
+ } else { |
|
15 |
+ header("location : " . Url::link_rewrite(false, 'error', [])); |
|
16 |
+ die('Ooops, something was wrong...'); |
|
17 |
+ } |
|
18 |
+ } |
|
19 |
+ |
|
20 |
+} |
|
0 | 21 |
\ No newline at end of file |
... | ... |
@@ -85,4 +85,33 @@ class Url |
85 | 85 |
$this->page = $page; |
86 | 86 |
|
87 | 87 |
} |
88 |
+ |
|
89 |
+ static public function link_rewrite($isControlPatern, $page, $params = array()) |
|
90 |
+ { |
|
91 |
+ if ($isControlPatern) { |
|
92 |
+ return self::controlLink_rewrite($page, $params); |
|
93 |
+ } else { |
|
94 |
+ return self::link_rewrite_slashParam($page, $params); |
|
95 |
+ } |
|
96 |
+ } |
|
97 |
+ |
|
98 |
+ static private function link_rewrite_slashParam($page, $params = array()) |
|
99 |
+ { |
|
100 |
+ $stringParams = ''; |
|
101 |
+ foreach ($params as $key => $values) { |
|
102 |
+ $stringParams .= "/" . $key . "/" . $values; |
|
103 |
+ } |
|
104 |
+ return '/' . $page . $stringParams; |
|
105 |
+ |
|
106 |
+ } |
|
107 |
+ |
|
108 |
+ static private function controlLink_rewrite($page, $params = array()) |
|
109 |
+ { |
|
110 |
+ $stringParams = ''; |
|
111 |
+ foreach ($params as $key => $values) { |
|
112 |
+ $stringParams .= "/" . $key . "/" . $values; |
|
113 |
+ } |
|
114 |
+ return '/' . 'control' . '/' . $page . $stringParams; |
|
115 |
+ } |
|
116 |
+ |
|
88 | 117 |
} |
89 | 118 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,50 @@ |
1 |
+<?php |
|
2 |
+/** |
|
3 |
+ * Build a configuration array to pass to `Hybridauth\Hybridauth` |
|
4 |
+ * |
|
5 |
+ */ |
|
6 |
+$config = [ |
|
7 |
+//Location where to redirect users once they authenticate with a provider |
|
8 |
+ 'callback' => \MVC\Url::link_rewrite(false, 'accueil', []), |
|
9 |
+ |
|
10 |
+//Providers specifics |
|
11 |
+ 'providers' => [ |
|
12 |
+ 'GitHub' => [ |
|
13 |
+ 'enabled' => true, |
|
14 |
+ 'keys' => ['id' => '', 'secret' => ''], |
|
15 |
+ ], |
|
16 |
+ |
|
17 |
+ 'Google' => [ |
|
18 |
+ 'enabled' => true, |
|
19 |
+ 'keys' => ['id' => '', 'secret' => ''], |
|
20 |
+ ], |
|
21 |
+ |
|
22 |
+ 'Facebook' => [ |
|
23 |
+ 'enabled' => true, |
|
24 |
+ 'keys' => ['id' => '', 'secret' => ''], |
|
25 |
+ ], |
|
26 |
+ |
|
27 |
+ 'Twitter' => [ |
|
28 |
+ 'enabled' => true, |
|
29 |
+ 'keys' => ['key' => '', 'secret' => ''], |
|
30 |
+ ] |
|
31 |
+ ], |
|
32 |
+ //optional : set debug mode |
|
33 |
+ 'debug_mode' => true, |
|
34 |
+ // Path to file writeable by the web server. Required if 'debug_mode' is not false |
|
35 |
+ 'debug_file' => LOG_PATH . DIRECTORY_SEPARATOR . 'hybridauth.log', |
|
36 |
+ |
|
37 |
+ /* optional : customize Curl settings |
|
38 |
+ // for more information on curl, refer to: http://www.php.net/manual/fr/function.curl-setopt.php |
|
39 |
+ 'curl_options' => [ |
|
40 |
+ // setting custom certificates |
|
41 |
+ CURLOPT_SSL_VERIFYPEER => true, |
|
42 |
+ CURLOPT_CAINFO => '/path/to/your/certificate.crt', |
|
43 |
+ |
|
44 |
+ // set a valid proxy ip address |
|
45 |
+ CURLOPT_PROXY => '*.*.*.*:*', |
|
46 |
+ |
|
47 |
+ // set a custom user agent |
|
48 |
+ CURLOPT_USERAGENT => '' |
|
49 |
+ ] */ |
|
50 |
+]; |
0 | 51 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,9 @@ |
1 |
+<?php |
|
2 |
+ |
|
3 |
+require CONFIG_PATH . DIRECTORY_SEPARATOR . "authentification-config-example.php"; |
|
4 |
+ |
|
5 |
+$hybridauth = new Hybridauth\Hybridauth($config); |
|
6 |
+$adapters = $hybridauth->getConnectedAdapters(); |
|
7 |
+ |
|
8 |
+$templateData['hybridauth'] = $hybridauth; |
|
9 |
+$templateData['adapters'] = $adapters; |
|
0 | 10 |
\ No newline at end of file |
1 | 11 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,18 @@ |
1 |
+<?php |
|
2 |
+\MVC\Session::isregistered(); |
|
3 |
+ |
|
4 |
+require CONFIG_PATH . DIRECTORY_SEPARATOR . "authentification-config-example.php"; |
|
5 |
+ |
|
6 |
+$hybridauth = new Hybridauth\Hybridauth($config); |
|
7 |
+$adapters = $hybridauth->getConnectedAdapters(); |
|
8 |
+ |
|
9 |
+$templateData['adapters'] = $adapters; |
|
10 |
+ |
|
11 |
+$templateData['extractedData'] = [ |
|
12 |
+ 'token' => $_SESSION['userToken'], |
|
13 |
+ 'identifier' => $_SESSION['userProfile']->identifier, |
|
14 |
+ 'email' => $_SESSION['userProfile']->email, |
|
15 |
+ 'first_name' => $_SESSION['userProfile']->firstName, |
|
16 |
+ 'last_name' => $_SESSION['userProfile']->lastName, |
|
17 |
+ 'photoURL' => strtok($_SESSION['userProfile']->photoURL, '?'), |
|
18 |
+]; |
... | ... |
@@ -1,9 +1,24 @@ |
1 | 1 |
<html> |
2 |
+ |
|
2 | 3 |
<head> |
3 | 4 |
<title>{{$page_title}}</title> |
4 |
- <meta name="description" lang="fr" content="{{$description}}" /> |
|
5 |
+ <meta name="description" lang="fr" content="{{$description}}"/> |
|
6 |
+ |
|
7 |
+ @section('top-css') |
|
8 |
+ @endsection |
|
9 |
+ |
|
10 |
+ @section('top-javascript') |
|
11 |
+ @endsection |
|
12 |
+ |
|
5 | 13 |
</head> |
14 |
+ |
|
6 | 15 |
<body> |
16 |
+ |
|
7 | 17 |
@yield('body') |
18 |
+ |
|
19 |
+@section('bottom-javascript') |
|
20 |
+@endsection |
|
21 |
+ |
|
8 | 22 |
</body> |
23 |
+ |
|
9 | 24 |
</html> |
10 | 25 |
\ No newline at end of file |
11 | 26 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,33 @@ |
1 |
+@extends('body') |
|
2 |
+ |
|
3 |
+@section('sidebar') |
|
4 |
+ @parent |
|
5 |
+ |
|
6 |
+ <p>This is appended to the master sidebar.</p> |
|
7 |
+@endsection |
|
8 |
+ |
|
9 |
+@section('content') |
|
10 |
+ <h1>Sign in</h1> |
|
11 |
+ |
|
12 |
+ <ul> |
|
13 |
+ @foreach ($hybridauth->getProviders() as $name) |
|
14 |
+ @if (!isset($adapters[$name])) |
|
15 |
+ <li> |
|
16 |
+ <a href="#" onclick="javascript:auth_popup('{{ $name }}');"> |
|
17 |
+ Sign in with {{ $name }} |
|
18 |
+ </a> |
|
19 |
+ </li> |
|
20 |
+ @endif |
|
21 |
+ @endforeach |
|
22 |
+ <ul> |
|
23 |
+ @endsection |
|
24 |
+ |
|
25 |
+ @section('top-javascript') |
|
26 |
+ <script> |
|
27 |
+ function auth_popup(provider) { |
|
28 |
+ // replace 'path/to/hybridauth' with the real path to this script |
|
29 |
+ var authWindow = window.open('/control/authentification-callback-example/provider/' + provider, 'authWindow', 'width=600,height=400,scrollbars=yes'); |
|
30 |
+ return false; |
|
31 |
+ } |
|
32 |
+ </script> |
|
33 |
+@endsection |
0 | 34 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,32 @@ |
1 |
+@extends('body') |
|
2 |
+ |
|
3 |
+@section('sidebar') |
|
4 |
+ @parent |
|
5 |
+ |
|
6 |
+ <p>This is appended to the master sidebar.</p> |
|
7 |
+@endsection |
|
8 |
+ |
|
9 |
+@section('content') |
|
10 |
+ |
|
11 |
+ <h1>Compte utilisateur</h1> |
|
12 |
+ |
|
13 |
+ @foreach ($extractedData as $key => $value) |
|
14 |
+ {{ $key }} :: {{ $value }} |
|
15 |
+ @endforeach |
|
16 |
+ |
|
17 |
+ @if ($adapters) |
|
18 |
+ <h1>You are logged in:</h1> |
|
19 |
+ <ul> |
|
20 |
+ @foreach ($adapters as $name => $adapter) |
|
21 |
+ <li> |
|
22 |
+ <strong>{{$adapter->getUserProfile()->displayName }}</strong> from |
|
23 |
+ <i>{{ $name }}</i> |
|
24 |
+ <span>(<a href="{{$config['callback'] }}?logout={{ $name }}" ; ?>">Log Out</a>)</span> |
|
25 |
+ </li> |
|
26 |
+ @endforeach |
|
27 |
+ </ul> |
|
28 |
+ @endif |
|
29 |
+ |
|
30 |
+ |
|
31 |
+@endsection |
|
32 |
+ |
1 | 34 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,81 @@ |
1 |
+<?php |
|
2 |
+/** |
|
3 |
+ * A simple example that shows how to use multiple providers, opening provider authentication in a pop-up. |
|
4 |
+ */ |
|
5 |
+ |
|
6 |
+use Hybridauth\Hybridauth; |
|
7 |
+ |
|
8 |
+require CONFIG_PATH . DIRECTORY_SEPARATOR . "authentification-config-example.php"; |
|
9 |
+ |
|
10 |
+try { |
|
11 |
+ |
|
12 |
+ $hybridauth = new Hybridauth\Hybridauth($config); |
|
13 |
+ $storage = new Hybridauth\Storage\Session(); |
|
14 |
+ $error = false; |
|
15 |
+ |
|
16 |
+ // |
|
17 |
+ // Event 1: User clicked SIGN-IN link |
|
18 |
+ // |
|
19 |
+ if (isset($url_params['provider'])) { |
|
20 |
+ // Validate provider exists in the $config |
|
21 |
+ if (in_array($url_params['provider'], $hybridauth->getProviders())) { |
|
22 |
+ // Store the provider for the callback event |
|
23 |
+ $storage->set('provider', $_GET['provider']); |
|
24 |
+ } else { |
|
25 |
+ $error = $_GET['provider']; |
|
26 |
+ } |
|
27 |
+ } |
|
28 |
+ |
|
29 |
+ // |
|
30 |
+ // Event 2: User clicked LOGOUT link |
|
31 |
+ // |
|
32 |
+ if (isset($url_params['logout'])) { |
|
33 |
+ if (in_array($url_params['logout'], $hybridauth->getProviders())) { |
|
34 |
+ // Disconnect the adapter |
|
35 |
+ $adapter = $hybridauth->getAdapter($url_params['logout']); |
|
36 |
+ $adapter->disconnect(); |
|
37 |
+ } else { |
|
38 |
+ $error = $url_params['logout']; |
|
39 |
+ } |
|
40 |
+ } |
|
41 |
+ |
|
42 |
+ // |
|
43 |
+ // Handle invalid provider errors |
|
44 |
+ // |
|
45 |
+ if ($error) { |
|
46 |
+ error_log('HybridAuth Error: Provider ' . json_encode($error) . ' not found or not enabled in $config'); |
|
47 |
+ // Close the pop-up window |
|
48 |
+ echo " |
|
49 |
+ <script> |
|
50 |
+ window.opener.location.reload(); |
|
51 |
+ window.close(); |
|
52 |
+ </script>"; |
|
53 |
+ exit; |
|
54 |
+ } |
|
55 |
+ |
|
56 |
+ // |
|
57 |
+ // Event 3: Provider returns via CALLBACK |
|
58 |
+ // |
|
59 |
+ if ($provider = $storage->get('provider')) { |
|
60 |
+ |
|
61 |
+ $hybridauth->authenticate($provider); |
|
62 |
+ $storage->set('provider', null); |
|
63 |
+ |
|
64 |
+ // Retrieve the provider record |
|
65 |
+ $adapter = $hybridauth->getAdapter($provider); |
|
66 |
+ $userProfile = $adapter->getUserProfile(); |
|
67 |
+ $accessToken = $adapter->getAccessToken(); |
|
68 |
+ |
|
69 |
+ // Close pop-up window |
|
70 |
+ echo " |
|
71 |
+ <script> |
|
72 |
+ window.opener.location.reload(); |
|
73 |
+ window.close(); |
|
74 |
+ </script>"; |
|
75 |
+ |
|
76 |
+ } |
|
77 |
+ |
|
78 |
+} catch (Exception $e) { |
|
79 |
+ error_log($e->getMessage()); |
|
80 |
+ echo $e->getMessage(); |
|
81 |
+} |
0 | 82 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,57 @@ |
1 |
+<?php |
|
2 |
+ |
|
3 |
+require CONFIG_PATH . DIRECTORY_SEPARATOR . "authentification-config-example.php"; |
|
4 |
+ |
|
5 |
+try { |
|
6 |
+ //Feed configuration array to Hybridauth |
|
7 |
+ $hybridauth = new \Hybridauth\Hybridauth($config); |
|
8 |
+ |
|
9 |
+ //Then we can proceed and sign in with Twitter as an example. If you want to use a diffirent provider, |
|
10 |
+ //simply replace 'Twitter' with 'Google' or 'Facebook'. |
|
11 |
+ |
|
12 |
+ //Attempt to authenticate users with a Twitter provider |
|
13 |
+ $adapter = $hybridauth->authenticate('Twitter'); |
|
14 |
+ //Returns a boolean of whether the user is connected with Twitter |
|
15 |
+ $isConnected = $adapter->isConnected(); |
|
16 |
+ |
|
17 |
+ if ($isConnected == false) { |
|
18 |
+ //Attempt to authenticate users with a Google provider |
|
19 |
+ $adapter = $hybridauth->authenticate('Google'); |
|
20 |
+ $isConnected = $adapter->isConnected(); |
|
21 |
+ } |
|
22 |
+ if ($isConnected == false) { |
|
23 |
+ //Attempt to authenticate users with a Facebook provider |
|
24 |
+ $adapter = $hybridauth->authenticate('Facebook'); |
|
25 |
+ $isConnected = $adapter->isConnected(); |
|
26 |
+ } |
|
27 |
+ if ($isConnected == false) { |
|
28 |
+ //Attempt to authenticate users with a Github provider |
|
29 |
+ $adapter = $hybridauth->authenticate('Github'); |
|
30 |
+ $isConnected = $adapter->isConnected(); |
|
31 |
+ } |
|
32 |
+ |
|
33 |
+ |
|
34 |
+ if ($isConnected) { |
|
35 |
+ session_start(); |
|
36 |
+ //Retrieve the user's token |
|
37 |
+ $token = $adapter->getAccessToken(); |
|
38 |
+ $_SESSION['accessToken'] = $token; |
|
39 |
+ |
|
40 |
+ //Retrieve the user's profile |
|
41 |
+ $userProfile = $adapter->getUserProfile(); |
|
42 |
+ $_SESSION['userProfile'] = $userProfile; |
|
43 |
+ |
|
44 |
+ //Disconnect the adapter |
|
45 |
+ $adapter->disconnect(); |
|
46 |
+ |
|
47 |
+ header("location:" . Url::link_rewrite(false, 'compte', [])); |
|
48 |
+ |
|
49 |
+ } else { |
|
50 |
+ |
|
51 |
+ header("location:" . Url::link_rewrite(false, 'error', [])); |
|
52 |
+ |
|
53 |
+ } |
|
54 |
+ |
|
55 |
+} catch (\Exception $e) { |
|
56 |
+ echo 'Oops, we ran into an issue! ' . $e->getMessage(); |
|
57 |
+} |
|
0 | 58 |
\ No newline at end of file |