-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathindex.php
121 lines (101 loc) · 3.21 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* SocialConnect project
* @author: Patsura Dmitry https://github.com/ovr <[email protected]>
*/
error_reporting(-1);
ini_set('display_errors', 1);
include_once __DIR__ . '/../vendor/autoload.php';
include_once __DIR__ . '/vendor/autoload.php';
$configureProviders = include_once 'config.php';
$httpClient = new \SocialConnect\HttpClient\Curl();
/**
* By default We are using Curl class from SocialConnect/Common
* but you can use Guzzle wrapper ^5.3|^6.0
*/
//$httpClient = new \Http\Adapter\Guzzle6\Client(
// new \GuzzleHttp\Client()
//);
/**
* Why We need Cache decorator for HTTP Client?
* Providers like OpenID & OpenIDConnect require US
* to request OpenID specification (and JWK(s) for OpenIDConnect)
*
* It's not a good idea to request it every time, because it's unneeded round trip to the server
* if you are using OpenID or OpenIDConnect we suggest you to use cache
*
* If you don`t use providers like (Steam) from OpenID or OpenIDConnect
* you may skip this because it's not needed
*/
//$httpClient = new \SocialConnect\HttpClient\Cache(
// $httpClient,
// /**
// * You can use any library with PSR-16 (simple-cache) compatibility
// */
// new \Symfony\Component\Cache\Psr16Cache(
// new \Symfony\Component\Cache\Adapter\PhpFilesAdapter(
// 'socialconnect',
// 0,
// __DIR__ . '/cache'
// )
// )
//);
/**
* By default collection factory is null, in this case Auth\Service will create
* a new instance of \SocialConnect\Auth\CollectionFactory
* you can use custom or register another providers by CollectionFactory instance
*/
$collectionFactory = null;
$service = new \SocialConnect\Auth\Service(
new \SocialConnect\Common\HttpStack(
$httpClient,
new \SocialConnect\HttpClient\RequestFactory(),
new \SocialConnect\HttpClient\StreamFactory()
),
new \SocialConnect\Provider\Session\Session(),
$configureProviders,
$collectionFactory
);
$app = new \Slim\App(
[
'settings' => [
'displayErrorDetails' => true
]
]
);
$app->any('/dump', function() {
dump($_POST);
dump($_GET);
dump($_SERVER);
});
$app->get('/auth/cb/{provider}/', function (\Slim\Http\Request $request) use ($service) {
$provider = strtolower($request->getAttribute('provider'));
if (!$service->has($provider)) {
throw new \Exception('Wrong $provider passed in url : ' . $provider);
}
$provider = $service->getProvider($provider);
$accessToken = $provider->getAccessTokenByRequestParameters($_GET);
dump($accessToken);
dump($accessToken->getUserId());
dump($accessToken->getExpires());
$user = $provider->getIdentity($accessToken);
dump($user);
});
$app->get('/', function () {
include_once 'page.php';
});
$app->post('/', function () use ($service) {
try {
if (!empty($_POST['provider'])) {
$providerName = $_POST['provider'];
} else {
throw new \Exception('No provider passed in POST Request');
}
$provider = $service->getProvider($providerName);
header('Location: ' . $provider->makeAuthUrl());
} catch (\Exception $e) {
echo $e->getMessage();
}
exit;
});
$app->run();