|
| 1 | +// Copyright 2014 The Gogs Authors. All rights reserved. |
| 2 | +// Copyright 2019 The Gitea Authors. All rights reserved. |
| 3 | +// Use of this source code is governed by a MIT-style |
| 4 | +// license that can be found in the LICENSE file. |
| 5 | + |
| 6 | +package sso |
| 7 | + |
| 8 | +import ( |
| 9 | + "fmt" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "code.gitea.io/gitea/models" |
| 13 | + "code.gitea.io/gitea/modules/log" |
| 14 | + "code.gitea.io/gitea/modules/setting" |
| 15 | + |
| 16 | + "gitea.com/macaron/macaron" |
| 17 | + "gitea.com/macaron/session" |
| 18 | + gouuid "github.com/google/uuid" |
| 19 | +) |
| 20 | + |
| 21 | +// Ensure the struct implements the interface. |
| 22 | +var ( |
| 23 | + _ SingleSignOn = &ReverseProxy{} |
| 24 | +) |
| 25 | + |
| 26 | +// ReverseProxy implements the SingleSignOn interface, but actually relies on |
| 27 | +// a reverse proxy for authentication of users. |
| 28 | +// On successful authentication the proxy is expected to populate the username in the |
| 29 | +// "setting.ReverseProxyAuthUser" header. Optionally it can also populate the email of the |
| 30 | +// user in the "setting.ReverseProxyAuthEmail" header. |
| 31 | +type ReverseProxy struct { |
| 32 | +} |
| 33 | + |
| 34 | +// getUserName extracts the username from the "setting.ReverseProxyAuthUser" header |
| 35 | +func (r *ReverseProxy) getUserName(ctx *macaron.Context) string { |
| 36 | + webAuthUser := strings.TrimSpace(ctx.Req.Header.Get(setting.ReverseProxyAuthUser)) |
| 37 | + if len(webAuthUser) == 0 { |
| 38 | + return "" |
| 39 | + } |
| 40 | + return webAuthUser |
| 41 | +} |
| 42 | + |
| 43 | +// Init does nothing as the ReverseProxy implementation does not need initialization |
| 44 | +func (r *ReverseProxy) Init() error { |
| 45 | + return nil |
| 46 | +} |
| 47 | + |
| 48 | +// Free does nothing as the ReverseProxy implementation does not have to release resources |
| 49 | +func (r *ReverseProxy) Free() error { |
| 50 | + return nil |
| 51 | +} |
| 52 | + |
| 53 | +// IsEnabled checks if EnableReverseProxyAuth setting is true |
| 54 | +func (r *ReverseProxy) IsEnabled() bool { |
| 55 | + return setting.Service.EnableReverseProxyAuth |
| 56 | +} |
| 57 | + |
| 58 | +// VerifyAuthData extracts the username from the "setting.ReverseProxyAuthUser" header |
| 59 | +// of the request and returns the corresponding user object for that name. |
| 60 | +// Verification of header data is not performed as it should have already been done by |
| 61 | +// the revese proxy. |
| 62 | +// If a username is available in the "setting.ReverseProxyAuthUser" header an existing |
| 63 | +// user object is returned (populated with username or email found in header). |
| 64 | +// Returns nil if header is empty. |
| 65 | +func (r *ReverseProxy) VerifyAuthData(ctx *macaron.Context, sess session.Store) *models.User { |
| 66 | + |
| 67 | + // Just return user if session is estabilshed already. |
| 68 | + user := SessionUser(sess) |
| 69 | + if user != nil { |
| 70 | + return user |
| 71 | + } |
| 72 | + |
| 73 | + // If no session established, get username from header. |
| 74 | + username := r.getUserName(ctx) |
| 75 | + if len(username) == 0 { |
| 76 | + return nil |
| 77 | + } |
| 78 | + |
| 79 | + var err error |
| 80 | + |
| 81 | + if r.isAutoRegisterAllowed() { |
| 82 | + // Use auto registration from reverse proxy if ENABLE_REVERSE_PROXY_AUTO_REGISTRATION enabled. |
| 83 | + if user, err = models.GetUserByName(username); err != nil { |
| 84 | + if models.IsErrUserNotExist(err) && r.isAutoRegisterAllowed() { |
| 85 | + if user = r.newUser(ctx); user == nil { |
| 86 | + return nil |
| 87 | + } |
| 88 | + } else { |
| 89 | + log.Error("GetUserByName: %v", err) |
| 90 | + return nil |
| 91 | + } |
| 92 | + } |
| 93 | + } else { |
| 94 | + // Use auto registration from other backends if ENABLE_REVERSE_PROXY_AUTO_REGISTRATION not enabled. |
| 95 | + if user, err = models.UserSignIn(username, "", true); err != nil { |
| 96 | + if !models.IsErrUserNotExist(err) { |
| 97 | + log.Error("UserSignIn: %v", err) |
| 98 | + } |
| 99 | + return nil |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // Initialize new session. |
| 104 | + handleSignIn(ctx, sess, user) |
| 105 | + |
| 106 | + user.SetLastLogin() |
| 107 | + if err = models.UpdateUserCols(user, false, "last_login_unix"); err != nil { |
| 108 | + log.Error(fmt.Sprintf("VerifyAuthData: error updating user last login time [user: %d]", user.ID)) |
| 109 | + } |
| 110 | + |
| 111 | + // Redirect to self to apply user language using cookie. |
| 112 | + ctx.Redirect(setting.AppSubURL + ctx.Req.URL.RequestURI()) |
| 113 | + |
| 114 | + return user |
| 115 | +} |
| 116 | + |
| 117 | +// isAutoRegisterAllowed checks if EnableReverseProxyAutoRegister setting is true |
| 118 | +func (r *ReverseProxy) isAutoRegisterAllowed() bool { |
| 119 | + return setting.Service.EnableReverseProxyAutoRegister |
| 120 | +} |
| 121 | + |
| 122 | +// newUser creates a new user object for the purpose of automatic registration |
| 123 | +// and populates its name and email with the information present in request headers. |
| 124 | +func (r *ReverseProxy) newUser(ctx *macaron.Context) *models.User { |
| 125 | + username := r.getUserName(ctx) |
| 126 | + if len(username) == 0 { |
| 127 | + return nil |
| 128 | + } |
| 129 | + |
| 130 | + email := gouuid.New().String() + "@localhost" |
| 131 | + if setting.Service.EnableReverseProxyEmail { |
| 132 | + webAuthEmail := ctx.Req.Header.Get(setting.ReverseProxyAuthEmail) |
| 133 | + if len(webAuthEmail) > 0 { |
| 134 | + email = webAuthEmail |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + user := &models.User{ |
| 139 | + Name: username, |
| 140 | + Email: email, |
| 141 | + Passwd: username, |
| 142 | + IsActive: true, |
| 143 | + } |
| 144 | + if err := models.CreateUser(user); err != nil { |
| 145 | + // FIXME: should I create a system notice? |
| 146 | + log.Error("CreateUser: %v", err) |
| 147 | + return nil |
| 148 | + } |
| 149 | + return user |
| 150 | +} |
0 commit comments