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

Minor restructure for greater confidence that only authenticated requests are proxied #180

Merged
merged 3 commits into from
Jun 15, 2019

Conversation

aeijdenberg
Copy link
Contributor

Description

We've been reviewing parts of the code base to evaluate usage of this app (and we like what we see so far :) ). This PR is a minor tidy up that I think makes crucial parts easier to reason about.

It splits the Authenticate() method which previously returned an HTTP status code into 2 methods, one to handle session related work and which returns a more standard Go error code, and a second to add headers to requests.

It then changes both invocations such that only a nil error code regards the session as authenticated, whereas the old code didn't explicitly check for success, rather it checked for known errors and then assumed in the absence of an expected error code, it would imply success.

Motivation and Context

I found the old code a little difficult to reason about in terms of what side-effects Authenticate() had, and it was more work for someone reading the code to need to follow everything the method did in order for the caller to be satisfied that they were handlng it correctly.

For example, with the code as is, if in the future someone modified the Authenticate() method to return a 400 bad request if the JSON cookie wouldn't unmarshal, which on it's own would be totally reasonable, the request would then be proxied through to the upstream server as the current code would fall through to fail open if an unexpected error was returned.

How Has This Been Tested?

It hasn't. The code changes are pretty small, and it is assumed the unit tests and code review will pick up any issues.

Checklist:

  • My change requires a change to the documentation or CHANGELOG.
  • I have updated the documentation/CHANGELOG accordingly.
  • I have created a feature (non-master) branch for my PR.

…icity allowed, rather

than as implicit dangling else
@aeijdenberg aeijdenberg requested a review from a team June 7, 2019 04:05
Copy link
Member

@JoelSpeed JoelSpeed left a comment

Choose a reason for hiding this comment

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

Thanks for working on this, I think this makes sense and improved the code. Added on minor nit below about using a switch that seems a little redundant, other than that, great!

Could you please update the Changelog to add a note about this PR

oauthproxy.go Outdated
status := p.Authenticate(rw, req)
if status == http.StatusAccepted {
session, err := p.getAuthenticatedSession(rw, req)
switch err {
Copy link
Member

Choose a reason for hiding this comment

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

Is there any reason for using a switch here? Would a typical if err != nil block not suffice? (I say != as I would personally reorder the statements so that the happy path is the least indented)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed - I think my first pass handled one type of error differently. Now though with only a non-nil check, the switch makes no sense. Have fixed as suggested. Thanks!

Copy link
Member

@JoelSpeed JoelSpeed left a comment

Choose a reason for hiding this comment

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

Looks good! Thanks for helping to clean up the code, we need a lot more of this 😅

@JoelSpeed JoelSpeed merged commit 0d6fa62 into oauth2-proxy:master Jun 15, 2019
Jing-ze pushed a commit to Jing-ze/oauth2-proxy that referenced this pull request Nov 19, 2024
Signed-off-by: bitliu <bitliu@tencent.com>
T-vK pushed a commit to T-vK/oauth2-proxy that referenced this pull request Nov 20, 2024
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.

2 participants