Skip to content

Commit 62bdea1

Browse files
fcellerajanikow
authored andcommitted
added user, exp, and allowed_paths
1 parent 9362100 commit 62bdea1

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

auth.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ var (
5151
authOptions struct {
5252
jwtSecretFile string
5353
user string
54+
paths []string
55+
exp int64
5456
}
5557
)
5658

@@ -62,6 +64,8 @@ func init() {
6264
pf := cmdAuth.PersistentFlags()
6365
pf.StringVar(&authOptions.jwtSecretFile, "auth.jwt-secret", "", "name of a plain text file containing a JWT secret used for server authentication")
6466
pf.StringVar(&authOptions.user, "auth.user", "", "name of a user to authenticate as. If empty, 'super-user' authentication is used")
67+
pf.StringSliceVar(&authOptions.paths, "auth.paths", nil, "a list of allowed pathes. The path must not include the '_db/DBNAME' prefix.")
68+
pf.Int64Var(&authOptions.exp, "auth.exp", 0, "an expiry date in seconds since epoche")
6569
}
6670

6771
// mustAuthCreateJWTToken creates a the JWT token based on authentication options.
@@ -77,7 +81,7 @@ func mustAuthCreateJWTToken() string {
7781
log.Fatal().Err(err).Msgf("Failed to read JWT secret file '%s'", authOptions.jwtSecretFile)
7882
}
7983
jwtSecret := strings.TrimSpace(string(content))
80-
token, err := service.CreateJwtToken(jwtSecret, authOptions.user)
84+
token, err := service.CreateJwtToken(jwtSecret, authOptions.user, "", authOptions.paths, authOptions.exp)
8185
if err != nil {
8286
log.Fatal().Err(err).Msg("Failed to create JWT token")
8387
}

service/authentication.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,29 @@ 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, user string) (string, error) {
38+
func CreateJwtToken(jwtSecret, user string, serverId string, paths []string, exp int64) (string, error) {
3939
if jwtSecret == "" {
4040
return "", nil
4141
}
42+
if serverId == "" {
43+
serverId = "foo"
44+
}
45+
4246
// Create a new token object, specifying signing method and the claims
4347
// you would like it to contain.
4448
claims := jwt.MapClaims{
4549
"iss": "arangodb",
46-
"server_id": "foo",
50+
"server_id": serverId,
4751
}
4852
if user != "" {
4953
claims["preferred_username"] = user
5054
}
55+
if paths != nil {
56+
claims["allowed_paths"] = paths
57+
}
58+
if exp > 0 {
59+
claims["exp"] = exp
60+
}
5161
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
5262

5363
// Sign and get the complete encoded token as a string using the secret
@@ -66,7 +76,7 @@ func addJwtHeader(req *http.Request, jwtSecret string) error {
6676
if jwtSecret == "" {
6777
return nil
6878
}
69-
signedToken, err := CreateJwtToken(jwtSecret, "")
79+
signedToken, err := CreateJwtToken(jwtSecret, "", "", nil, 0)
7080
if err != nil {
7181
return maskAny(err)
7282
}

0 commit comments

Comments
 (0)