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'])));
|
2017-08-17 06:05:49 +08:00
|
|
|
$_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'])) {
|
2020-04-30 21:43:07 +08:00
|
|
|
// User had already authorized the client during a previous session.
|
|
|
|
$is_authorized = true;
|
2020-04-30 21:43:07 +08:00
|
|
|
}
|
|
|
|
// Display an authorization form
|
|
|
|
else if (empty($_POST)) {
|
2017-08-08 03:01:11 +08:00
|
|
|
exit('
|
2017-08-26 21:54:24 +08:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
2020-05-02 02:41:24 +08:00
|
|
|
<head>
|
|
|
|
<link rel="stylesheet" type="text/css" href="./style.css">
|
|
|
|
<title>Mattermost - LDAP Authorization</title>
|
2019-12-01 00:55:32 +08:00
|
|
|
|
2020-05-02 02:41:24 +08:00
|
|
|
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css"
|
|
|
|
integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">
|
|
|
|
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400" rel="stylesheet">
|
2019-12-01 00:55:32 +08:00
|
|
|
|
2020-05-02 02:41:24 +08:00
|
|
|
</head>
|
2019-12-01 00:55:32 +08:00
|
|
|
|
2020-05-02 02:41:24 +08:00
|
|
|
<body>
|
|
|
|
<div id="form-wrapper" style="text-align: center;">
|
|
|
|
<div id="form_credentials">
|
|
|
|
<h1>LDAP Authentication</h1>
|
|
|
|
<div id="form_icon">
|
|
|
|
<img src="./images/auth_icon.png" alt="authentication icon" >
|
|
|
|
</div>
|
|
|
|
<br>
|
|
|
|
<h2>Authorize Mattermost to get the following data:</h2>
|
|
|
|
<table>
|
2017-08-26 21:54:24 +08:00
|
|
|
<tr>
|
2020-05-02 02:41:24 +08:00
|
|
|
<td>
|
|
|
|
<strong>Full Name</strong><br/>
|
|
|
|
<strong>E-mail</strong><br/>
|
|
|
|
</td>
|
2017-08-26 21:54:24 +08:00
|
|
|
</tr>
|
|
|
|
</table>
|
2020-05-02 03:22:34 +08:00
|
|
|
<br/>
|
|
|
|
Logged as : <strong>' . $_SESSION['uid'] . ' </strong> <button type="submit" class="link" name="disconnect" value="true" ><span>(not me ?)</span></button>
|
|
|
|
<br/>
|
|
|
|
<br/>
|
2019-12-01 00:55:32 +08:00
|
|
|
|
2020-05-02 02:41:24 +08:00
|
|
|
<form method="POST">
|
|
|
|
<input type="submit" value="Authorize" name="authorized" id="input_accept" class="input_field">
|
|
|
|
<input type="submit" value="Deny" name="authorized" id="input_deny" class="input_field">
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</body>
|
2017-08-26 21:54:24 +08:00
|
|
|
</html>
|
2019-12-01 00:55:32 +08:00
|
|
|
');
|
2017-08-08 03:01:11 +08:00
|
|
|
}
|
2020-04-30 21:43:07 +08:00
|
|
|
else {
|
|
|
|
// Check if user has authorized to share his data with the client
|
|
|
|
$is_authorized = ($_POST['authorized'] === 'Authorize');
|
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
|
|
|
// Print the authorization code if the user has authorized your client
|
|
|
|
$server->handleAuthorizeRequest($request, $response, $is_authorized,$_SESSION['uid']);
|
2017-08-08 03:01:11 +08:00
|
|
|
|
2020-05-02 03:22:34 +08:00
|
|
|
// Authentication process is terminated, session can be destroyed.
|
|
|
|
$_SESSION=array();
|
|
|
|
|
2020-04-30 21:43:07 +08:00
|
|
|
if ($is_authorized)
|
2017-08-08 03:01:11 +08:00
|
|
|
{
|
2018-05-20 00:04:13 +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();
|