-
Notifications
You must be signed in to change notification settings - Fork 153
/
gitlab.go
121 lines (93 loc) · 2.95 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
package auth
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"github.com/weaveworks/weave-gitops/pkg/services/auth/internal"
"github.com/weaveworks/weave-gitops/pkg/services/auth/types"
)
var gitlabScopes = []string{"api", "read_user", "profile"}
//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 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
}