Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions login/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const contentTypeJWT = "application/jwt"
const contentTypeJSON = "application/json"
const contentTypePlain = "text/plain"

type userClaimsFunc func(userInfo model.UserInfo) (jwt.Claims, error)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This type should not be exported. It makes no sense to export this from my point of view; it is an implementation detail of loginsrv and not a public API which should be used from outside. If you want to provide custom user claims use the "user endpoint"-feature

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I am using loginsrv embedded in my application, and my custom provider is just another piece of code in that application. It doesn't make sense to do provision over HTTP in this case(opening localhost server, and then connecting to itself). Maybe if we export UserClaimsFunc "user endpoint" feature becomes one of the user claims providers available.
I do find mandatory provision over HTTP as unnecessary.

// UserClaimsFunc returns jwt.Claims
type UserClaimsFunc func(userInfo model.UserInfo) (jwt.Claims, error)

// Handler is the mail login handler.
// It serves the login ressource and does the authentication against the backends or oauth provider.
Expand All @@ -31,7 +32,7 @@ type Handler struct {
signingMethod jwt.SigningMethod
signingKey interface{}
signingVerifyKey interface{}
userClaims userClaimsFunc
UserClaims UserClaimsFunc
}

// NewHandler creates a login handler based on the supplied configuration.
Expand Down Expand Up @@ -70,7 +71,7 @@ func NewHandler(config *Config) (*Handler, error) {
backends: backends,
config: config,
oauth: oauth,
userClaims: userClaims.Claims,
UserClaims: userClaims.Claims,
}, nil
}

Expand Down Expand Up @@ -277,9 +278,9 @@ func (h *Handler) respondAuthenticatedHTML(w http.ResponseWriter, r *http.Reques

func (h *Handler) createToken(userInfo model.UserInfo) (string, error) {
var claims jwt.Claims = userInfo
if h.userClaims != nil {
if h.UserClaims != nil {
var err error
claims, err = h.userClaims(userInfo)
claims, err = h.UserClaims(userInfo)
if err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions login/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ func TestHandler_ReturnUserInfoJSON_CustomClaims(t *testing.T) {
h := testHandler()
input := model.UserInfo{Sub: "marvin", Expiry: time.Now().Add(time.Second).Unix()}
claims := customClaims{"sub": "marvin", "exp": json.Number(strconv.FormatInt(input.Expiry, 10)), "foo": "bar"}
h.userClaims = func(userInfo model.UserInfo) (jwt.Claims, error) {
h.UserClaims = func(userInfo model.UserInfo) (jwt.Claims, error) {
return claims, nil
}
token, err := h.createToken(input)
Expand Down Expand Up @@ -667,7 +667,7 @@ func TestHandler_getToken_InvalidNoToken(t *testing.T) {
func TestHandler_getToken_WithUserClaims(t *testing.T) {
h := testHandler()
input := model.UserInfo{Sub: "marvin", Expiry: time.Now().Add(time.Second).Unix()}
h.userClaims = func(userInfo model.UserInfo) (jwt.Claims, error) {
h.UserClaims = func(userInfo model.UserInfo) (jwt.Claims, error) {
return customClaims{"sub": "Zappod", "origin": "fake", "exp": userInfo.Expiry}, nil
}
token, err := h.createToken(input)
Expand Down