mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2025-01-03 17:44:29 +00:00
bff1e0e901
Allows for Caplin to be used with other ELs from aside of Erigon --------- Co-authored-by: Giulio <giulio.rebuffo@gmail.com>
33 lines
793 B
Go
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)
|
|
}
|