Skip to content

Make unathenticated callers the exception - #649

Open
augustuswm wants to merge 6 commits into
mainfrom
opt-in-unathn
Open

augustuswm wants to merge 6 commits into
mainfrom
opt-in-unathn

Conversation

@augustuswm

Copy link
Copy Markdown
Collaborator

Currently by default anyone that calls get_caller gets back either an authenticated or unauthenticated caller. In either case they only carry the permissions assigned to them. This means that a downstream application has to do their own work to check if the caller is authenticated if they want to have a 401 gate. This is a breaking change that changes that behavior. Instead we now emit a 401 by default if get_caller fails to extract an authenticated caller. For the case where someone wants to accept an unauthenticated caller they can explicitly request if via:

ctx.get_caller(rqctx).await.allow_unauthenticated()?

Anyone that does not need unauthenticated callers does not need to make any changes.

@notpeter notpeter left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good.

header
.to_str()
.ok()
.and_then(|value| value.strip_prefix("Bearer "))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: this should technically be case-insensitive. link

Comment thread v-api/src/context/mod.rs
}

pub fn builtin_unauthenticated_caller(&self) -> Caller<T> {
fn builtin_unauthenticated_caller(&self) -> Caller<T> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Was it intentional to make this no longer pub? (rfd-api uses it)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I was intending to yes, but let me see how rfd-api is using it. I can revert this if the needed.

Comment thread v-api/src/context/mod.rs Outdated
}

pub trait UnauthenticatedCaller<T> {
/// Call to allow an unathenticated caller to be returned.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

spelling: s/unathenticated/unauthenticated/

Comment thread v-api/src/context/mod.rs Outdated
VContextCallerError::Auth(inner) => inner.into(),
VContextCallerError::Caller(inner) => inner.into(),
VContextCallerError::Unauthenticated(_) => {
client_error(ClientErrorStatusCode::UNAUTHORIZED, "Unauthenticated")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: Could use unauthorized() helper.

Comment thread v-api/src/authn/mod.rs Outdated
{
// Ensure there is a bearer, without it there is nothing else to do
let bearer = BearerAuth::from_request(rqctx).await.map_err(|err| {
tracing::info!(?err, "Failed to extract bearer auth");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: Remove this or change to debug otherwise it'll generate duplicate info loglines for invalid bearer tokens. (bearer.rs:38)

Comment thread v-api/src/authn/jwt.rs Outdated
{
// Ensure there is a bearer, without it there is nothing else to do
let bearer = BearerAuth::from_request(rqctx).await.map_err(|err| {
tracing::info!(?err, "Failed to extract bearer auth");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: Doubled log line. Drop to debug or remove. (bearer.rs:38)

/// Extracting a bearer token should never fail, it should always return `Ok(BearerAuth(Some(token))))`
/// or `Ok(BearerAuth(None))`. `None` will be returned in any of the cases that a valid string can not
/// be extracted. This extractor is not responsible for checking the value of the token.
fn bearer_from_header(header: Option<&HeaderValue>) -> Result<BearerAuth, HttpError> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: this may just be personal preference, but considering this function can only be expected to work when the arg is header::AUTHORIZATION I'd just have it take request headers as the param instead. Taking an Option as a param when None can't succeed seems weird to me.

fn bearer_from_headers(headers: &HeaderMap<HeaderValue>) -> Result<BearerAuth, HttpError> {

But it is a private fn and maybe that makes the tests a little more annoying. 🤷

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I think making it take a map makes sense. I really dislike the option argument as well (it doesn't make sense as you note). I think it is just one of those frustrations of not being able to create a synthetic request context in tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants