/
idtokenprovider.go
243 lines (200 loc) · 6.19 KB
/
idtokenprovider.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package internal
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strings"
"log"
retry "github.com/avast/retry-go"
"github.com/pkg/errors"
"golang.org/x/net/html"
)
type idTokenProvider interface {
fetchIdToken() (string, error)
}
type dexIdTokenProvider struct {
httpClient *http.Client
config idProviderConfig
}
func newDexIdTokenProvider(httpClient *http.Client, config idProviderConfig) idTokenProvider {
// turn off follow-redirects
httpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
return &dexIdTokenProvider{httpClient, config}
}
func (p *dexIdTokenProvider) fetchIdToken() (string, error) {
flowResult, err := p.implicitFlow()
if err != nil {
return "", err
}
return flowResult["id_token"], nil
}
func (p *dexIdTokenProvider) implicitFlow() (map[string]string, error) {
var result map[string]string = nil
happyRun := true
err := retry.Do(
func() error {
loginEndpoint, err := p.initializeImplicitFlow()
if err != nil {
return err
}
approvalEndpoint, err := p.login(loginEndpoint)
if err != nil {
return err
}
finalRedirectURL, err := p.receiveToken(approvalEndpoint)
if err != nil {
return err
}
result = p.parseTokenResponse(finalRedirectURL)
return nil
},
retry.Attempts(p.config.RetryConfig.MaxAttempts),
retry.Delay(p.config.RetryConfig.Delay),
retry.DelayType(retry.FixedDelay),
retry.OnRetry(func(retryNo uint, err error) {
happyRun = false
log.Printf("Retry: [%d / %d], error: %s", retryNo, p.config.RetryConfig.MaxAttempts, err)
}),
)
if err != nil {
return nil, err
}
if happyRun {
log.Println("Flow finished flawlessly")
}
return result, nil
}
//Performs call to the <authorize> endpoint.
func (p *dexIdTokenProvider) initializeImplicitFlow() (string, error) {
authorizeResp, err := p.httpClient.PostForm(p.config.DexConfig.AuthorizeEndpoint, url.Values{
"response_type": {"id_token token"},
"client_id": {p.config.ClientConfig.ID},
"redirect_uri": {p.config.ClientConfig.RedirectUri},
"scope": {"openid profile email groups"},
"nonce": {"vF7FAQlqq41CObeUFYY0ggv1qEELvfHaXQ0ER4XM"},
})
if err != nil {
return "", err
}
defer closeRespBody(authorizeResp)
authorizeRespBody := readRespBody(authorizeResp)
switch authorizeResp.StatusCode {
case http.StatusFound:
case http.StatusOK:
default:
return "", fmt.Errorf("got unexpected response on authorize: %d - %s", authorizeResp.StatusCode, authorizeRespBody)
}
var loginEndpoint string
if authorizeResp.StatusCode == http.StatusOK {
b := bytes.NewBufferString(authorizeRespBody)
loginEndpoint, err = getLocalAuthEndpoint(b)
if err != nil {
return "", errors.Wrapf(err, "while fetching link to static authentication")
}
} else {
loginEndpoint = authorizeResp.Header.Get("location")
if strings.Contains(loginEndpoint, "#.*error") {
return "", fmt.Errorf("login - Redirected with error: '%s'", loginEndpoint)
}
}
return loginEndpoint, nil
}
//Handles redirect to login endpoint
func (p *dexIdTokenProvider) login(loginEndpoint string) (string, error) {
if _, err := p.httpClient.Get(p.config.DexConfig.BaseUrl + loginEndpoint); err != nil {
return "", errors.Wrap(err, "while performing HTTP GET on login endpoint")
}
loginResp, err := p.httpClient.PostForm(p.config.DexConfig.BaseUrl+loginEndpoint, url.Values{
"login": {p.config.UserCredentials.Username},
"password": {p.config.UserCredentials.Password},
})
if err != nil {
return "", errors.Wrap(err, "while performing HTTP POST on login endpoint")
}
defer closeRespBody(loginResp)
loginRespBody := readRespBody(loginResp)
if loginResp.StatusCode < 300 || loginResp.StatusCode > 399 {
return "", fmt.Errorf("login - response error: '%s' - %s", loginResp.Status, loginRespBody)
}
approvalEndpoint := loginResp.Header.Get("location")
if strings.Contains(approvalEndpoint, "#.*error") {
return "", fmt.Errorf("approval - Redirected with error: '%s'", approvalEndpoint)
}
return approvalEndpoint, nil
}
//Handles redirect to approval endpoint to get the token (end of flow)
func (p *dexIdTokenProvider) receiveToken(approvalEndpoint string) (*url.URL, error) {
approvalResp, err := p.httpClient.Get(p.config.DexConfig.BaseUrl + approvalEndpoint)
if err != nil {
return nil, err
}
defer closeRespBody(approvalResp)
approvalRespBody := readRespBody(approvalResp)
if approvalResp.StatusCode < 300 || approvalResp.StatusCode > 399 {
return nil, errors.New(fmt.Sprintf("Approval - response error: '%s' - %s", approvalResp.Status, approvalRespBody))
}
clientEndpoint := approvalResp.Header.Get("location")
if strings.Contains(clientEndpoint, "#.*error") {
return nil, fmt.Errorf("client - Redirected with error: '%s'", clientEndpoint)
}
parsedUrl, parseErr := url.Parse(clientEndpoint)
if parseErr != nil {
return nil, parseErr
}
return parsedUrl, nil
}
func (p *dexIdTokenProvider) parseTokenResponse(parsedUrl *url.URL) map[string]string {
result := make(map[string]string)
fragmentParams := strings.Split(parsedUrl.Fragment, "&")
for _, param := range fragmentParams {
keyAndValue := strings.Split(param, "=")
result[keyAndValue[0]] = keyAndValue[1]
}
return result
}
func readRespBody(resp *http.Response) string {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("WARNING: Unable to read response body (status: '%s'). Root cause: %v", resp.Status, err)
return "<<Error reading response body>>"
}
return string(b)
}
func closeRespBody(resp *http.Response) {
err := resp.Body.Close()
if err != nil {
log.Printf("WARNING: Unable to close response body. Cause: %v", err)
}
}
func getLocalAuthEndpoint(body io.Reader) (string, error) {
z := html.NewTokenizer(body)
for {
nt := z.Next()
if nt == html.ErrorToken {
return "", errors.New("got HTML error token")
}
token := z.Token()
if "a" != token.Data {
continue
}
for _, attr := range token.Attr {
if attr.Key != "href" {
continue
}
match, err := regexp.MatchString("/auth/local.*", attr.Val)
if err != nil {
log.Printf("WARNING: Unable to match string. Cause: %v", err)
return "", err
}
if match {
return attr.Val, nil
}
}
}
}