Skip to content

Commit

Permalink
Export Handler.UserClaims, so this provider can be swapped with custo…
Browse files Browse the repository at this point in the history
…m one.
  • Loading branch information
Dragomir-Ivanov committed Nov 25, 2019
1 parent 2b77f84 commit 9c29985
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
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)
// 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

0 comments on commit 9c29985

Please sign in to comment.