forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uaa_authentication.go
113 lines (91 loc) · 3.11 KB
/
uaa_authentication.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package wrapper
import (
"bytes"
"io/ioutil"
"net/http"
"strings"
"code.cloudfoundry.org/cli/api/uaa"
)
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . UAAClient
// UAAClient is the interface for getting a valid access token
type UAAClient interface {
RefreshAccessToken(refreshToken string) (uaa.RefreshedTokens, error)
}
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . TokenCache
// TokenCache is where the UAA token information is stored.
type TokenCache interface {
AccessToken() string
RefreshToken() string
SetAccessToken(token string)
SetRefreshToken(token string)
}
// UAAAuthentication wraps connections and adds authentication headers to all
// requests
type UAAAuthentication struct {
connection uaa.Connection
client UAAClient
cache TokenCache
}
// NewUAAAuthentication returns a pointer to a UAAAuthentication wrapper with
// the client and token cache.
func NewUAAAuthentication(client UAAClient, cache TokenCache) *UAAAuthentication {
return &UAAAuthentication{
client: client,
cache: cache,
}
}
// Make adds authentication headers to the passed in request and then calls the
// wrapped connection's Make
func (t *UAAAuthentication) Make(request *http.Request, passedResponse *uaa.Response) error {
if t.client == nil {
return t.connection.Make(request, passedResponse)
}
var err error
var rawRequestBody []byte
if request.Body != nil {
rawRequestBody, err = ioutil.ReadAll(request.Body)
defer request.Body.Close()
if err != nil {
return err
}
request.Body = ioutil.NopCloser(bytes.NewBuffer(rawRequestBody))
if skipAuthenticationHeader(request, rawRequestBody) {
return t.connection.Make(request, passedResponse)
}
}
request.Header.Set("Authorization", t.cache.AccessToken())
err = t.connection.Make(request, passedResponse)
if _, ok := err.(uaa.InvalidAuthTokenError); ok {
tokens, refreshErr := t.client.RefreshAccessToken(t.cache.RefreshToken())
if refreshErr != nil {
return refreshErr
}
t.cache.SetAccessToken(tokens.AuthorizationToken())
t.cache.SetRefreshToken(tokens.RefreshToken)
if rawRequestBody != nil {
request.Body = ioutil.NopCloser(bytes.NewBuffer(rawRequestBody))
}
request.Header.Set("Authorization", t.cache.AccessToken())
return t.connection.Make(request, passedResponse)
}
return err
}
// SetClient sets the UAA client that the wrapper will use.
func (t *UAAAuthentication) SetClient(client UAAClient) {
t.client = client
}
// Wrap sets the connection on the UAAAuthentication and returns itself
func (t *UAAAuthentication) Wrap(innerconnection uaa.Connection) uaa.Connection {
t.connection = innerconnection
return t
}
// The authentication header is not added to token refresh requests or login
// requests.
func skipAuthenticationHeader(request *http.Request, body []byte) bool {
stringBody := string(body)
return strings.Contains(request.URL.String(), "/oauth/token") &&
request.Method == http.MethodPost &&
(strings.Contains(stringBody, "grant_type=refresh_token") ||
strings.Contains(stringBody, "grant_type=password") ||
strings.Contains(stringBody, "grant_type=client_credentials"))
}