-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjwt.go
36 lines (27 loc) · 769 Bytes
/
jwt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package jwt
import (
"fmt"
"net/http"
"github.com/alexfalkowski/go-service/security/header"
"github.com/alexfalkowski/go-service/security/jwt"
)
// NewRoundTripper for token.
func NewRoundTripper(gen jwt.Generator, hrt http.RoundTripper) *RoundTripper {
return &RoundTripper{gen: gen, RoundTripper: hrt}
}
// RoundTripper for zap.
type RoundTripper struct {
gen jwt.Generator
http.RoundTripper
}
func (r *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
t, err := r.gen.Generate(req.Context())
if err != nil {
return nil, err
}
if len(t) == 0 {
return nil, header.ErrInvalidAuthorization
}
req.Header.Add("Authorization", fmt.Sprintf("%s %s", header.BearerAuthorization, string(t)))
return r.RoundTripper.RoundTrip(req)
}