erigon-pulse/cl/phase1/execution_client/rpc_helper/jwt_roundtripper.go
Enrique Jose Avila Asapche bff1e0e901
[Caplin] Added json rpc cl (#7836)
Allows for Caplin to be used with other ELs from aside of Erigon

---------

Co-authored-by: Giulio <giulio.rebuffo@gmail.com>
2023-07-08 16:42:30 +02:00

33 lines
793 B
Go

package rpc_helper
import (
"fmt"
"net/http"
"time"
"github.com/golang-jwt/jwt/v4"
)
type JWTRoundTripper struct {
underlyingTransport http.RoundTripper
jwtSecret []byte
}
func NewJWTRoundTripper(jwtSecret []byte) *JWTRoundTripper {
return &JWTRoundTripper{underlyingTransport: http.DefaultTransport, jwtSecret: jwtSecret}
}
func (t *JWTRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"iat": time.Now().Unix(),
})
tokenString, err := token.SignedString(t.jwtSecret)
if err != nil {
return nil, fmt.Errorf("JwtRoundTripper failed to produce a JWT token, err: %w", err)
}
req.Header.Set("Authorization", "Bearer "+tokenString)
return t.underlyingTransport.RoundTrip(req)
}