Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions libs/drogon/src/controllers/AuthorizationEndpointController.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ ::drogon::HttpResponsePtr buildAuthorizeErrorRedirect(
const std::string &redirectUri,
const std::string &error,
const std::string &description,
const std::string &state
const std::string &state,
const std::string &issuer = ""
)
{
// Each value belongs to the verified callback query, not a complete URI.
Expand All @@ -43,6 +44,8 @@ ::drogon::HttpResponsePtr buildAuthorizeErrorRedirect(
location += "&error_description=" + ::drogon::utils::urlEncodeComponent(description);
if (!state.empty())
location += "&state=" + ::drogon::utils::urlEncodeComponent(state);
if (!issuer.empty())
location += "&iss=" + ::drogon::utils::urlEncodeComponent(issuer);
return ::drogon::HttpResponse::newRedirectionResponse(location);
}
} // namespace
Expand Down Expand Up @@ -387,7 +390,8 @@ void AuthorizationEndpointController::authorize(
redirectUri,
"invalid_request",
"code_challenge_method 'plain' is not allowed; use S256",
state
state,
plugin->getIssuer()
)
);
return;
Expand All @@ -400,7 +404,8 @@ void AuthorizationEndpointController::authorize(
"invalid_request",
"code_challenge_method is required when code_challenge is "
"present (S256)",
state
state,
plugin->getIssuer()
)
);
return;
Expand All @@ -412,7 +417,8 @@ void AuthorizationEndpointController::authorize(
redirectUri,
"invalid_request",
"code_challenge_method must be 'S256'",
state
state,
plugin->getIssuer()
)
);
return;
Expand All @@ -431,7 +437,8 @@ void AuthorizationEndpointController::authorize(
redirectUri,
"invalid_request",
"code_challenge must be 43-128 characters of [A-Za-z0-9-._~]",
state
state,
plugin->getIssuer()
));
return;
}
Expand Down Expand Up @@ -499,7 +506,8 @@ void AuthorizationEndpointController::authorize(
redirectUri,
"login_required",
"prompt=none requested but no fully authenticated session",
state
state,
plugin->getIssuer()
));
return;
}
Expand Down Expand Up @@ -753,7 +761,7 @@ void AuthorizationEndpointController::authorize(
{
error = "access_denied";
}
callback(buildAuthorizeErrorRedirect(redirectUri, error, desc, state));
callback(buildAuthorizeErrorRedirect(redirectUri, error, desc, state, plugin->getIssuer()));
return;
}

