-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.go
70 lines (60 loc) · 1.92 KB
/
token.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package wgo
import (
"net/http"
"time"
)
// Reference: golang.org/x/oauth2
// More targeted changes compared to oauth2 to avoid introducing too many dependencies
// A TokenSource is anything that can return a token.
type TokenSource interface {
// Token returns a token or an error.
// Token must be safe for concurrent use by multiple goroutines.
// The returned Token must not be modified.
Token() (*Token, error)
}
type tokenSourceFunc func() (*Token, error)
func (f tokenSourceFunc) Token() (*Token, error) {
return f()
}
// Token represents the credentials used to authorize
// the requests to access protected resources on the OAuth 2.0
// provider's backend.
//
// Most users of this package should not access fields of Token
// directly. They're exported mostly for use by related packages
// implementing derivative OAuth2 flows.
type Token struct {
// AccessToken is the token that authorizes and authenticates
// the requests.
AccessToken string `json:"access_token"`
// Expiry is the optional expiration time of the access token.
//
// If zero, TokenSource implementations will reuse the same
// token forever and RefreshToken or equivalent
// mechanisms for that TokenSource will not be used.
Expiry time.Time `json:"expiry,omitempty"`
}
// RoundTripper is a RoundTripper that implements the http.RoundTripper interface
type RoundTripper struct {
next http.RoundTripper
TokenSource TokenSource
}
// RoundTrip Add the AccessToken parameter to the request
func (r *RoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
if r.next == nil {
r.next = http.DefaultTransport
}
if r.TokenSource == nil {
resp, err := r.next.RoundTrip(request)
return resp, err
}
tok, err := r.TokenSource.Token()
if err != nil {
return nil, err
}
val := request.URL.Query()
val.Set("access_token", tok.AccessToken)
request.URL.RawQuery = val.Encode()
resp, err := r.next.RoundTrip(request)
return resp, err
}