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

Allow custom verifyAccess implementations to inhect key/value pairs into the request context #29

Merged
merged 1 commit into from
Dec 20, 2024

Conversation

ilmaruk
Copy link

@ilmaruk ilmaruk commented Dec 19, 2024

In jwtAuthHandler.ServeHTTP(), no using a copy of the request context, but always accessing it via r.Context(), to allow injecting key/value pairs by user defined functions, such as verifyAccess.

Also moved tests to their own package authkit_test to allow for the creation of test only utilities, such as functions and constants.

middleware.go Outdated
reqCtx = NewContextWithBearerToken(reqCtx, bearerToken)
reqCtx = NewContextWithJWTClaims(reqCtx, jwtClaims)
h.next.ServeHTTP(rw, r.WithContext(reqCtx))
h.next.ServeHTTP(rw, r)
}

func (h *jwtAuthHandler) logger(ctx context.Context) log.FieldLogger {
Copy link
Member

Choose a reason for hiding this comment

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

Suggest removing logger() func. It looks redundant after your refactoring.

middleware.go Outdated

bearerToken := GetBearerTokenFromRequest(r)
if bearerToken == "" {
apiErr := restapi.NewError(h.errorDomain, ErrCodeBearerTokenMissing, ErrMessageBearerTokenMissing)
restapi.RespondError(rw, http.StatusUnauthorized, apiErr, h.logger(reqCtx))
restapi.RespondError(rw, http.StatusUnauthorized, apiErr, h.logger(r.Context()))
Copy link
Member

Choose a reason for hiding this comment

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

h.logger(r.Context()) -> logger?

type mockJWTAuthMiddlewareNextHandler struct {
ctx context.Context
called int
jwtClaims jwt.Claims
Copy link
Member

Choose a reason for hiding this comment

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

There’s no need to have jwtClaims now since it can be derived from the ctx.

I suggest replacing the ctx field with a lastRequest field for the following reasons:
a) lastRequest is clearer and more intuitive to understand in this context.
b) Storing ctx in a struct is generally considered a poor practice. It may confuse other developers.

@@ -22,14 +23,21 @@ import (
"github.com/acronis/go-authkit/jwt"
)

const (
errDomain = "TestDomain"
Copy link
Member

@vasayxtx vasayxtx Dec 19, 2024

Choose a reason for hiding this comment

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

[minor] How about renaming errDomain to testErrDomain to be consistent with testBearerToken?:)

@ilmaruk ilmaruk force-pushed the bugfix/VP-4039-migrations-wont-start branch from 2531d0b to 281d63d Compare December 20, 2024 07:44
…ff, to allow injecting key/value pairs by user defined functions, such as verifyAccess.
@ilmaruk ilmaruk force-pushed the bugfix/VP-4039-migrations-wont-start branch from 281d63d to be2b2c6 Compare December 20, 2024 07:56
@vasayxtx vasayxtx merged commit 138392d into acronis:main Dec 20, 2024
5 checks passed
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