/** * Authentication function * Validates and parses incoming token, and returns its payload * * To be used as a dispatcher authentication, like this: * * // Authenticate token in every request * Server::resource("*", [ * "any" => [ * "authentication" => "Phidias\Oauth\Controller::authenticate({request})" * ] * ]); * */ public static function authenticate($request) { if ($request->hasHeader("authorization")) { list($authorizationMethod, $authorizationCredentials) = explode(" ", $request->getHeader("authorization")[0], 2); switch (strtolower($authorizationMethod)) { case "bearer": Token::load(trim($authorizationCredentials)); return Token::getPayload(); break; } } }