Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
tsuzu committed Dec 17, 2024
1 parent e589551 commit 3d19bad
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/miscord-dev/dexsidecar

go 1.23.4

require github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
26 changes: 15 additions & 11 deletions pkg/issuer/issuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"os"
"strings"
"time"

"github.com/golang-jwt/jwt/v5"
)

type Issuer interface {
Expand Down Expand Up @@ -72,21 +74,22 @@ func (iss *tokenIssuer) issue(ctx context.Context, config Config) (string, int,
}

func (iss *tokenIssuer) loadTokenExp(ctx context.Context, config Config) (*time.Time, error) {
fp, err := os.Open(config.DstPath)
b, err := os.ReadFile(config.DstPath)
if err != nil {
return nil, fmt.Errorf("failed to open file %s: %w", config.DstPath, err)
}
defer fp.Close()

var jwt struct {
Exp int `json:"exp"`
var claims jwt.RegisteredClaims
_, err = jwt.ParseWithClaims(string(b), &claims, nil, jwt.WithoutClaimsValidation())
if err != nil {
return nil, fmt.Errorf("failed to parse jwt: %w", err)
}
if err := json.NewDecoder(fp).Decode(&jwt); err != nil {
return nil, fmt.Errorf("failed to decode jwt: %w", err)

if claims.ExpiresAt == nil {
return nil, fmt.Errorf("missing expiration claim")
}

exp := time.Unix(int64(jwt.Exp), 0)
return &exp, nil
return &claims.ExpiresAt.Time, nil
}

func (iss *tokenIssuer) save(ctx context.Context, config Config, token string) error {
Expand All @@ -96,8 +99,9 @@ func (iss *tokenIssuer) save(ctx context.Context, config Config, token string) e
}
defer fp.Close()

if err := json.NewEncoder(fp).Encode(token); err != nil {
return fmt.Errorf("failed to encode token: %w", err)
_, err = fp.WriteString(token)
if err != nil {
return fmt.Errorf("failed to write token: %w", err)
}

return nil
Expand Down Expand Up @@ -129,7 +133,7 @@ func (iss *tokenIssuer) Rotate(ctx context.Context) error {
} else if expiresAt.After(iss.now().Add(config.RefreshBefore)) {
return nil
}
slog.Info("token is outdating or deleted", "expires_at", expiresAt, "refresh_before", config.RefreshBefore)
slog.Info("token is being outdated or deleted", "expires_at", expiresAt, "refresh_before", config.RefreshBefore)

token, expIn, err := iss.issue(ctx, config)
if err != nil {
Expand Down

0 comments on commit 3d19bad

Please sign in to comment.