//get_oauth_token.php * e.g.: http://localhost/ocomon/ocomon_desenv/admin/oauth/get_oauth_token.php * * Ensure dependencies are installed with 'composer install' * * Set up an app in your Google/Yahoo/Microsoft account * * Set the script address as the app's redirect URL * If no refresh token is obtained when running this file, * revoke access to your app and run the script again. */ /** * Aliases for League Provider Classes * Make sure you have added these to your composer.json and run `composer install` * Plenty to choose from here: * @see https://oauth2-client.thephpleague.com/providers/thirdparty/ */ //@see https://github.com/thephpleague/oauth2-google // use League\OAuth2\Client\Provider\Google; //@see https://packagist.org/packages/hayageek/oauth2-yahoo // use Hayageek\OAuth2\Client\Provider\Yahoo; //@see https://github.com/stevenmaguire/oauth2-microsoft // use Stevenmaguire\OAuth2\Client\Provider\Microsoft; //@see https://github.com/greew/oauth2-azure-provider // use Greew\OAuth2\Client\Provider\Azure; if (!isset($_SESSION['s_logado']) || $_SESSION['s_logado'] == 0) { $_SESSION['session_expired'] = 1; echo ""; exit; } require_once __DIR__ . "/" . "../../includes/include_geral_new.inc.php"; require_once __DIR__ . "/" . "../../includes/classes/ConnectPDO.php"; use includes\classes\ConnectPDO; $conn = ConnectPDO::getInstance(); $auth = new AuthNew($_SESSION['s_logado'], $_SESSION['s_nivel'], 1); $basicConfig = getConfig($conn); $siteUrl = $basicConfig['conf_ocomon_site']; $siteUrl = rtrim($siteUrl, '/'); $thisFileRelativePath = '/admin/oauth/get_oauth_token.php'; $redirectUri = $siteUrl . $thisFileRelativePath; ?> <?= APP_NAME; ?> <?= VERSAO; ?>

 

' . TRANS('HELPER_CREDENTIALS_OAUTH_IMAP'), '', '', true); ?>
$clientId, 'clientSecret' => $clientSecret, 'redirectUri' => $redirectUri, 'accessType' => 'offline' // ,'prompt' => 'consent', ]; $options = []; $provider = null; $params['tenantId'] = $tenantId; $provider = new Azure($params); $options = [ 'scope' => [ 'https://outlook.office.com/IMAP.AccessAsUser.All', // 'https://outlook.office.com/SMTP.Send', 'offline_access' ] ]; $configKeys = []; $configKeys['IMAP_OAUTH_PROVIDER'] = $providerName; $configKeys['IMAP_OAUTH_CLIENT_ID'] = $clientId; $configKeys['IMAP_OAUTH_CLIENT_SECRET'] = $clientSecret; $configKeys['IMAP_OAUTH_TENANT_ID'] = $tenantId; if (null === $provider) { exit('Provider missing'); } if (!isset($_GET['code'])) { //If we don't have an authorization code then get one $authUrl = $provider->getAuthorizationUrl($options); $_SESSION['oauth2state'] = $provider->getState(); header('Location: ' . $authUrl); exit; //Check given state against previously stored one to mitigate CSRF attack } elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) { unset($_SESSION['oauth2state']); unset($_SESSION['provider']); exit('Invalid state'); } else { unset($_SESSION['provider']); //Try to get an access token (using the authorization code grant) $token = $provider->getAccessToken( 'authorization_code', [ 'code' => $_GET['code'] ] ); //Use this to interact with an API on the users behalf //Use this to get a new access token if the old one expires // echo 'Refresh Token: ', htmlspecialchars($token->getRefreshToken()); $tokenChars = $token->getToken(); $refreshToken = $token->getRefreshToken(); if (empty($tokenChars) || empty($refreshToken)) { exit('Failed to get access token'); } $configKeys['IMAP_OAUTH_ACCESS_TOKEN'] = $tokenChars; $configKeys['IMAP_OAUTH_REFRESH_TOKEN'] = $refreshToken; $keyErrors = []; foreach ($configKeys as $key => $value) { if (!setConfigValue($conn, $key, $value)) { $keyErrors[] = $key; } } ?>
" . TRANS('ERROR_ON_SAVE_CONFIG') . "
" . implode("
", $keyErrors); return; exit(); } echo "

"; echo message('success', 'Yeahh!', TRANS('SUCCESS_ON_RETRIEVE_TOKEN'), '', '', true); ?>