|
| 1 | +/* |
| 2 | +Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +contributor license agreements. See the NOTICE file distributed with |
| 4 | +this work for additional information regarding copyright ownership. |
| 5 | +The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +(the "License"); you may not use this file except in compliance with |
| 7 | +the License. You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package api |
| 19 | + |
| 20 | +import ( |
| 21 | + "encoding/json" |
| 22 | + "net/http" |
| 23 | + "net/http/httptest" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/gin-gonic/gin" |
| 27 | + "github.com/spf13/viper" |
| 28 | + |
| 29 | + "github.com/apache/incubator-devlake/core/config" |
| 30 | + corecontext "github.com/apache/incubator-devlake/core/context" |
| 31 | + "github.com/apache/incubator-devlake/core/dal" |
| 32 | + "github.com/apache/incubator-devlake/core/log" |
| 33 | + "github.com/apache/incubator-devlake/impls/logruslog" |
| 34 | + "github.com/apache/incubator-devlake/server/api/shared" |
| 35 | +) |
| 36 | + |
| 37 | +type proxyAuthTestBasicRes struct { |
| 38 | + cfg config.ConfigReader |
| 39 | + logger log.Logger |
| 40 | +} |
| 41 | + |
| 42 | +func (b *proxyAuthTestBasicRes) GetConfigReader() config.ConfigReader { return b.cfg } |
| 43 | +func (b *proxyAuthTestBasicRes) GetConfig(name string) string { return b.cfg.GetString(name) } |
| 44 | +func (b *proxyAuthTestBasicRes) GetLogger() log.Logger { return b.logger } |
| 45 | +func (b *proxyAuthTestBasicRes) NestedLogger(name string) corecontext.BasicRes { |
| 46 | + return &proxyAuthTestBasicRes{cfg: b.cfg, logger: b.logger.Nested(name)} |
| 47 | +} |
| 48 | +func (b *proxyAuthTestBasicRes) ReplaceLogger(logger log.Logger) corecontext.BasicRes { |
| 49 | + return &proxyAuthTestBasicRes{cfg: b.cfg, logger: logger} |
| 50 | +} |
| 51 | +func (b *proxyAuthTestBasicRes) GetDal() dal.Dal { return nil } |
| 52 | + |
| 53 | +type proxyAuthResponse struct { |
| 54 | + Authenticated bool `json:"authenticated"` |
| 55 | + Name string `json:"name"` |
| 56 | + Email string `json:"email"` |
| 57 | +} |
| 58 | + |
| 59 | +func newProxyAuthRouter(secret string) *gin.Engine { |
| 60 | + gin.SetMode(gin.TestMode) |
| 61 | + cfg := viper.New() |
| 62 | + cfg.Set("FORWARDED_USER_SECRET", secret) |
| 63 | + basicRes := &proxyAuthTestBasicRes{ |
| 64 | + cfg: cfg, |
| 65 | + logger: logruslog.Global, |
| 66 | + } |
| 67 | + r := gin.New() |
| 68 | + r.Use(OAuth2ProxyAuthentication(basicRes)) |
| 69 | + r.GET("/me", func(c *gin.Context) { |
| 70 | + user, ok := shared.GetUser(c) |
| 71 | + if !ok { |
| 72 | + c.JSON(http.StatusOK, proxyAuthResponse{}) |
| 73 | + return |
| 74 | + } |
| 75 | + c.JSON(http.StatusOK, proxyAuthResponse{ |
| 76 | + Authenticated: true, |
| 77 | + Name: user.Name, |
| 78 | + Email: user.Email, |
| 79 | + }) |
| 80 | + }) |
| 81 | + return r |
| 82 | +} |
| 83 | + |
| 84 | +func performProxyAuthRequest(t *testing.T, router *gin.Engine, headers map[string]string) proxyAuthResponse { |
| 85 | + t.Helper() |
| 86 | + req := httptest.NewRequest(http.MethodGet, "/me", nil) |
| 87 | + for key, value := range headers { |
| 88 | + req.Header.Set(key, value) |
| 89 | + } |
| 90 | + recorder := httptest.NewRecorder() |
| 91 | + router.ServeHTTP(recorder, req) |
| 92 | + if recorder.Code != http.StatusOK { |
| 93 | + t.Fatalf("expected 200, got %d: %s", recorder.Code, recorder.Body.String()) |
| 94 | + } |
| 95 | + var body proxyAuthResponse |
| 96 | + if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil { |
| 97 | + t.Fatalf("decode response: %v", err) |
| 98 | + } |
| 99 | + return body |
| 100 | +} |
| 101 | + |
| 102 | +func TestOAuth2ProxyAuthenticationRejectsUntrustedForwardedHeaders(t *testing.T) { |
| 103 | + router := newProxyAuthRouter("shared-secret") |
| 104 | + cases := map[string]map[string]string{ |
| 105 | + "missing secret header": { |
| 106 | + forwardedUserHeader: "admin@example.com", |
| 107 | + forwardedEmailHeader: "admin@example.com", |
| 108 | + }, |
| 109 | + "mismatched secret header": { |
| 110 | + forwardedUserHeader: "admin@example.com", |
| 111 | + forwardedEmailHeader: "admin@example.com", |
| 112 | + forwardedUserSecretHeader: "wrong-secret", |
| 113 | + }, |
| 114 | + } |
| 115 | + for name, headers := range cases { |
| 116 | + t.Run(name, func(t *testing.T) { |
| 117 | + body := performProxyAuthRequest(t, router, headers) |
| 118 | + if body.Authenticated { |
| 119 | + t.Fatalf("expected forwarded headers to be rejected, got %+v", body) |
| 120 | + } |
| 121 | + }) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func TestOAuth2ProxyAuthenticationRequiresConfiguredSecret(t *testing.T) { |
| 126 | + router := newProxyAuthRouter("") |
| 127 | + body := performProxyAuthRequest(t, router, map[string]string{ |
| 128 | + forwardedUserHeader: "admin@example.com", |
| 129 | + forwardedEmailHeader: "admin@example.com", |
| 130 | + forwardedUserSecretHeader: "shared-secret", |
| 131 | + }) |
| 132 | + if body.Authenticated { |
| 133 | + t.Fatalf("expected forwarded headers to be ignored without FORWARDED_USER_SECRET, got %+v", body) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func TestOAuth2ProxyAuthenticationAcceptsTrustedForwardedHeaders(t *testing.T) { |
| 138 | + router := newProxyAuthRouter("shared-secret") |
| 139 | + body := performProxyAuthRequest(t, router, map[string]string{ |
| 140 | + forwardedUserHeader: "admin@example.com", |
| 141 | + forwardedEmailHeader: "admin@example.com", |
| 142 | + forwardedUserSecretHeader: "shared-secret", |
| 143 | + }) |
| 144 | + if !body.Authenticated || body.Name != "admin@example.com" || body.Email != "admin@example.com" { |
| 145 | + t.Fatalf("expected trusted forwarded user, got %+v", body) |
| 146 | + } |
| 147 | +} |
0 commit comments