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

Update token endpoint for better error handling #364

Merged
merged 19 commits into from
Nov 12, 2024
Merged

Conversation

DTCurrie
Copy link
Member

@DTCurrie DTCurrie commented Oct 3, 2024

Update the token endpoint to read the existing bearer token (if it exists) and better handle situations a user may run into.

  • User attempts to get a token with no existing bearer token and no token cookies present:
    • Return a 400
  • User attempts to get a token with an existing bearer token and no token cookies present:
    • If the bearer token is valid, do nothing and return a 204 (the user is in an active session)
    • If the bearer token is invalid, return a 401 (the user is in an inactive session)

Otherwise, we will continue with the existing behavior.

Left comments in to explain the flow, can delete/simplify them before merging.

@DTCurrie DTCurrie requested review from mcous and jr22 October 3, 2024 17:10
@DTCurrie DTCurrie self-assigned this Oct 3, 2024
@viambot viambot added the safe to test Mark as safe to test label Oct 3, 2024
@viambot viambot added safe to test Mark as safe to test and removed safe to test Mark as safe to test labels Oct 3, 2024
@viambot viambot added safe to test Mark as safe to test and removed safe to test Mark as safe to test labels Oct 3, 2024
Copy link
Member

@mcous mcous left a comment

Choose a reason for hiding this comment

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

LGTM

@viambot viambot added safe to test Mark as safe to test and removed safe to test Mark as safe to test labels Oct 4, 2024
web/auth.go Outdated Show resolved Hide resolved
@viambot viambot added safe to test Mark as safe to test and removed safe to test Mark as safe to test labels Oct 4, 2024
web/auth.go Outdated
// - return an unauthenticated error 401
// - force user logout
if failedToGetCookies && current != "" {
isValid := h.state.sessions.HasSessionWithAccessToken(ctx, current)
Copy link
Member Author

Choose a reason for hiding this comment

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

@jr22 Ok so when testing, I noticed I could logout, and use my old token from the logged out session to get myself a new token and start an active session. Just want to check, I would assume if the value of current is a token that was logged out, then this would return false, but it appears that may not be happening given my testing on @mcous 's PR. Is my assumption correct?

Copy link
Member

@jr22 jr22 Oct 8, 2024

Choose a reason for hiding this comment

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

hm yeah i wouldnt think that should work. let me know if you want to debug together.

Copy link
Member

Choose a reason for hiding this comment

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

did you fix this/ figure out what was going on?

@viambot viambot added safe to test Mark as safe to test and removed safe to test Mark as safe to test labels Oct 9, 2024
@DTCurrie DTCurrie requested review from jr22 and mcous October 9, 2024 17:33
@viambot viambot removed the safe to test Mark as safe to test label Oct 9, 2024
@DTCurrie
Copy link
Member Author

@jr22 I have been fighting this linter for way too long. For some reason, I can't run make lint on my local. Seeing these warnings before the process terminates itself:

WARN [linters_context] running gomodguard failed: unable to parse module file go.mod: go.mod:3: invalid go version '1.21.13': must match format 1.23: if you are not using go modules it is suggested to disable this linter 
WARN [linters_context] running gomoddirectives failed: failed to get module file: go.mod:3: invalid go version '1.21.13': must match format 1.23: if you are not using go modules it is suggested to disable this linter 

It seems to run fine in CI. I just wiped my local repo, re-cloned, and tried again, and I am seeing the same issues. There are linting issues, but I can't parse what the problem is.

@jr22
Copy link
Member

jr22 commented Oct 10, 2024

@jr22 I have been fighting this linter for way too long. For some reason, I can't run make lint on my local. Seeing these warnings before the process terminates itself:

WARN [linters_context] running gomodguard failed: unable to parse module file go.mod: go.mod:3: invalid go version '1.21.13': must match format 1.23: if you are not using go modules it is suggested to disable this linter 
WARN [linters_context] running gomoddirectives failed: failed to get module file: go.mod:3: invalid go version '1.21.13': must match format 1.23: if you are not using go modules it is suggested to disable this linter 

It seems to run fine in CI. I just wiped my local repo, re-cloned, and tried again, and I am seeing the same issues. There are linting issues, but I can't parse what the problem is.

might be because the go.mod for this repo is 1.21.13 but your local version is 1.23. you might have to downgrade the version of go youre running to get this to work (app is on go 1.23 so would make sense that locally youre running tht)

@DTCurrie
Copy link
Member Author

@jr22 I have been fighting this linter for way too long. For some reason, I can't run make lint on my local. Seeing these warnings before the process terminates itself:

WARN [linters_context] running gomodguard failed: unable to parse module file go.mod: go.mod:3: invalid go version '1.21.13': must match format 1.23: if you are not using go modules it is suggested to disable this linter 
WARN [linters_context] running gomoddirectives failed: failed to get module file: go.mod:3: invalid go version '1.21.13': must match format 1.23: if you are not using go modules it is suggested to disable this linter 

It seems to run fine in CI. I just wiped my local repo, re-cloned, and tried again, and I am seeing the same issues. There are linting issues, but I can't parse what the problem is.

might be because the go.mod for this repo is 1.21.13 but your local version is 1.23. you might have to downgrade the version of go youre running to get this to work (app is on go 1.23 so would make sense that locally youre running tht)

Ah that makes sense, thanks.

@viambot viambot added safe to test Mark as safe to test and removed safe to test Mark as safe to test labels Oct 15, 2024
@DTCurrie
Copy link
Member Author

@mcous updated the code per our last conversation, should we link it on your PR so we can test it out?

@viambot viambot added safe to test Mark as safe to test and removed safe to test Mark as safe to test labels Nov 8, 2024
@DTCurrie
Copy link
Member Author

@jr22 @mcous Got tests passing on the app PR: https://github.com/viamrobotics/app/pull/6392

I think we are good to final review and merge this, cut a release, and then merge that app-side PR.

@viambot viambot added safe to test Mark as safe to test and removed safe to test Mark as safe to test labels Nov 11, 2024
Copy link
Member

@mcous mcous left a comment

Choose a reason for hiding this comment

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

web/auth.go Outdated Show resolved Hide resolved
@viambot viambot added safe to test Mark as safe to test and removed safe to test Mark as safe to test labels Nov 12, 2024
@viambot viambot added safe to test Mark as safe to test and removed safe to test Mark as safe to test labels Nov 12, 2024
Copy link
Member

@jr22 jr22 left a comment

Choose a reason for hiding this comment

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

2 small comments (not blocking) otherwise looks good

web/auth.go Outdated Show resolved Hide resolved
defer span.End()

current := getBearerToken(r)
data := getAndClearAuthCookieValues(w, r)
Copy link
Member

Choose a reason for hiding this comment

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

note that theres a small change in behavior here where we will still clear cookies even if the json marshling fails. i think this is fine but calling it out

Copy link
Member Author

Choose a reason for hiding this comment

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

Good callout. I agree it's fine, if the marshalling fails it probably means something was broken/corrupted in the auth flow and it is probably best to start again with a clean state.

@viambot viambot added safe to test Mark as safe to test and removed safe to test Mark as safe to test labels Nov 12, 2024
@viambot viambot added safe to test Mark as safe to test and removed safe to test Mark as safe to test labels Nov 12, 2024
@DTCurrie DTCurrie merged commit 842479f into main Nov 12, 2024
8 checks passed
@DTCurrie DTCurrie deleted the better-token-handling branch November 12, 2024 19:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
safe to test Mark as safe to test
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants