-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create S2S tokens * Create a config struct S2SClient * Fix CI
- Loading branch information
1 parent
bc9c31b
commit fe8f8fe
Showing
6 changed files
with
111 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package authcontrol | ||
|
||
import ( | ||
"maps" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/go-chi/jwtauth/v5" | ||
"github.com/go-chi/traceid" | ||
"github.com/go-chi/transport" | ||
"github.com/lestrrat-go/jwx/v2/jwt" | ||
) | ||
|
||
type S2SClientConfig struct { | ||
Service string | ||
JWTSecret string | ||
DebugRequests bool | ||
} | ||
|
||
// Service-to-service HTTP client for internal communication between Sequence services. | ||
func S2SClient(cfg *S2SClientConfig) *http.Client { | ||
httpClient := &http.Client{ | ||
Transport: transport.Chain(http.DefaultTransport, | ||
traceid.Transport, | ||
transport.SetHeaderFunc("Authorization", s2sAuthHeader(cfg.JWTSecret, map[string]any{"service": cfg.Service})), | ||
transport.If(cfg.DebugRequests, transport.LogRequests(transport.LogOptions{Concise: true, CURL: true})), | ||
), | ||
} | ||
|
||
return httpClient | ||
} | ||
|
||
// Create short-lived service-to-service JWT token for internal communication between Sequence services. | ||
func S2SToken(jwtSecret string, claims map[string]any) string { | ||
jwtAuth := jwtauth.New("HS256", []byte(jwtSecret), nil, jwt.WithAcceptableSkew(2*time.Minute)) | ||
|
||
now := time.Now().UTC() | ||
|
||
c := maps.Clone(claims) | ||
if c == nil { | ||
c = map[string]any{} | ||
} | ||
|
||
c["iat"] = now | ||
|
||
if _, ok := c["exp"]; !ok { | ||
c["exp"] = now.Add(30 * time.Second) | ||
} | ||
|
||
_, t, _ := jwtAuth.Encode(c) | ||
return t | ||
} | ||
|
||
func s2sAuthHeader(jwtSecret string, claims map[string]any) func(req *http.Request) string { | ||
return func(req *http.Request) string { | ||
return "BEARER " + S2SToken(jwtSecret, claims) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters