* 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(); // If user has clicked on "not me" link, disconnect him by cleaning PHP SESSION variables. if ($_POST['disconnect']) { $_SESSION=array(); } // Validate the authorize request if (!$server->validateAuthorizeRequest($request, $response)) { $response->send(); die; } // If user is not yet authenticated, he is redirected. if (!isset($_SESSION['uid'])) { // Store the authorize request $explode_url=explode("/", strip_tags(trim($_SERVER['REQUEST_URI']))); $_SESSION['auth_page']=end($explode_url); header('Location: index.php'); exit(); } // 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'])) { // User had already authorized the client during a previous session. $is_authorized = true; } // Display an authorization form else if (empty($_POST)) { exit(' Mattermost - LDAP Authorization

LDAP Authentication

authentication icon

Authorize Mattermost to get the following data:

  Full Name
  E-mail
Login as : ' . $_SESSION['uid'] . '

'); } else { // Check if user has authorized to share his data with the client $is_authorized = ($_POST['authorized'] === 'Authorize'); } // Print the authorization code if the user has authorized your client $server->handleAuthorizeRequest($request, $response, $is_authorized,$_SESSION['uid']); if ($is_authorized) { // This is only here so that you get to see your code in the cURL request. Otherwise, we'd redirect back to the client $code = substr($response->getHttpHeader('Location'), strpos($response->getHttpHeader('Location'), 'code=')+5, 40); header('Location: ' . $response->getHttpHeader('Location')); exit(); } // Send message in case of error $response->send();