ESI Refresh Token Generator

EOF; } /** * @return string */ function get_footer() { return <<<'EOF'
EOF; } // Page contents /** * Fresh, new login page. */ function new_login() { $action = $_SERVER['PHP_SELF'] . '?action=submitsecrets'; $callback = get_sso_callback_url(); echo get_header(); echo <<

Create a new Application on the EVE Online Developers Site. Use the resultant Client ID and Secret Key in the form below.

The callback url to use in the application form is:

$callback

ClientID From the EVE Online Developers Site
Secret From the EVE Online Developers Site
EOF; echo get_footer(); } /** * @param $url */ function print_sso_url($url) { echo get_header(); echo <<

Click the button below to login with your EVE Online account.

The generated URL is:

$url

EOF; echo get_footer(); } /** * @param $access_token * @param $refresh_token */ function print_tokens($access_token, $refresh_token) { $start_again_url = $_SERVER['PHP_SELF'] . '?action=new'; echo get_header(); echo <<

Your current access token is:

$access_token

Valid for ~20 minutes.

Your refresh token is:

$refresh_token

Valid until you delete the app from your account here.

Start Again EOF; echo get_footer(); } // Ensure we have an action! if (! isset($_GET['action'])) redirect_to_new(); // Worlds most caveman router! // Decide where to go based on the value of 'action' switch ($_GET['action']) { // Display the form to create a new login. case 'new': $_SESSION['test'] = 'bob'; new_login(); break; case 'submitsecrets': // Ensure we got some values if (! isset($_REQUEST['clientid']) || ! isset($_REQUEST['secret']) || ! isset($_REQUEST['scopes']) ) { echo 'All fields are mandatory!
' . PHP_EOL; echo 'Start again'; exit(); } $_SESSION['clientid'] = $_REQUEST['clientid']; $_SESSION['secret'] = $_REQUEST['secret']; $_SESSION['state'] = uniqid(); // Generate the url with the requested scopes $url = 'https://login.eveonline.com/v2/oauth/authorize/?response_type=code&redirect_uri=' . urlencode(get_sso_callback_url()) . '&client_id=' . $_SESSION['clientid'] . '&scope=' . implode(' ', $_REQUEST['scopes']) . ' &state=' . $_SESSION['state']; // Print the HTML with the login button. print_sso_url($url); break; case 'eveonlinecallback': // Verify the state. if ($_REQUEST['state'] != $_SESSION['state']) { echo 'Invalid State! You will have to start again!
'; echo 'Start again'; exit(); } // Clear the state value. $_SESSION['state'] = null; // Prep the authentication header. $headers = [ 'Authorization: Basic ' . base64_encode($_SESSION['clientid'] . ':' . $_SESSION['secret']), 'Content-Type: application/json', ]; // Seems like CCP does not mind JSON in the body. Yay. $fields = json_encode([ 'grant_type' => 'authorization_code', 'code' => $_REQUEST['code'], ]); // Start a cURL session $ch = curl_init('https://login.eveonline.com/v2/oauth/token'); curl_setopt_array($ch, [ CURLOPT_URL => 'https://login.eveonline.com/v2/oauth/token', CURLOPT_POST => true, CURLOPT_POSTFIELDS => $fields, CURLOPT_HTTPHEADER => $headers, CURLOPT_RETURNTRANSFER => true, CURLOPT_USERAGENT => 'eseye/tokengenerator', CURLOPT_SSL_VERIFYPEER => true, CURLOPT_SSL_CIPHER_LIST => 'TLSv1', ]); $result = curl_exec($ch); $data = json_decode($result); print_tokens($data->access_token, $data->refresh_token); break; // If we dont know what 'action' to perform, then redirect. default: redirect_to_new(); break; }