-
Notifications
You must be signed in to change notification settings - Fork 5
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
Allow custom verifyAccess
implementations to inhect key/value pairs into the request context
#29
Conversation
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 { |
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.
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())) |
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.
h.logger(r.Context())
-> logger
?
middleware_test.go
Outdated
type mockJWTAuthMiddlewareNextHandler struct { | ||
ctx context.Context | ||
called int | ||
jwtClaims jwt.Claims |
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.
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.
middleware_test.go
Outdated
@@ -22,14 +23,21 @@ import ( | |||
"github.com/acronis/go-authkit/jwt" | |||
) | |||
|
|||
const ( | |||
errDomain = "TestDomain" |
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.
[minor] How about renaming errDomain
to testErrDomain
to be consistent with testBearerToken
?:)
2531d0b
to
281d63d
Compare
…ff, to allow injecting key/value pairs by user defined functions, such as verifyAccess.
281d63d
to
be2b2c6
Compare
In
jwtAuthHandler.ServeHTTP()
, no using a copy of the request context, but always accessing it viar.Context()
, to allow injecting key/value pairs by user defined functions, such asverifyAccess
.Also moved tests to their own package
authkit_test
to allow for the creation of test only utilities, such as functions and constants.