Skip to content

Commit

Permalink
Update token expires
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Jan 9, 2025
1 parent b731b69 commit 970beb6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
7 changes: 5 additions & 2 deletions cmd/crproxy/cluster/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type flagpole struct {

TokenPrivateKeyFile string
TokenPublicKeyFile string
TokenExpiresSecond int

SimpleAuthUserpass map[string]string

Expand All @@ -49,7 +50,8 @@ type flagpole struct {

func NewCommand() *cobra.Command {
flags := &flagpole{
Address: ":18000",
Address: ":18000",
TokenExpiresSecond: 3600,
}

cmd := &cobra.Command{
Expand All @@ -69,6 +71,7 @@ func NewCommand() *cobra.Command {

cmd.Flags().StringVar(&flags.TokenPrivateKeyFile, "token-private-key-file", "", "private key file")
cmd.Flags().StringVar(&flags.TokenPublicKeyFile, "token-public-key-file", "", "public key file")
cmd.Flags().IntVar(&flags.TokenExpiresSecond, "token-expires-second", flags.TokenExpiresSecond, "Token expires second")

cmd.Flags().StringToStringVar(&flags.SimpleAuthUserpass, "simple-auth-userpass", flags.SimpleAuthUserpass, "Simple auth userpass")

Expand Down Expand Up @@ -196,7 +199,7 @@ func runE(ctx context.Context, flags *flagpole) error {
return t.Attribute, true
}

gen := token.NewGenerator(token.NewEncoder(signing.NewSigner(privateKey)), authFunc, logger)
gen := token.NewGenerator(token.NewEncoder(signing.NewSigner(privateKey)), authFunc, flags.TokenExpiresSecond, logger)
container.Handle("/auth/token", gen)

var handler http.Handler = container
Expand Down
19 changes: 11 additions & 8 deletions token/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,23 @@ import (
)

type Generator struct {
authFunc func(r *http.Request, userinfo *url.Userinfo, t *Token) (Attribute, bool)
logger *slog.Logger
tokenEncoder *Encoder
authFunc func(r *http.Request, userinfo *url.Userinfo, t *Token) (Attribute, bool)
logger *slog.Logger
expiresSecond int
tokenEncoder *Encoder
}

func NewGenerator(
tokenEncoder *Encoder,
authFunc func(r *http.Request, userinfo *url.Userinfo, t *Token) (Attribute, bool),
expiresSecond int,
logger *slog.Logger,
) *Generator {
return &Generator{
authFunc: authFunc,
logger: logger,
tokenEncoder: tokenEncoder,
authFunc: authFunc,
expiresSecond: expiresSecond,
logger: logger,
tokenEncoder: tokenEncoder,
}
}

Expand All @@ -46,8 +49,8 @@ func (g *Generator) ServeHTTP(rw http.ResponseWriter, r *http.Request) {

rw.Header().Set("Content-Type", "application/json")

now := time.Now()
expiresIn := 60
now := time.Now().UTC()
expiresIn := g.expiresSecond

t.ExpiresAt = now.Add((time.Duration(expiresIn) + 10) * time.Second)

Expand Down

0 comments on commit 970beb6

Please sign in to comment.