<?php require_once 'functions.php'; require_once 'jwt_functions.php'; $config = loadConfiguration(); if (empty($config['api_root'])) { displaySetup(); } if (!empty($_REQUEST['action']) && $_REQUEST['action'] == 'obtain' && !empty($_REQUEST['private_key'])) { $assertion = generateJwtAssertion($_REQUEST['private_key'], $config['api_key'], !empty($_REQUEST['user_id']) ? intval($_REQUEST['user_id']) : 0, $config['api_root']); $url = sprintf('%s/index.php?oauth/token', $config['api_root']); $json = makeCurlPost($url, array('grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer', 'assertion' => $assertion)); $message = renderMessageForJson('obtain', $json); if (!empty($json['access_token'])) { $accessToken = $json['access_token']; } } ?> <?php require 'html/header.php'; ?> <h3>JWT Bearer Grant Type</h3> <?php if (!empty($message)) { ?> <div class="message"><?php echo $message; ?>
if (empty($_REQUEST['topic'])) { $message = 'Subscription request must have `topic` parameter!'; break; } $topic = $_REQUEST['topic']; if (empty($_REQUEST['fwd'])) { $message = 'Subscription request must have `fwd` parameter!'; break; } $fwd = $_REQUEST['fwd']; if ($action == 'subscribe') { $json = makeSubscriptionRequest($config, $topic, $fwd, $accessToken); } else { $json = makeSubscriptionRequest($config, $topic, $fwd); } $message = renderMessageForJson($action, $json); break; case 'authorize': default: // step 1 $authorizeUrl = sprintf('%s/index.php?oauth/authorize&response_type=code&client_id=%s&scope=%s&redirect_uri=%s', $config['api_root'], rawurlencode($config['api_key']), rawurlencode($config['api_scope']), rawurlencode(getCallbackUrl())); $message = sprintf('<h3>Authorization (step 1)</h3>' . '<a href="%s">Click here</a> to go to %s and start the authorizing flow.' . ' Or <a href="jwt.php">click here</a> and try the JWT Bearer grant type.', $authorizeUrl, parse_url($authorizeUrl, PHP_URL_HOST)); break; } ?> <?php require 'html/header.php'; ?> <?php
function renderAccessTokenMessage($tokenUrl, array $json) { global $config, $accessToken; if (!empty($json['access_token'])) { $accessToken = $json['access_token']; $message = sprintf('Obtained access token successfully!<br />' . 'Scopes: %s<br />' . 'Expires At: %s<br />', $json['scope'], date('c', time() + $json['expires_in'])); if (!empty($json['refresh_token'])) { $message .= sprintf('Refresh Token: <a href="index.php?action=refresh&refresh_token=%1$s">%1$s</a><br />', $json['refresh_token']); } else { $message .= sprintf('Refresh Token: N/A<br />'); } list($body, $json) = makeRequest('index', $config['api_root'], $accessToken); if (!empty($json['links'])) { $message .= '<hr />' . renderMessageForJson('index', $json); } } else { $message = renderMessageForJson($tokenUrl, $json); } return $message; }