-
Notifications
You must be signed in to change notification settings - Fork 45
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
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
bf44206
Update token endpoint for better error handling
DTCurrie f0d0568
lint
DTCurrie e17c818
Merge branch 'main' of github.com:viamrobotics/goutils into better-to…
DTCurrie 3acd28c
Fix cookie checks
DTCurrie b7d8794
Update /token and add tests
DTCurrie 85ea279
Merge branch 'main' of github.com:viamrobotics/goutils into better-to…
DTCurrie 90218f0
test cleanup
DTCurrie 12ce521
nil test cases
DTCurrie 4130122
lint
DTCurrie 37f380e
nolint gosec, may need more
DTCurrie 7ab3b90
I hate go linting, adding comments and stuff
DTCurrie f0f022b
Merge branch 'main' of github.com:viamrobotics/goutils into better-to…
DTCurrie 4b55c7e
lint
DTCurrie 28f781c
Merge branch 'main' of github.com:viamrobotics/goutils into better-to…
DTCurrie 4409995
Merge branch 'main' of github.com:viamrobotics/goutils into better-to…
DTCurrie 23e38da
combine get and clear auth cookie functions
DTCurrie 229a77d
lint
DTCurrie 0b4a3ac
cleanup
DTCurrie 577c1fa
add missing return
DTCurrie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package web | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"go.viam.com/test" | ||
) | ||
|
||
func createRequest(t *testing.T) (http.ResponseWriter, *http.Request) { | ||
w := httptest.NewRecorder() | ||
r, err := http.NewRequest(http.MethodGet, "http://localhost/", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
return nil, nil | ||
} | ||
|
||
return w, r | ||
} | ||
|
||
func setCookie(r *http.Request, key, value string) { | ||
r.AddCookie(&http.Cookie{ | ||
Name: key, | ||
Value: value, | ||
Path: "/", | ||
MaxAge: 10000, | ||
Secure: true, | ||
SameSite: http.SameSiteLaxMode, | ||
HttpOnly: true, | ||
}) | ||
} | ||
|
||
func TestWebAuth(t *testing.T) { | ||
t.Run("should return nil when token cookie is not present", func(t *testing.T) { | ||
w, r := createRequest(t) | ||
setCookie(r, ViamRefreshCookie, "") | ||
setCookie(r, ViamExpiryCookie, "123456") | ||
|
||
data := getAndClearAuthCookieValues(w, r) | ||
test.That(t, data, test.ShouldBeNil) | ||
}) | ||
|
||
t.Run("should return nil when token cookie is empty", func(t *testing.T) { | ||
w, r := createRequest(t) | ||
setCookie(r, ViamTokenCookie, "") | ||
setCookie(r, ViamRefreshCookie, "") | ||
setCookie(r, ViamExpiryCookie, "123456") | ||
|
||
data := getAndClearAuthCookieValues(w, r) | ||
test.That(t, data, test.ShouldBeNil) | ||
}) | ||
|
||
t.Run("should return nil when refresh cookies is not present", func(t *testing.T) { | ||
w, r := createRequest(t) | ||
setCookie(r, ViamTokenCookie, "abc123") | ||
setCookie(r, ViamExpiryCookie, "123456") | ||
|
||
data := getAndClearAuthCookieValues(w, r) | ||
test.That(t, data, test.ShouldBeNil) | ||
}) | ||
|
||
t.Run("should return nil when expiry cookie is not present", func(t *testing.T) { | ||
w, r := createRequest(t) | ||
setCookie(r, ViamTokenCookie, "abc123") | ||
setCookie(r, ViamRefreshCookie, "") | ||
|
||
data := getAndClearAuthCookieValues(w, r) | ||
test.That(t, data, test.ShouldBeNil) | ||
}) | ||
|
||
t.Run("should return nil when expiry cookie is empty", func(t *testing.T) { | ||
w, r := createRequest(t) | ||
setCookie(r, ViamTokenCookie, "abc123") | ||
setCookie(r, ViamRefreshCookie, "") | ||
setCookie(r, ViamExpiryCookie, "") | ||
|
||
data := getAndClearAuthCookieValues(w, r) | ||
test.That(t, data, test.ShouldBeNil) | ||
}) | ||
|
||
t.Run("should return token response data when cookies are set and clear the cookies", func(t *testing.T) { | ||
w, r := createRequest(t) | ||
setCookie(r, ViamTokenCookie, "abc123") | ||
setCookie(r, ViamRefreshCookie, "") | ||
setCookie(r, ViamExpiryCookie, "123456") | ||
|
||
data := getAndClearAuthCookieValues(w, r) | ||
test.That(t, data.AccessToken, test.ShouldEqual, "abc123") | ||
test.That(t, data.RefreshToken, test.ShouldEqual, "") | ||
test.That(t, data.Expiry, test.ShouldEqual, "123456") | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.