-
Notifications
You must be signed in to change notification settings - Fork 28
/
client.go
200 lines (168 loc) · 5.75 KB
/
client.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright 2018 The Service Manager Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package oidc
import (
"context"
"encoding/json"
"errors"
"net/http"
"github.com/Peripli/service-manager/pkg/auth"
"github.com/Peripli/service-manager/pkg/auth/util"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)
// ErrTokenExpired indicates that the access token has expired and cannot be refreshed
var ErrTokenExpired = errors.New("access token has expired")
// NewClient builds configured HTTP client.
//
// If token is provided will try to refresh the token if it has expired,
// otherwise if token is not provided will do client_credentials flow and fetch token
func NewClient(options *auth.Options, token *auth.Token) (*Client, error) {
httpClient, err := util.BuildHTTPClient(options)
if err != nil {
return nil, err
}
httpClient.Timeout = options.Timeout
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, httpClient)
var tt oauth2.Token
if token != nil {
tt.AccessToken = token.AccessToken
tt.RefreshToken = token.RefreshToken
tt.Expiry = token.ExpiresIn
tt.TokenType = token.TokenType
}
flow := options.AuthFlow
if flow == auth.DefaultFlow {
if options.User != "" {
flow = auth.PasswordGrant
} else {
flow = auth.ClientCredentials
}
}
tokenSource := noRefreshTokenSource(tt)
if options.ClientID != "" {
if tt.RefreshToken != "" {
tokenSource = refreshTokenSource(ctx, options, tt)
} else if flow == auth.ClientCredentials {
tokenSource = clientCredentialsTokenSource(ctx, options, tt)
}
}
oauthClient := oauth2.NewClient(ctx, tokenSource)
oauthClient.Timeout = options.Timeout
return &Client{
tokenSource: tokenSource,
httpClient: oauthClient,
}, nil
}
type requireLoginTokenSource struct{}
func (requireLoginTokenSource) Token() (*oauth2.Token, error) {
return nil, ErrTokenExpired
}
func noRefreshTokenSource(token oauth2.Token) oauth2.TokenSource {
var requireLogin requireLoginTokenSource
return oauth2.ReuseTokenSource(&token, requireLogin)
}
func refreshTokenSource(ctx context.Context, options *auth.Options, token oauth2.Token) oauth2.TokenSource {
oauthConfig := newOauth2Config(options)
return oauthConfig.TokenSource(ctx, &token)
}
func newClientCredentialsConfig(options *auth.Options) *clientcredentials.Config {
return &clientcredentials.Config{
ClientID: options.ClientID,
ClientSecret: options.ClientSecret,
TokenURL: options.TokenEndpoint,
AuthStyle: authStyle(options),
}
}
func newOauth2Config(options *auth.Options) *oauth2.Config {
return &oauth2.Config{
ClientID: options.ClientID,
ClientSecret: options.ClientSecret,
Endpoint: oauth2.Endpoint{
AuthURL: options.AuthorizationEndpoint,
TokenURL: options.TokenEndpoint,
AuthStyle: authStyle(options),
},
}
}
func authStyle(options *auth.Options) oauth2.AuthStyle {
authStyle := oauth2.AuthStyleAutoDetect
if !options.TokenBasicAuth {
authStyle = oauth2.AuthStyleInParams
}
return authStyle
}
func clientCredentialsTokenSource(ctx context.Context, options *auth.Options, token oauth2.Token) oauth2.TokenSource {
oauthConfig := newClientCredentialsConfig(options)
clientCredentialsSource := oauthConfig.TokenSource(ctx)
// The double wrapping of TokenSource objects is needed, because there is no other way
// to pass the existing access token and the client will try to fetch a token for each request
return oauth2.ReuseTokenSource(&token, clientCredentialsSource)
}
// Client is used to make http requests including bearer token automatically and refreshing it
// if necessary
type Client struct {
tokenSource oauth2.TokenSource
httpClient *http.Client
}
// Do makes a http request with the underlying HTTP client which includes an access token in the request
func (c *Client) Do(req *http.Request) (*http.Response, error) {
return c.httpClient.Do(req)
}
// Token returns the token, refreshing it if necessary
func (c *Client) Token() (*auth.Token, error) {
token, err := c.tokenSource.Token()
if err != nil {
return nil, err
}
return &auth.Token{
AccessToken: token.AccessToken,
RefreshToken: token.RefreshToken,
ExpiresIn: token.Expiry,
TokenType: token.TokenType,
}, nil
}
// DoRequestFunc is an alias for any function that takes an http request and returns a response and error
type DoRequestFunc func(request *http.Request) (*http.Response, error)
func fetchOpenidConfiguration(issuerURL string, readConfigurationFunc DoRequestFunc) (*openIDConfiguration, error) {
url := issuerURL + "/.well-known/openid-configuration"
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
response, err := readConfigurationFunc(req)
if err != nil {
return nil, err
}
if response.StatusCode != http.StatusOK {
return nil, errors.New("unexpected status code")
}
var configuration *openIDConfiguration
if err = unmarshalResponse(response, &configuration); err != nil {
return nil, err
}
return configuration, nil
}
// UnmarshalResponse reads the response body and tries to parse it.
func unmarshalResponse(response *http.Response, jsonResult interface{}) error {
defer func() {
err := response.Body.Close()
if err != nil {
panic(err)
}
}()
return json.NewDecoder(response.Body).Decode(&jsonResult)
}