Skip to content

Commit 301bdd5

Browse files
committed
Add IAT and custome fields
1 parent af38d78 commit 301bdd5

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

auth.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ package main
2525
import (
2626
"fmt"
2727
"io/ioutil"
28+
"strconv"
2829
"strings"
2930
"time"
3031

@@ -56,6 +57,9 @@ var (
5657
paths []string
5758
exp string
5859
expDuration time.Duration
60+
61+
fieldsOverride []string
62+
fieldsOverrideMap map[string]interface{}
5963
}
6064
)
6165

@@ -69,6 +73,7 @@ func init() {
6973
pf.StringVar(&authOptions.user, "auth.user", "", "name of a user to authenticate as. If empty, 'super-user' authentication is used")
7074
pf.StringSliceVar(&authOptions.paths, "auth.paths", nil, "a list of allowed pathes. The path must not include the '_db/DBNAME' prefix.")
7175
pf.StringVar(&authOptions.exp, "auth.exp", "", "a time in which token should expire - based on current time in UTC. Supported units: h, m, s (default)")
76+
pf.StringSliceVar(&authOptions.fieldsOverride, "auth.fields", nil, "a list of additional fields set in the token. This flags override one auto-generated in token")
7277
}
7378

7479
// mustAuthCreateJWTToken creates a the JWT token based on authentication options.
@@ -84,7 +89,7 @@ func mustAuthCreateJWTToken() string {
8489
log.Fatal().Err(err).Msgf("Failed to read JWT secret file '%s'", authOptions.jwtSecretFile)
8590
}
8691
jwtSecret := strings.TrimSpace(string(content))
87-
token, err := service.CreateJwtToken(jwtSecret, authOptions.user, "", authOptions.paths, authOptions.expDuration)
92+
token, err := service.CreateJwtToken(jwtSecret, authOptions.user, "", authOptions.paths, authOptions.expDuration, authOptions.fieldsOverrideMap)
8893
if err != nil {
8994
log.Fatal().Err(err).Msg("Failed to create JWT token")
9095
}
@@ -119,6 +124,32 @@ func persistentAuthPreFunE(cmd *cobra.Command, args []string) error {
119124
authOptions.expDuration = d
120125
}
121126

127+
authOptions.fieldsOverrideMap = map[string]interface{}{}
128+
129+
for _, field := range authOptions.fieldsOverride {
130+
tokens := strings.Split(field, "=")
131+
if len(tokens) == 0 {
132+
return fmt.Errorf("invalid format of the field override: `%s`", field)
133+
}
134+
135+
key := tokens[0]
136+
value := strings.Join(tokens[1:], "=")
137+
var calculatedValue interface{} = value
138+
139+
switch value {
140+
case "true":
141+
calculatedValue = true
142+
case "false":
143+
calculatedValue = false
144+
default:
145+
if i, err := strconv.Atoi(value); err == nil {
146+
calculatedValue = i
147+
}
148+
}
149+
150+
authOptions.fieldsOverrideMap[key] = calculatedValue
151+
}
152+
122153
return nil
123154
}
124155

service/authentication.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const (
3636

3737
// CreateJwtToken calculates a JWT authorization token based on the given secret.
3838
// If the secret is empty, an empty token is returned.
39-
func CreateJwtToken(jwtSecret, user string, serverId string, paths []string, exp time.Duration) (string, error) {
39+
func CreateJwtToken(jwtSecret, user string, serverId string, paths []string, exp time.Duration, fieldsOverride jwt.MapClaims) (string, error) {
4040
if jwtSecret == "" {
4141
return "", nil
4242
}
@@ -57,7 +57,12 @@ func CreateJwtToken(jwtSecret, user string, serverId string, paths []string, exp
5757
claims["allowed_paths"] = paths
5858
}
5959
if exp > 0 {
60-
claims["exp"] = time.Now().UTC().Add(exp).Unix()
60+
t := time.Now().UTC()
61+
claims["iat"] = t.Unix()
62+
claims["exp"] = t.Add(exp).Unix()
63+
}
64+
for k, v := range fieldsOverride {
65+
claims[k] = v
6166
}
6267
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
6368

@@ -77,7 +82,7 @@ func addJwtHeader(req *http.Request, jwtSecret string) error {
7782
if jwtSecret == "" {
7883
return nil
7984
}
80-
signedToken, err := CreateJwtToken(jwtSecret, "", "", nil, 0)
85+
signedToken, err := CreateJwtToken(jwtSecret, "", "", nil, 0, nil)
8186
if err != nil {
8287
return maskAny(err)
8388
}

0 commit comments

Comments
 (0)