mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2025-01-05 18:42:19 +00:00
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)
|
||
|
}
|