Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic auth support for images #356

Merged
merged 4 commits into from
Jul 7, 2024
Merged

Conversation

cyb3rko
Copy link
Contributor

@cyb3rko cyb3rko commented Jun 18, 2024

Closes #111

I've used an authenticator instead of an interceptor.
The difference is that an interceptor is used on every single call which is not needed in our case.
The authenticator only is called when a request throws a 401 so we try again with basic auth.


Here's my caddy config to test it (username: gotify, password: test)

(require_basic_auth) {
    # in caddy v2.8.0 and later it's called 'basic_auth'
    basicauth {
        gotify $2a$14$TSKxQgAh8n2GRe5ehrn/Ju0nWZH9tFXnmdyRF2TC6wbeMuHezx.oa       
    }
}

my.domain:1234 {
    import require_basic_auth
    # wherever you've stored your images
    reverse_proxy localhost:8080
    tls internal
}

// If there's no username, skip the authentication
if (request.url.username.isNotEmpty()) {
val basicAuthString = "${request.url.username}:${request.url.password}@"
val url = request.url.toString().replace(basicAuthString, "")
Copy link
Member

Choose a reason for hiding this comment

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

Does this replace have a use, for me it works with and without? I've improved the placeholder handling and added logging for failing requests. Could you have a look at it?

Copy link
Member

Choose a reason for hiding this comment

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

@cyb3rko should I merge this without your review?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, there's a lot going on at the moment.
Hopefully I can find some time today to take a look.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Your changes work on my side.
I could have sworn I found edge cases where it wouldn't work without stripping the URL, but I can't reproduce them at the moment. The only thing that could be interesting is what happens if the response is still 401 even after applying basic auth. Does it always start the interceptor again and again and run into request loops?
With stripping the URL we can not have that problem at all because on the next try it does not have a request.url.username anymore.

Nevertheless, I'm fine with merging.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for looking over it. It seems this behavior is required when using okhttp3.Authenticator

Reactive Authentication

Implementations authenticate by returning a follow-up request that includes an authorization
header, or they may decline the challenge by returning null. In this case the unauthenticated
response will be returned to the caller that triggered it.

Implementations should check if the initial request already included an attempt to
authenticate. If so it is likely that further attempts will not be useful and the authenticator
should give up.

but the Interceptor doesn't do new requests, so I'll remove it from here.

@jmattheis jmattheis mentioned this pull request Jul 5, 2024
Copy link
Member

@jmattheis jmattheis left a comment

Choose a reason for hiding this comment

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

:shipit:

@jmattheis jmattheis merged commit e8b88ab into gotify:master Jul 7, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Username and password in image url
2 participants