ldap-matter/oauth/authorize.php

136 lines
3.9 KiB
PHP
Raw Normal View History

2017-08-08 03:01:11 +08:00
<?php
session_start();
/**
* @author Denis CLAVIER <clavierd at gmail dot com>
* Adapted from Oauth2-server-php cookbook
* @see http://bshaffer.github.io/oauth2-server-php-docs/cookbook/
*/
// include our OAuth2 Server object
require_once __DIR__.'/server.php';
$request = OAuth2\Request::createFromGlobals();
$response = new OAuth2\Response();
2020-04-30 21:43:07 +08:00
// If user has clicked on "not me" link, disconnect him by cleaning PHP SESSION variables.
if ($_POST['disconnect']) {
$_SESSION=array();
}
// Validate the authorize request
2017-08-08 03:01:11 +08:00
if (!$server->validateAuthorizeRequest($request, $response)) {
$response->send();
die;
}
2020-04-30 21:43:07 +08:00
// If user is not yet authenticated, he is redirected.
2017-08-08 03:01:11 +08:00
if (!isset($_SESSION['uid']))
{
2020-04-30 21:43:07 +08:00
// Store the authorize request
$explode_url=explode("/", strip_tags(trim($_SERVER['REQUEST_URI'])));
$_SESSION['auth_page']=end($explode_url);
2017-08-08 03:01:11 +08:00
header('Location: index.php');
exit();
}
2020-04-30 21:43:07 +08:00
// Check if user has already authorized oauth to share data with Mattermost. In this case, user should exist in 'user' table.
if ($server->userExists($_SESSION['uid'])) {
// Bypass authorize form, continue Oauth process.
$server->handleAuthorizeRequest($request, $response, true, $_SESSION['uid']);
}
// Display an authorization form
else if (empty($_POST)) {
2017-08-08 03:01:11 +08:00
exit('
<!DOCTYPE html>
<html>
<head>
2017-11-15 00:13:48 +08:00
<meta charset="UTF-8" />
<link rel="stylesheet" type="text/css" href="./style.css">
<title>Authorisation Mattermost</title>
</head>
<body>
<center>
<table background="images/login.png" border="0" width="729" height="343" cellspacing="1" cellpadding="4">
<tr>
<td width="40%">&nbsp;</td>
2020-04-30 21:43:07 +08:00
<td width="60%">
<table border="0" width="100%">
<tr>
<td align="center">
<div class="LoginTitle">Mattermost desires access to your LDAP data:</div>
2020-04-30 21:43:07 +08:00
<form method="post">
2020-04-30 21:43:07 +08:00
<table border="0" width="90%" cellpadding="1">
<tr>
<td colspan="2" align="left">
2020-04-30 21:43:07 +08:00
<div class="messageLogin" align="center">
2020-04-30 21:43:07 +08:00
</div>
&nbsp;
</td>
</tr>
<tr>
<td align="center" width="100%" class="LoginUsername">
2020-04-30 21:43:07 +08:00
Login as : <b>' . $_SESSION['uid'] . ' </b> <button type="submit" class="link" name="disconnect" value="true" ><span>(not me ?)</span></button>
</td>
</tr>
<tr>
<td align="left" width="100%" class="LoginUsername">
2020-04-30 21:43:07 +08:00
<br/>
Requested Data : <br/>
&nbsp; -> Username,<br/>
2020-04-30 21:43:07 +08:00
&nbsp; -> Full Name,<br/>
&nbsp; -> Email
2020-04-30 21:43:07 +08:00
</td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr>
2020-04-30 21:43:07 +08:00
<td colspan="2" align="center"> <input type="submit" class="GreenButton" name="authorized" value="Authorize" >
<input type="submit" class="GreenButton" name="authorized" value="Deny" > </td>
</tr>
2020-04-30 21:43:07 +08:00
</table>
</form>
2020-04-30 21:43:07 +08:00
</td>
</tr>
</table>
2020-04-30 21:43:07 +08:00
</td>
</tr>
</table>
</center>
</body>
</html>
');
2017-08-08 03:01:11 +08:00
}
2020-04-30 21:43:07 +08:00
else {
// Print the authorization code if the user has authorized your client
$is_authorized = ($_POST['authorized'] === 'Authorize');
$server->handleAuthorizeRequest($request, $response, $is_authorized, $_SESSION['uid']);
}
2017-08-08 03:01:11 +08:00
2020-04-30 21:43:07 +08:00
if ($is_authorized)
2017-08-08 03:01:11 +08:00
{
// This is only here so that you get to see your code in the cURL request. Otherwise, we'd redirect back to the client
2017-08-08 03:01:11 +08:00
$code = substr($response->getHttpHeader('Location'), strpos($response->getHttpHeader('Location'), 'code=')+5, 40);
header('Location: ' . $response->getHttpHeader('Location'));
exit();
}
// Send message in case of error
2020-04-30 21:43:07 +08:00
$response->send();