-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.php
95 lines (79 loc) · 2.63 KB
/
auth.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
<?php
/**
* Routes for prefix "/auth"
*/
on('GET', '/callback', function () {
if (isset($_GET['oauth_verifier'])) {
$_SESSION['oauthVerifier'] = $_GET['oauth_verifier'];
if (isset($_SESSION['accessToken'])) {
flash('error', 'Temporary credentials may only be exchanged for token credentials once');
redirect('/');
}
try {
$client = new Evernote\Client(array(
'consumerKey' => config('evernote.oauth_consumer_key'),
'consumerSecret' => config('evernote.oauth_consumer_secret'),
'sandbox' => config('evernote.sandbox')
));
$accessTokenInfo = $client->getAccessToken(
$_SESSION['requestToken'],
$_SESSION['requestTokenSecret'],
$_SESSION['oauthVerifier']
);
if ($accessTokenInfo) {
$_SESSION['accessToken'] = $accessTokenInfo['oauth_token'];
// The authenticated action
flash('success', 'Welcome back.');
redirect('/user/edit');
}
else {
flash('error', 'Failed to obtain token credentials.');
}
}
catch (OAuthException $e) {
flash('error', 'Error obtaining token credentials: ' . $e->getMessage());
}
}
else {
flash('error', 'Content owner did not authorize the temporary credentials');
}
redirect('/');
});
on('GET', '/authorize', function () {
try {
$client = new Evernote\Client(array(
'consumerKey' => config('evernote.oauth_consumer_key'),
'consumerSecret' => config('evernote.oauth_consumer_secret'),
'sandbox' => config('evernote.sandbox')
));
$requestTokenInfo = $client->getRequestToken(
strtr(config('evernote.oauth_callback_url'), array(
'%schema' => empty($_SERVER['HTTPS']) ? "http" : "https",
'%host' => $_SERVER['SERVER_NAME'],
))
);
if ($requestTokenInfo) {
$_SESSION['requestToken'] = $requestTokenInfo['oauth_token'];
$_SESSION['requestTokenSecret'] = $requestTokenInfo['oauth_token_secret'];
redirect($client->getAuthorizeUrl($_SESSION['requestToken']));
}
else {
flash('error', 'Failed to obtain temporary credentials.');
}
}
catch (OAuthException $e) {
flash('error', 'Error obtaining temporary credentials: ' . $e->getMessage());
}
redirect('/');
});
on('GET', '/logout', function () {
unset($_SESSION['account']);
unset($_SESSION['requestToken']);
unset($_SESSION['requestTokenSecret']);
unset($_SESSION['oauthVerifier']);
unset($_SESSION['accessToken']);
unset($_SESSION['accessTokenSecret']);
unset($_SESSION['tokenExpires']);
flash('success', 'You are now logged out');
redirect('/');
});