forked from julien-c/oauth2-example-auth-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth.php
226 lines (160 loc) · 4.96 KB
/
oauth.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
session_start();
ini_set('display_errors', true);
error_reporting(-1);
// Include the Composer autoloader
include 'vendor/autoload.php';
// Include the storage models
include 'model_client.php';
include 'model_scope.php';
include 'model_session.php';
// New Slim app
$app = new \Slim\Slim();
// Initiate the Request handler
$request = new \OAuth2\Util\Request();
// Initiate the auth server with the models
$server = new \OAuth2\AuthServer(new ClientModel, new SessionModel, new ScopeModel);
// Enable support for the authorization code grant
$server->addGrantType(new \OAuth2\Grant\AuthCode());
// Clients will redirect to this address
$app->get('/', function () use ($server, $app) {
// Tell the auth server to check the required parameters are in the query string
$params = $server->checkAuthoriseParams();
// Save the verified parameters to the user's session
$_SESSION['params'] = serialize($params);
// Redirect the user to sign-in
$app->redirect('/oauth.php/signin');
});
// Sign-in
$app->get('/signin', function () {
// Check the authorization params are set
if ( ! isset($_SESSION['params']))
{
throw new Exception('Missing auth parameters');
}
// Get the params from the session
$params = unserialize($_SESSION['params']);
?>
<form method="post">
<h1>Sign in to <?php echo $params['client_details']['name']; ?></h1>
<p>
<label for="username">Username: </label>
<input type="text" name="username" id="password" value="alex">
</p>
<p>
<label for="password">Password: </label>
<input type="password" name="password" id="password" value="password">
</p>
<p>
<input type="submit" name="submit" id="submit" value="Sign in">
</p>
</form>
<?php
});
// Process sign-in form submission
$app->post('/signin', function () use ($app) {
// Check the auth params are in the session
if ( ! isset($_SESSION['params']))
{
throw new Exception('Missing auth parameters');
}
$params = unserialize($_SESSION['params']);
// Check the user's credentials
if ($_POST['username'] === 'alex' && $_POST['password'] === 'password')
{
// Add the user ID to the auth params and forward the user to authorise the client
$params['user_id'] = 1;
$_SESSION['params'] = serialize($params);
$app->redirect('/oauth.php/authorise');
}
// Wrong username/password
else
{
$app->redirect('/oauth.php/signin');
}
});
// The user authorises the app
$app->get('/authorise', function () use ($app) {
// Check the auth params are in the session
if ( ! isset($_SESSION['params']))
{
throw new Exception('Missing auth parameters');
}
$params = unserialize($_SESSION['params']);
// Check the user is signed in
if ( ! isset($params['user_id']))
{
$app->redirect('/oauth.php/signin');
}
?>
<h1>Authorise <?php echo $params['client_details']['name']; ?></h1>
<p>
The application <strong><?php echo $params['client_details']['name']; ?></strong> would like permission to access your:
</p>
<ul>
<?php foreach ($params['scopes'] as $scope): ?>
<li>
<?php echo $scope['name']; ?>
</li>
<?php endforeach; ?>
</ul>
<p>
<form method="post" style="display:inline">
<input type="submit" name="approve" id="approve" value="Approve">
</form>
<form method="post" style="display:inline">
<input type="submit" name="deny" id="deny" value="Deny">
</form>
</p>
<?php
});
// Process authorise form
$app->post('/authorise', function() use ($server, $app) {
// Check the auth params are in the session
if ( ! isset($_SESSION['params']))
{
throw new Exception('Missing auth parameters');
}
$params = unserialize($_SESSION['params']);
// Check the user is signed in
if ( ! isset($params['user_id']))
{
$app->redirect('/oauth.php/signin');
}
// If the user approves the client then generate an authoriztion code
if (isset($_POST['approve']))
{
$authCode = $server->newAuthoriseRequest('user', $params['user_id'], $params);
echo '<p>The user authorised a request and so would be redirected back to the client...</p>';
// Generate the redirect URI
echo OAuth2\Util\RedirectUri::make($params['redirect_uri'], array(
'code' => $authCode,
'state' => $params['state']
));
}
// The user denied the request so send them back to the client with an error
elseif (isset($_POST['deny']))
{
echo '<p>The user denied the request and so would be redirected back to the client...</p>';
echo OAuth2\Util\RedirectUri::make($params['redirect_uri'], array(
'error' => 'access_denied',
'error_message' => $server::getExceptionMessage('access_denied'),
'state' => $params['state']
));
}
});
// The client will exchange the authorization code for an access token
$app->post('/access_token', function() use ($server) {
header('Content-type: application/javascript');
try {
// Issue an access token
$p = $server->issueAccessToken();
echo json_encode($p);
}
catch (Exception $e)
{
// Show an error message
echo json_encode(array('error' => $e->getMessage(), 'error_code' => $e->getCode()));
}
});
$app->run();