-
Notifications
You must be signed in to change notification settings - Fork 3
/
middleware_plugin.go
272 lines (244 loc) · 8.15 KB
/
middleware_plugin.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
package traefik_github_oauth_plugin
import (
"context"
"crypto/rand"
"encoding/hex"
"fmt"
"net/http"
"os"
"strings"
"github.com/MuXiu1997/traefik-github-oauth-plugin/internal/app/traefik-github-oauth-server/model"
"github.com/MuXiu1997/traefik-github-oauth-plugin/internal/pkg/constant"
"github.com/MuXiu1997/traefik-github-oauth-plugin/internal/pkg/jwt"
gologger "github.com/apsdehal/go-logger"
"github.com/dghubble/sling"
"github.com/scylladb/go-set/strset"
)
const (
DefaultConfigAuthPath = "/_auth"
)
// Config the middleware configuration.
type Config struct {
ApiBaseUrl string `json:"api_base_url,omitempty"`
ApiSecretKey string `json:"api_secret_key,omitempty"`
AuthPath string `json:"auth_path,omitempty"`
JwtSecretKey string `json:"jwt_secret_key,omitempty"`
LogLevel string `json:"log_level,omitempty"`
Whitelist ConfigWhitelist `json:"whitelist,omitempty"`
}
// ConfigWhitelist the middleware configuration whitelist.
type ConfigWhitelist struct {
// Ids the GitHub user id list.
Ids []string `json:"ids,omitempty"`
// Logins the GitHub user login list.
Logins []string `json:"logins,omitempty"`
}
// CreateConfig creates the default middleware configuration.
func CreateConfig() *Config {
return &Config{
ApiBaseUrl: "",
ApiSecretKey: "",
AuthPath: DefaultConfigAuthPath,
JwtSecretKey: getRandomString32(),
Whitelist: ConfigWhitelist{
Ids: []string{},
Logins: []string{},
},
}
}
// TraefikGithubOauthMiddleware the middleware.
type TraefikGithubOauthMiddleware struct {
ctx context.Context
next http.Handler
name string
apiBaseUrl string
apiSecretKey string
authPath string
jwtSecretKey string
whitelistIdSet *strset.Set
whitelistLoginSet *strset.Set
logger *gologger.Logger
}
var _ http.Handler = (*TraefikGithubOauthMiddleware)(nil)
// New creates a new TraefikGithubOauthMiddleware.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
// region Setup logger
logLevel := gologger.InfoLevel
switch config.LogLevel {
case "DEBUG", "debug":
logLevel = gologger.DebugLevel
case "INFO", "info":
logLevel = gologger.InfoLevel
case "WARNING", "warning", "WARN", "warn":
logLevel = gologger.WarningLevel
case "ERROR", "error":
logLevel = gologger.ErrorLevel
}
logger, err := gologger.New("TraefikGithubOauthMiddleware", os.Stdout, 0)
if err != nil {
return nil, err
}
logger.SetLogLevel(logLevel)
logger.SetFormat("[%{module}] | %{level} | %{time} | %{message}")
// endregion Setup logger
authPath := config.AuthPath
if !strings.HasPrefix(authPath, "/") {
authPath = "/" + authPath
}
return &TraefikGithubOauthMiddleware{
ctx: ctx,
next: next,
name: name,
apiBaseUrl: config.ApiBaseUrl,
apiSecretKey: config.ApiSecretKey,
authPath: authPath,
jwtSecretKey: config.JwtSecretKey,
whitelistIdSet: strset.New(config.Whitelist.Ids...),
whitelistLoginSet: strset.New(config.Whitelist.Logins...),
logger: logger,
}, nil
}
// ServeHTTP implements http.Handler.
func (p *TraefikGithubOauthMiddleware) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if req.URL.Path == p.authPath {
p.handleAuthRequest(rw, req)
return
}
p.handleRequest(rw, req)
}
// handleRequest
func (p *TraefikGithubOauthMiddleware) handleRequest(rw http.ResponseWriter, req *http.Request) {
user, err := p.getGitHubUserFromCookie(req)
if err != nil {
p.logger.Debugf("handleRequest: getGitHubUserFromCookie: %s\n", err.Error())
if req.Method == http.MethodGet {
p.redirectToOAuthPage(rw, req)
}
http.Error(rw, err.Error(), http.StatusUnauthorized)
return
}
if !p.whitelistIdSet.Has(user.Id) && !p.whitelistLoginSet.Has(user.Login) {
setNoCacheHeaders(rw)
http.Error(rw, "not in whitelist", http.StatusForbidden)
return
}
p.next.ServeHTTP(rw, req)
}
// handleAuthRequest
func (p *TraefikGithubOauthMiddleware) handleAuthRequest(rw http.ResponseWriter, req *http.Request) {
setNoCacheHeaders(rw)
rid := req.URL.Query().Get(constant.QUERY_KEY_REQUEST_ID)
result, err := p.getAuthResult(rid)
if err != nil {
p.logger.Debugf("handleAuthRequest: getAuthResult: %s\n", err.Error())
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
tokenString, err := jwt.GenerateJwtTokenString(result.GitHubUserID, result.GitHubUserLogin, p.jwtSecretKey)
if err != nil {
p.logger.Debugf("handleAuthRequest: GenerateJwtTokenString: %s\n", err.Error())
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
http.SetCookie(rw, &http.Cookie{
Name: constant.COOKIE_NAME_JWT,
Value: tokenString,
HttpOnly: true,
})
http.Redirect(rw, req, result.RedirectURI, http.StatusFound)
}
func (p *TraefikGithubOauthMiddleware) redirectToOAuthPage(rw http.ResponseWriter, req *http.Request) {
setNoCacheHeaders(rw)
oAuthPageURL, err := p.generateOAuthPageURL(getRawRequestUrl(req), p.getAuthURL(req))
if err != nil {
p.logger.Debugf("redirectToOAuthPage: generateOAuthPageURL: %s\n", err.Error())
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(rw, req, oAuthPageURL, http.StatusFound)
}
func (p *TraefikGithubOauthMiddleware) generateOAuthPageURL(redirectURI, authURL string) (string, error) {
reqBody := model.RequestGenerateOAuthPageURL{
RedirectURI: redirectURI,
AuthURL: authURL,
}
req := sling.New().Base(p.apiBaseUrl).Post(constant.ROUTER_GROUP_PATH_OAUTH + "/" + constant.ROUTER_PATH_OAUTH_PAGE_URL)
if 0 < len(p.apiSecretKey) {
req.Set(constant.HTTP_HEADER_AUTHORIZATION, fmt.Sprintf("%s %s", constant.AUTHORIZATION_PREFIX_TOKEN, p.apiSecretKey))
}
var respBody model.ResponseGenerateOAuthPageURL
var errRespBody model.ResponseError
_, err := req.BodyJSON(reqBody).Receive(&respBody, &errRespBody)
if err != nil {
return "", err
}
if 0 < len(errRespBody.Message) {
return "", fmt.Errorf("rpc failed, message: %s", errRespBody.Message)
}
return respBody.OAuthPageURL, nil
}
func (p *TraefikGithubOauthMiddleware) getAuthResult(rid string) (*model.ResponseGetAuthResult, error) {
req := sling.New().Base(p.apiBaseUrl).Get(constant.ROUTER_GROUP_PATH_OAUTH + "/" + constant.ROUTER_PATH_OAUTH_RESULT)
if 0 < len(p.apiSecretKey) {
req.Set(constant.HTTP_HEADER_AUTHORIZATION, fmt.Sprintf("%s %s", constant.AUTHORIZATION_PREFIX_TOKEN, p.apiSecretKey))
}
// req.QueryStruct seems to panic in yaegi
httpRequest, err := req.Request()
if err != nil {
return nil, err
}
q := httpRequest.URL.Query()
q.Add(constant.QUERY_KEY_REQUEST_ID, rid)
httpRequest.URL.RawQuery = q.Encode()
var respBody model.ResponseGetAuthResult
var errRespBody model.ResponseError
_, err = req.Do(httpRequest, &respBody, &errRespBody)
if err != nil {
return nil, err
}
if 0 < len(errRespBody.Message) {
return nil, fmt.Errorf("rpc failed, message: %s", errRespBody.Message)
}
return &respBody, nil
}
func (p *TraefikGithubOauthMiddleware) getGitHubUserFromCookie(req *http.Request) (*jwt.PayloadUser, error) {
jwtCookie, err := req.Cookie(constant.COOKIE_NAME_JWT)
if err != nil {
return nil, err
}
return jwt.ParseTokenString(jwtCookie.Value, p.jwtSecretKey)
}
func (p *TraefikGithubOauthMiddleware) getAuthURL(originalReq *http.Request) string {
var builder strings.Builder
scheme := "http"
if originalReq.TLS != nil {
scheme = "https"
}
builder.WriteString(scheme)
builder.WriteString("://")
builder.WriteString(originalReq.Host)
builder.WriteString(p.authPath)
return builder.String()
}
func setNoCacheHeaders(rw http.ResponseWriter) {
rw.Header().Set(constant.HTTP_HEADER_CACHE_CONTROL, "no-cache, no-store, must-revalidate, private")
rw.Header().Set(constant.HTTP_HEADER_PRAGMA, "no-cache")
rw.Header().Set(constant.HTTP_HEADER_EXPIRES, "0")
}
func getRawRequestUrl(originalReq *http.Request) string {
var builder strings.Builder
scheme := "http"
if originalReq.TLS != nil {
scheme = "https"
}
builder.WriteString(scheme)
builder.WriteString("://")
builder.WriteString(originalReq.Host)
builder.WriteString(originalReq.URL.String())
return builder.String()
}
func getRandomString32() string {
randBytes := make([]byte, 16)
_, _ = rand.Read(randBytes)
return hex.EncodeToString(randBytes)
}