-
Notifications
You must be signed in to change notification settings - Fork 153
/
gitlab.go
308 lines (239 loc) · 8.86 KB
/
gitlab.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package auth
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/weaveworks/weave-gitops/pkg/services/auth/internal"
"github.com/weaveworks/weave-gitops/pkg/services/auth/types"
)
const (
serverShutdownSuccessMessage = "The authorization flow was completed, you may close your browser and go back to the CLI.\n"
serverShutdownErrorMessage = "\nThere was an issue with the authorization flow, you may close your browser and go back to the CLI for more information.\n"
)
var gitlabScopes = []string{"api", "read_user", "profile"}
type gitlabAuthFlow struct {
client *http.Client
codeVerifier internal.CodeVerifier
redirectUri string
scopes []string
}
//counterfeiter:generate . GitlabAuthClient
type GitlabAuthClient interface {
AuthURL(ctx context.Context, redirectUri string) (url.URL, error)
ExchangeCode(ctx context.Context, redirectUri, code string) (*types.TokenResponseState, error)
ValidateToken(ctx context.Context, token string) error
}
type glAuth struct {
http *http.Client
verifier internal.CodeVerifier
}
func NewGitlabAuthClient(client *http.Client) GitlabAuthClient {
cv, err := internal.NewCodeVerifier(internal.GitlabVerifierMin, internal.GitlabVerifierMax)
if err != nil {
panic(err)
}
return glAuth{
http: client,
verifier: cv,
}
}
func (g glAuth) AuthURL(ctx context.Context, redirectUri string) (url.URL, error) {
return internal.GitlabAuthorizeUrl(redirectUri, gitlabScopes, g.verifier)
}
func (g glAuth) ExchangeCode(ctx context.Context, redirectUri, code string) (*types.TokenResponseState, error) {
tUrl := internal.GitlabTokenUrl(redirectUri, code, g.verifier)
return doCodeExchangeRequest(ctx, tUrl, g.http)
}
func (g glAuth) ValidateToken(ctx context.Context, token string) error {
u := internal.GitlabUserUrl()
req, err := http.NewRequestWithContext(ctx, "GET", u.String(), nil)
if err != nil {
return err
}
res, err := g.http.Do(req)
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
return fmt.Errorf("invalid token: %s", res.Status)
}
return nil
}
func NewGitlabAuthFlow(redirectUri string, client *http.Client) (types.AuthFlow, error) {
cv, err := internal.NewCodeVerifier(internal.GitlabVerifierMin, internal.GitlabVerifierMax)
if err != nil {
return nil, err
}
return &gitlabAuthFlow{
client: client,
codeVerifier: cv,
redirectUri: redirectUri,
scopes: gitlabScopes,
}, nil
}
func (gaf *gitlabAuthFlow) Authorize(ctx context.Context) (*http.Request, error) {
authUrl, err := internal.GitlabAuthorizeUrl(gaf.redirectUri, gaf.scopes, gaf.codeVerifier)
if err != nil {
return nil, fmt.Errorf("gitlab auth flow create authorize url: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, authUrl.String(), strings.NewReader(""))
if err != nil {
return nil, fmt.Errorf("gitlab authorize endpoint new request: %w", err)
}
return req, nil
}
func (gaf *gitlabAuthFlow) CallbackHandler(tokenState *types.TokenResponseState, next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
defer next.ServeHTTP(w, r)
params := r.URL.Query()
code := params.Get("code")
tokenUrl := internal.GitlabTokenUrl(gaf.redirectUri, code, gaf.codeVerifier)
ctx := context.Background()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, tokenUrl.String(), strings.NewReader(""))
req.Header.Set("Content-Type", "application/json")
if err != nil {
reqErr := fmt.Errorf("unable to generate request to fetch gitlab token, please try again later: %w", err)
tokenState.HttpStatusCode = http.StatusInternalServerError
tokenState.Err = reqErr
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(reqErr.Error()))
return
}
res, err := gaf.client.Do(req)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
tokenState.HttpStatusCode = http.StatusInternalServerError
tokenState.Err = fmt.Errorf("gitlab token requeset client issue: %w", err)
return
}
if res.StatusCode == http.StatusOK {
payload, err := parseTokenResponseBody(res.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
tokenState.HttpStatusCode = http.StatusInternalServerError
tokenState.Err = fmt.Errorf("gitlab token response json decode: %w", err)
return
}
w.WriteHeader(http.StatusOK)
tokenState.SetGitlabTokenResponse(payload)
tokenState.HttpStatusCode = http.StatusOK
} else {
w.WriteHeader(res.StatusCode)
_, _ = w.Write([]byte(http.StatusText(res.StatusCode)))
tokenState.HttpStatusCode = res.StatusCode
tokenState.Err = errors.New(http.StatusText(res.StatusCode))
}
}
return http.HandlerFunc(fn)
}
func doCodeExchangeRequest(ctx context.Context, tUrl url.URL, c *http.Client) (*types.TokenResponseState, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, tUrl.String(), strings.NewReader(""))
if err != nil {
return nil, fmt.Errorf("could not create gitlab code request: %w", err)
}
res, err := c.Do(req)
if err != nil {
return nil, fmt.Errorf("error exchanging gitlab code: %w", err)
}
if res.StatusCode != http.StatusOK {
errRes := struct {
Error string `json:"error"`
Description string `json:"error_description"`
}{}
if err := json.NewDecoder(res.Body).Decode(&errRes); err != nil {
return nil, fmt.Errorf("could not parse error response: %w", err)
}
return nil, fmt.Errorf("code=%v, error=%s, description=%s", res.StatusCode, errRes.Error, errRes.Description)
}
r, err := parseTokenResponseBody(res.Body)
if err != nil {
return nil, err
}
token := &types.TokenResponseState{}
token.SetGitlabTokenResponse(r)
return token, nil
}
func parseTokenResponseBody(body io.ReadCloser) (internal.GitlabTokenResponse, error) {
defer func() {
_ = body.Close()
}()
var tokenResponse internal.GitlabTokenResponse
err := json.NewDecoder(body).Decode(&tokenResponse)
if err != nil {
return internal.GitlabTokenResponse{}, err
}
return tokenResponse, nil
}
// NewGitlabAuthFlowHandler returns a BlockingCLIAuthHandler for the Gitlab OAuth flow in a CLI.
// It will set up a temporary server as a callback mechanism. Once the user runs through the flow
// the server will be shutdown, and we will exit the function.
func NewGitlabAuthFlowHandler(client *http.Client, flow types.AuthFlow) BlockingCLIAuthHandler {
return func(ctx context.Context, w io.Writer) (string, error) {
req, err := flow.Authorize(ctx)
if err != nil {
return "", fmt.Errorf("could not do code request: %w", err)
}
_, _ = fmt.Fprintf(w, "Starting authorization server:\n")
_, _ = fmt.Fprintf(w, "Visit this URL to authenticate with Gitlab:\n\n")
_, _ = fmt.Fprintf(w, "%s\n\n", req.URL)
_, _ = fmt.Fprintf(w, "Waiting for authentication flow completion...\n\n")
serverShutdown := &sync.WaitGroup{}
serverShutdown.Add(1)
token := types.TokenResponseState{}
server := startAuthServerForCLI(serverShutdown, w, &token, flow)
_, clientErr := client.Do(req)
if clientErr != nil {
serverShutdown.Done()
token.Err = fmt.Errorf("gitlab auth client error: %w", clientErr)
}
serverShutdown.Wait()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
fmt.Fprintf(w, "Shutting the server down...\n")
if err := server.Shutdown(shutdownCtx); err != nil {
fmt.Fprintf(w, "An error occurred shutting down the server: %s\n", err.Error())
}
fmt.Fprintf(w, "Server shutdown complete!\n")
if token.Err != nil {
fmt.Fprintf(w, "There was an issue going through the Gitlab authentication flow:\n\n")
if token.HttpStatusCode == 0 || token.HttpStatusCode == http.StatusOK {
fmt.Fprintf(w, "%s", token.Err.Error())
} else {
fmt.Fprintf(w, "Gitlab returned status code %d with the error message: %s", token.HttpStatusCode, token.Err.Error())
}
return "", token.Err
} else {
return token.AccessToken, nil
}
}
}
func startAuthServerForCLI(wg *sync.WaitGroup, w io.Writer, token *types.TokenResponseState, flow types.AuthFlow) *http.Server {
srv := &http.Server{Addr: internal.GitlabTempServerPort}
http.Handle(internal.GitlabCallbackPath, flow.CallbackHandler(token, shutdownServerForCLI(token, wg)))
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
fmt.Fprintf(w, "Error starting server: %s", err.Error())
}
}()
return srv
}
func shutdownServerForCLI(token *types.TokenResponseState, wg *sync.WaitGroup) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
defer wg.Done()
if token.HttpStatusCode == http.StatusOK {
_, _ = w.Write([]byte(serverShutdownSuccessMessage))
} else {
_, _ = w.Write([]byte(serverShutdownErrorMessage))
}
}
return http.HandlerFunc(fn)
}