Skip to content

Commit 789cc3a

Browse files
committed
add support for custom claims and roles
Signed-off-by: Robert Landers <landers.robert@gmail.com>
1 parent a9751b3 commit 789cc3a

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

cli/auth/keys.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,20 @@ func ExtractUser(r *http.Request, config *config.Config) (user *User, ok bool) {
109109
// The token is signed using the active secret key from the config.
110110
// The token will expire in 72 hours and is valid starting from 5 minutes ago.
111111
// Returns the signed token string or an error if the signing process fails.
112-
func CreateUser(userId UserId, role []Role, config *config.Config) (string, error) {
113-
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
112+
func CreateUser(userId UserId, role []Role, claims map[string]string, config *config.Config) (string, error) {
113+
claimMap := jwt.MapClaims{
114114
"sub": userId,
115115
"exp": time.Now().Add(72 * time.Hour).Unix(),
116-
"iat": time.Now().Add(-5 * time.Minute).Unix(),
116+
"iat": time.Now(),
117+
"nbf": time.Now().Add(-5 * time.Minute).Unix(),
117118
"roles": role,
118-
})
119+
}
120+
121+
for k, v := range claims {
122+
claimMap[k] = v
123+
}
124+
125+
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claimMap)
119126

120127
key, err := getActiveKey(config)
121128
if err != nil {

cli/cli.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,8 @@ func main() {
529529
createUser := cli.NewCommand("create-user", "Create a new user").
530530
WithArg(cli.NewArg("id", "The user id to assign to the user").WithType(cli.TypeString)).
531531
WithOption(cli.NewOption("admin", "Create the user as an admin").WithType(cli.TypeBool)).
532+
WithOption(cli.NewOption("roles", "Create with the roles").WithType(cli.TypeString).WithChar('r')).
533+
WithOption(cli.NewOption("claims", "Create with the claims as key:value;key:value").WithType(cli.TypeString).WithChar('c')).
532534
WithAction(func(args []string, options map[string]string) int {
533535
cfg, err := config.GetProjectConfig()
534536
if err != nil {
@@ -540,7 +542,22 @@ func main() {
540542
rol = append(rol, "admin")
541543
}
542544

543-
user, err := auth.CreateUser(auth.UserId(args[0]), rol, cfg)
545+
roles := strings.Split(options["roles"], ",")
546+
for _, role := range roles {
547+
rol = append(rol, auth.Role(role))
548+
}
549+
550+
claims := strings.Split(options["claims"], ";")
551+
extraClaims := make(map[string]string)
552+
for _, claim := range claims {
553+
kv := strings.Split(claim, ":")
554+
if len(kv) != 2 {
555+
panic(fmt.Errorf("invalid claim: %s", claim))
556+
}
557+
extraClaims[kv[0]] = kv[1]
558+
}
559+
560+
user, err := auth.CreateUser(auth.UserId(args[0]), rol, extraClaims, cfg)
544561
if err != nil {
545562
return 1
546563
}

0 commit comments

Comments
 (0)