Skip to content

Commit 5687053

Browse files
Add JWT token option to S2SClient (#50)
* Add JWT token option to S2SClient * Add clarification to S2SClient function documentation regarding JWT token creation * Add clarification to S2SClient function documentation regarding JWTToken precedence * Use transport.SetHeader() for the static JWT token --------- Co-authored-by: Vojtech Vitek <[email protected]>
1 parent 42e426f commit 5687053

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ require (
99
github.com/go-chi/metrics v0.1.0
1010
github.com/go-chi/traceid v0.2.0
1111
github.com/go-chi/transport v0.4.0
12+
github.com/goware/base64 v0.1.0
13+
github.com/jxskiss/base62 v1.1.0
1214
github.com/lestrrat-go/jwx/v2 v2.1.3
15+
github.com/spf13/cobra v1.9.1
1316
github.com/stretchr/testify v1.10.0
1417
)
1518

@@ -20,9 +23,7 @@ require (
2023
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
2124
github.com/goccy/go-json v0.10.3 // indirect
2225
github.com/google/uuid v1.6.0 // indirect
23-
github.com/goware/base64 v0.1.0 // indirect
2426
github.com/inconshreveable/mousetrap v1.1.0 // indirect
25-
github.com/jxskiss/base62 v1.1.0 // indirect
2627
github.com/kr/text v0.2.0 // indirect
2728
github.com/lestrrat-go/blackmagic v1.0.2 // indirect
2829
github.com/lestrrat-go/httpcc v1.0.1 // indirect
@@ -37,7 +38,6 @@ require (
3738
github.com/prometheus/procfs v0.15.1 // indirect
3839
github.com/rogpeppe/go-internal v1.12.0 // indirect
3940
github.com/segmentio/asm v1.2.0 // indirect
40-
github.com/spf13/cobra v1.9.1 // indirect
4141
github.com/spf13/pflag v1.0.6 // indirect
4242
golang.org/x/crypto v0.31.0 // indirect
4343
golang.org/x/sync v0.10.0 // indirect

s2s.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,21 @@ import (
1414
)
1515

1616
type S2SClientConfig struct {
17-
Service string
18-
JWTSecret string
19-
AccessKey string
17+
// JWTToken is the static JWT token used for authentication.
18+
JWTToken string
19+
// JWTSecret is the secret key used to dynamically create JWT BEARER token for authorization.
20+
JWTSecret string
21+
// Service is used in the service claim of the JWT token.
22+
Service string
23+
// AccessKey is an optional access key used for authentication.
24+
AccessKey string
25+
// DebugRequests enables logging of HTTP requests.
2026
DebugRequests bool
2127
}
2228

2329
// Service-to-service HTTP client for internal communication between Sequence services.
30+
// If JWTSecret is provided, it will create a HS256 JWT token with the service name in the claims.
31+
// If both JWTSecret and JWTToken are provided, JWTToken will take precedence.
2432
func S2SClient(cfg *S2SClientConfig) *http.Client {
2533
serviceName := cmp.Or(cfg.Service, filepath.Base(os.Args[0]))
2634

@@ -33,6 +41,9 @@ func S2SClient(cfg *S2SClientConfig) *http.Client {
3341
return "BEARER " + S2SToken(cfg.JWTSecret, map[string]any{"service": serviceName})
3442
}),
3543
),
44+
transport.If(cfg.JWTToken != "",
45+
transport.SetHeader("Authorization", "BEARER "+cfg.JWTToken),
46+
),
3647
transport.If(cfg.AccessKey != "",
3748
transport.SetHeader("X-Access-Key", cfg.AccessKey),
3849
),

0 commit comments

Comments
 (0)