Skip to content

Commit 59ea087

Browse files
committed
Added "--auth.user" option to auth commands
1 parent 0c1acb8 commit 59ea087

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

auth.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ var (
5050
}
5151
authOptions struct {
5252
jwtSecretFile string
53+
user string
5354
}
5455
)
5556

@@ -60,6 +61,7 @@ func init() {
6061

6162
pf := cmdAuth.PersistentFlags()
6263
pf.StringVar(&authOptions.jwtSecretFile, "auth.jwt-secret", "", "name of a plain text file containing a JWT secret used for server authentication")
64+
pf.StringVar(&authOptions.user, "auth.user", "", "name of a user to authenticate as. If empty, 'super-user' authentication is used")
6365
}
6466

6567
// mustAuthCreateJWTToken creates a the JWT token based on authentication options.
@@ -75,7 +77,7 @@ func mustAuthCreateJWTToken() string {
7577
log.Fatal().Err(err).Msgf("Failed to read JWT secret file '%s'", authOptions.jwtSecretFile)
7678
}
7779
jwtSecret := strings.TrimSpace(string(content))
78-
token, err := service.CreateJwtToken(jwtSecret)
80+
token, err := service.CreateJwtToken(jwtSecret, authOptions.user)
7981
if err != nil {
8082
log.Fatal().Err(err).Msg("Failed to create JWT token")
8183
}

service/authentication.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,20 @@ const (
3535

3636
// CreateJwtToken calculates a JWT authorization token based on the given secret.
3737
// If the secret is empty, an empty token is returned.
38-
func CreateJwtToken(jwtSecret string) (string, error) {
38+
func CreateJwtToken(jwtSecret, user string) (string, error) {
3939
if jwtSecret == "" {
4040
return "", nil
4141
}
4242
// Create a new token object, specifying signing method and the claims
4343
// you would like it to contain.
44-
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
44+
claims := jwt.MapClaims{
4545
"iss": "arangodb",
4646
"server_id": "foo",
47-
})
47+
}
48+
if user != "" {
49+
claims["preferred_username"] = user
50+
}
51+
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
4852

4953
// Sign and get the complete encoded token as a string using the secret
5054
signedToken, err := token.SignedString([]byte(jwtSecret))
@@ -62,7 +66,7 @@ func addJwtHeader(req *http.Request, jwtSecret string) error {
6266
if jwtSecret == "" {
6367
return nil
6468
}
65-
signedToken, err := CreateJwtToken(jwtSecret)
69+
signedToken, err := CreateJwtToken(jwtSecret, "")
6670
if err != nil {
6771
return maskAny(err)
6872
}

0 commit comments

Comments
 (0)