Expand All @@ -773,7 +781,8 @@ void AuthorizationEndpointController::authorize(
redirectUri,
"consent_required",
"prompt=none requested but user interaction (consent) is required",
state
state,
plugin->getIssuer()
));
return;
}
Expand Down Expand Up @@ -891,7 +900,8 @@ void AuthorizationEndpointController::authorize(
redirectUri,
"invalid_request",
"PKCE (code_challenge) is required",
state
state,
plugin->getIssuer()
)
);
return;
Expand All @@ -908,9 +918,10 @@ void AuthorizationEndpointController::authorize(
codeChallenge,
codeChallengeMethod,
nonce,
[redirectUri, state, callback = std::move(callback)](
[plugin, redirectUri, state, callback = std::move(callback)](
bool success, std::string code, std::string error
) mutable {
const std::string issuer = plugin ? plugin->getIssuer() : "";
if (!success)
{
LOG_ERROR << "Failed to generate authorization code: " << error;
Expand All @@ -921,20 +932,23 @@ void AuthorizationEndpointController::authorize(
redirectUri,
"server_error",
"Failed to generate authorization code",
state
state,
issuer
)
);
return;
}

// F-020 (RFC 6749 §4.1.2/§4.1.3): urlEncode the
// code and state so special chars survive the query.
// F-020 (RFC 6749 §4.1.2/§4.1.3) + RFC 9207 section 2:
// urlEncode code, state, and iss.
std::string location =
redirectUri +
::fulla::drogon::utils::authorizeUrlQuerySeparator(redirectUri) +
"code=" + ::drogon::utils::urlEncodeComponent(code);
if (!state.empty())
location += "&state=" + ::drogon::utils::urlEncodeComponent(state);
if (!issuer.empty())
location += "&iss=" + ::drogon::utils::urlEncodeComponent(issuer);
auto resp = ::drogon::HttpResponse::newRedirectionResponse(location);
if (auto m = ::drogon::app().getPlugin<::OAuth2Plugin>()->getMetrics())
m->incrementCounter(
Expand Down
4 changes: 4 additions & 0 deletions libs/drogon/src/controllers/DiscoveryController.cc
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ void DiscoveryController::metadata(
// advertised.
metadata["code_challenge_methods_supported"] = Json::Value(Json::arrayValue);
metadata["code_challenge_methods_supported"].append("S256");
// RFC 9207 section 3: authorization responses carry the iss parameter.
metadata["authorization_response_iss_parameter_supported"] = true;

// Client authentication methods
metadata["token_endpoint_auth_methods_supported"] = Json::Value(Json::arrayValue);
Expand Down Expand Up @@ -369,6 +371,8 @@ void DiscoveryController::oidcDiscovery(
// P0-5 (#259): S256-only — plain is no longer accepted at authorize and
// is no longer advertised.
discovery["code_challenge_methods_supported"].append("S256");
// RFC 9207 section 3: authorization responses carry the iss parameter.
discovery["authorization_response_iss_parameter_supported"] = true;

// F-022 (OIDC Core §3.1.2.1 / §5.1): advertise prompt/max_age support and
// the auth_time/acr/amr claims the id_token now carries.
Expand Down
14 changes: 14 additions & 0 deletions libs/drogon/src/controllers/SessionController.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ void sendOAuthErrorRedirect(
location += "&error_description=" + ::drogon::utils::urlEncodeComponent(description);
if (!state.empty())
location += "&state=" + ::drogon::utils::urlEncodeComponent(state);
if (auto *p = ::drogon::app().getPlugin<::OAuth2Plugin>(); p && !p->getIssuer().empty())
location += "&iss=" + ::drogon::utils::urlEncodeComponent(p->getIssuer());
if (jsonMode)
{
Json::Value ret;
Expand Down Expand Up @@ -1087,6 +1089,9 @@ void SessionController::login(
"code=" + ::drogon::utils::urlEncodeComponent(code);
if (!state.empty())
location += "&state=" + ::drogon::utils::urlEncodeComponent(state);
if (auto *p = ::drogon::app().getPlugin<::OAuth2Plugin>();
p && !p->getIssuer().empty())
location += "&iss=" + ::drogon::utils::urlEncodeComponent(p->getIssuer());
if (req->getParameter("json") == "true")
{
Json::Value ret;
Expand Down Expand Up @@ -1186,6 +1191,9 @@ void SessionController::login(
"code=" + ::drogon::utils::urlEncodeComponent(code);
if (!state.empty())
location += "&state=" + ::drogon::utils::urlEncodeComponent(state);
if (auto *p = ::drogon::app().getPlugin<::OAuth2Plugin>();
p && !p->getIssuer().empty())
location += "&iss=" + ::drogon::utils::urlEncodeComponent(p->getIssuer());
if (req->getParameter("json") == "true")
{
Json::Value ret;
Expand Down Expand Up @@ -1524,6 +1532,9 @@ void SessionController::consent(
"error=access_denied&error_description=User+denied+consent";
if (!state.empty())
location += "&state=" + ::drogon::utils::urlEncodeComponent(state);
if (auto *p = ::drogon::app().getPlugin<::OAuth2Plugin>();
p && !p->getIssuer().empty())
location += "&iss=" + ::drogon::utils::urlEncodeComponent(p->getIssuer());
if (jsonMode)
{
Json::Value ret;
Expand Down Expand Up @@ -1728,6 +1739,9 @@ void SessionController::consent(
"code=" + ::drogon::utils::urlEncodeComponent(code);
if (!state.empty())
location += "&state=" + ::drogon::utils::urlEncodeComponent(state);
if (auto *p = ::drogon::app().getPlugin<::OAuth2Plugin>();
p && !p->getIssuer().empty())
location += "&iss=" + ::drogon::utils::urlEncodeComponent(p->getIssuer());
if (jsonMode)
{
// Same envelope as the /oauth2/login json=true path;
Expand Down
18 changes: 15 additions & 3 deletions libs/drogon/src/filters/OAuth2AuthFilter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,22 @@ void fulla::drogon::filters::OAuth2AuthFilter::doFilter(
return;
}

std::string token;
auto authHeader = req->getHeader("Authorization");
if (authHeader.empty() || authHeader.substr(0, 7) != "Bearer ")
if (!authHeader.empty() && authHeader.rfind("Bearer ", 0) == 0)
{
token = authHeader.substr(7);
}
else if (
req->method() == Post && req->path() == "/oauth2/userinfo" &&
req->contentType() == CT_APPLICATION_X_FORM
)
{
// OIDC Core 5.3.1 + RFC 6750 2.2: allow form-encoded body
// access_token on POST /oauth2/userinfo (never URL query params).
token = req->getParameter("access_token");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Read the form bearer token exclusively from the request body

For a POST request with a form content type, getParameter("access_token") also reads URL query parameters; the same merged-parameter behavior is explicitly documented in libs/drogon/src/services/SocialLoginGuard.cc in parseBody(). Consequently, this branch accepts /oauth2/userinfo?access_token=... even when the form body contains no token, contrary to its stated body-only restriction. Parse the body separately and add a POST-with-query-token rejection test. The built-in userinfo route is currently GET-only, so this affects the new filter branch when invoked directly or mounted on a POST route.

Useful? React with 👍 / 👎.

}
if (token.empty())
{
LOG_WARN << "OAuth2AuthFilter: Missing or invalid Authorization header";
auto error = fulla::common::error::Error::fromCode(
Expand All @@ -52,8 +66,6 @@ void fulla::drogon::filters::OAuth2AuthFilter::doFilter(
return;
}

std::string token = authHeader.substr(7);

// Async Token Validation
plugin->validateAccessToken(
token,
Expand Down
Loading