<?php
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

$config = parse_ini_file("config.ini");

// Initialize the OAuth client
$oauthClient = array(
    'clientId' => $config['OAUTH_APP_ID'],
    'clientSecret' => $config['OAUTH_APP_PASSWORD'],
    'redirectUri' => $config['OAUTH_REDIRECT_URI'],
    'urlAuthorize' => $config['OAUTH_AUTHORITY'] . $config['OAUTH_AUTHORIZE_ENDPOINT'],
    'urlAccessToken' => $config['OAUTH_AUTHORITY'] . $config['OAUTH_TOKEN_ENDPOINT'],
    'urlResourceOwnerDetails' => '',
    'scopes' => $config['OAUTH_SCOPES']
);

$PostmanClient = new \OAuthOffice365( $oauthClient );

$authorizationUrl = $PostmanClient->getOffice365AuthURL();

// Save client state so we can validate in response
$_SESSION['oauth_state'] = bin2hex(random_bytes( 32 / 2 ));

// Redirect to authorization endpoint
header('Location: ' . $authorizationUrl);
exit();