return base64url_encode(openssl_random_pseudo_bytes(18)); } // Generate a redirect URI corresponding to the given route. // Note that OAuth 2.0 only allows HTTPS URLs in general, but Dropbox // allows HTTP URLs for localhost/127.0.0.1 endpoints only. function generate_redirect_uri($route_name) { $app = \Slim\Slim::getInstance(); $env = $app->environment(); return $env['slim.url_scheme'] . '://' . $_SERVER['HTTP_HOST'] . $app->urlFor($route_name); } // Main endpoint for the app. This page just starts the OAuth flow by // redirecting the user to Dropbox to sign in (if necessary) and allow // the app's request for access. $app->get('/', function () use($app) { $csrfToken = generateCSRFToken(); $_SESSION['csrfToken'] = $csrfToken; // Redirect to the OAuth authorize endpoint, using the authorization // code flow. $app->redirect('https://www.dropbox.com/1/oauth2/authorize?' . http_build_query(array('response_type' => 'code', 'client_id' => $GLOBALS['APP_KEY'], 'redirect_uri' => generate_redirect_uri('callback'), 'state' => $csrfToken))); }); // OAuth callback URL, which the user is redirected to by Dropbox after // allowing access to the app. The query parameters will include an // access code, which is then exchanged for an access token. The access // token is what's used to make calls to the Dropbox API. $app->get('/callback', function () use($app, $env) { $params = array(); parse_str($env['QUERY_STRING'], $params); // If there's an error, display it. if (isset($params['error'])) { echo 'Received an "' . $params['error'] . '" error with the message "' . $params['error_description'] . '"';
/** * @return string */ public function csrfToken() : string { return generateCSRFToken(); }