Skip to content

Commit ef4c900

Browse files
committed
remove unused method of Auth interface
1 parent 3b141e1 commit ef4c900

File tree

9 files changed

+46
-85
lines changed

9 files changed

+46
-85
lines changed

modules/context/api.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,8 @@ func (ctx *APIContext) CheckForOTP() {
220220
// APIAuth converts auth.Auth as a middleware
221221
func APIAuth(authMethod auth.Auth) func(*APIContext) {
222222
return func(ctx *APIContext) {
223-
if !authMethod.IsEnabled() {
224-
return
225-
}
226223
// Get user from session if logged in.
227-
ctx.User = authMethod.VerifyAuthData(ctx.Req, ctx.Resp, ctx, ctx.Session)
224+
ctx.User = authMethod.Verify(ctx.Req, ctx.Resp, ctx, ctx.Session)
228225
if ctx.User != nil {
229226
ctx.IsBasicAuth = ctx.Data["AuthedMethod"].(string) == new(auth.Basic).Name()
230227
ctx.IsSigned = true

modules/context/context.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -608,11 +608,7 @@ func getCsrfOpts() CsrfOptions {
608608
// Auth converts auth.Auth as a middleware
609609
func Auth(authMethod auth.Auth) func(*Context) {
610610
return func(ctx *Context) {
611-
if !authMethod.IsEnabled() {
612-
return
613-
}
614-
615-
ctx.User = authMethod.VerifyAuthData(ctx.Req, ctx.Resp, ctx, ctx.Session)
611+
ctx.User = authMethod.Verify(ctx.Req, ctx.Resp, ctx, ctx.Session)
616612
if ctx.User != nil {
617613
ctx.IsBasicAuth = ctx.Data["AuthedMethod"].(string) == new(auth.Basic).Name()
618614
ctx.IsSigned = true

services/auth/auth.go

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ var authMethods = []Auth{
3131
&OAuth2{},
3232
&Basic{},
3333
&Session{},
34-
&ReverseProxy{},
3534
}
3635

3736
// The purpose of the following three function variables is to let the linter know that
@@ -53,6 +52,9 @@ func Register(method Auth) {
5352
// Init should be called exactly once when the application starts to allow plugins
5453
// to allocate necessary resources
5554
func Init() {
55+
if setting.Service.EnableReverseProxyAuth {
56+
Register(&ReverseProxy{})
57+
}
5658
for _, method := range Methods() {
5759
err := method.Init()
5860
if err != nil {
@@ -72,33 +74,6 @@ func Free() {
7274
}
7375
}
7476

75-
// SessionUser returns the user object corresponding to the "uid" session variable.
76-
func SessionUser(sess SessionStore) *models.User {
77-
// Get user ID
78-
uid := sess.Get("uid")
79-
if uid == nil {
80-
return nil
81-
}
82-
log.Trace("Session Authorization: Found user[%d]", uid)
83-
84-
id, ok := uid.(int64)
85-
if !ok {
86-
return nil
87-
}
88-
89-
// Get user object
90-
user, err := models.GetUserByID(id)
91-
if err != nil {
92-
if !models.IsErrUserNotExist(err) {
93-
log.Error("GetUserById: %v", err)
94-
}
95-
return nil
96-
}
97-
98-
log.Trace("Session Authorization: Logged in user %-v", user)
99-
return user
100-
}
101-
10277
// isAttachmentDownload check if request is a file download (GET) with URL to an attachment
10378
func isAttachmentDownload(req *http.Request) bool {
10479
return strings.HasPrefix(req.URL.Path, "/attachments/") && req.Method == "GET"

services/auth/basic.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,11 @@ func (b *Basic) Free() error {
4343
return nil
4444
}
4545

46-
// IsEnabled returns true as this plugin is enabled by default and its not possible to disable
47-
// it from settings.
48-
func (b *Basic) IsEnabled() bool {
49-
return true
50-
}
51-
52-
// VerifyAuthData extracts and validates Basic data (username and password/token) from the
46+
// Verify extracts and validates Basic data (username and password/token) from the
5347
// "Authorization" header of the request and returns the corresponding user object for that
5448
// name/token on successful validation.
5549
// Returns nil if header is empty or validation fails.
56-
func (b *Basic) VerifyAuthData(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *models.User {
50+
func (b *Basic) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *models.User {
5751
// Basic authentication should only fire on API, Download or on Git or LFSPaths
5852
if !middleware.IsAPIPath(req) && !isAttachmentDownload(req) && !isGitRawOrLFSPath(req) {
5953
return nil

services/auth/group.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,15 @@ func (b *Group) Free() error {
5252
return nil
5353
}
5454

55-
// IsEnabled returns true as this plugin is enabled by default and its not possible to disable
56-
// it from settings.
57-
func (b *Group) IsEnabled() bool {
58-
return true
59-
}
60-
61-
// VerifyAuthData extracts and validates
62-
func (b *Group) VerifyAuthData(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *models.User {
55+
// Verify extracts and validates
56+
func (b *Group) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *models.User {
6357
if !models.HasEngine {
6458
return nil
6559
}
6660

6761
// Try to sign in with each of the enabled plugins
6862
for _, ssoMethod := range b.methods {
69-
if !ssoMethod.IsEnabled() {
70-
continue
71-
}
72-
user := ssoMethod.VerifyAuthData(req, w, store, sess)
63+
user := ssoMethod.Verify(req, w, store, sess)
7364
if user != nil {
7465
if store.GetData()["AuthedMethod"] == nil {
7566
store.GetData()["AuthedMethod"] = ssoMethod.Name()

services/auth/interface.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,10 @@ type Auth interface {
3030
// give chance to the plugin to free any allocated resources
3131
Free() error
3232

33-
// IsEnabled checks if the current auth method has been enabled in settings.
34-
IsEnabled() bool
35-
36-
// VerifyAuthData tries to verify the authentication data contained in the request.
33+
// Verify tries to verify the authentication data contained in the request.
3734
// If verification is successful returns either an existing user object (with id > 0)
3835
// or a new user object (with id = 0) populated with the information that was found
3936
// in the authentication data (username or email).
4037
// Returns nil if verification fails.
41-
VerifyAuthData(http *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *models.User
38+
Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *models.User
4239
}

services/auth/oauth2.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,11 @@ func (o *OAuth2) userIDFromToken(req *http.Request, store DataStore) int64 {
112112
return t.UID
113113
}
114114

115-
// IsEnabled returns true as this plugin is enabled by default and its not possible
116-
// to disable it from settings.
117-
func (o *OAuth2) IsEnabled() bool {
118-
return true
119-
}
120-
121-
// VerifyAuthData extracts the user ID from the OAuth token in the query parameters
115+
// Verify extracts the user ID from the OAuth token in the query parameters
122116
// or the "Authorization" header and returns the corresponding user object for that ID.
123117
// If verification is successful returns an existing user object.
124118
// Returns nil if verification fails.
125-
func (o *OAuth2) VerifyAuthData(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *models.User {
119+
func (o *OAuth2) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *models.User {
126120
if !models.HasEngine {
127121
return nil
128122
}

services/auth/reverseproxy.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,14 @@ func (r *ReverseProxy) Free() error {
5454
return nil
5555
}
5656

57-
// IsEnabled checks if EnableReverseProxyAuth setting is true
58-
func (r *ReverseProxy) IsEnabled() bool {
59-
return setting.Service.EnableReverseProxyAuth
60-
}
61-
62-
// VerifyAuthData extracts the username from the "setting.ReverseProxyAuthUser" header
57+
// Verify extracts the username from the "setting.ReverseProxyAuthUser" header
6358
// of the request and returns the corresponding user object for that name.
6459
// Verification of header data is not performed as it should have already been done by
6560
// the revese proxy.
6661
// If a username is available in the "setting.ReverseProxyAuthUser" header an existing
6762
// user object is returned (populated with username or email found in header).
6863
// Returns nil if header is empty.
69-
func (r *ReverseProxy) VerifyAuthData(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *models.User {
64+
func (r *ReverseProxy) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *models.User {
7065
username := r.getUserName(req)
7166
if len(username) == 0 {
7267
return nil

services/auth/session.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99

1010
"code.gitea.io/gitea/models"
11+
"code.gitea.io/gitea/modules/log"
1112
)
1213

1314
// Ensure the struct implements the interface.
@@ -35,19 +36,40 @@ func (s *Session) Free() error {
3536
return nil
3637
}
3738

38-
// IsEnabled returns true as this plugin is enabled by default and its not possible to disable
39-
// it from settings.
40-
func (s *Session) IsEnabled() bool {
41-
return true
42-
}
43-
44-
// VerifyAuthData checks if there is a user uid stored in the session and returns the user
39+
// Verify checks if there is a user uid stored in the session and returns the user
4540
// object for that uid.
4641
// Returns nil if there is no user uid stored in the session.
47-
func (s *Session) VerifyAuthData(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *models.User {
42+
func (s *Session) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *models.User {
4843
user := SessionUser(sess)
4944
if user != nil {
5045
return user
5146
}
5247
return nil
5348
}
49+
50+
// SessionUser returns the user object corresponding to the "uid" session variable.
51+
func SessionUser(sess SessionStore) *models.User {
52+
// Get user ID
53+
uid := sess.Get("uid")
54+
if uid == nil {
55+
return nil
56+
}
57+
log.Trace("Session Authorization: Found user[%d]", uid)
58+
59+
id, ok := uid.(int64)
60+
if !ok {
61+
return nil
62+
}
63+
64+
// Get user object
65+
user, err := models.GetUserByID(id)
66+
if err != nil {
67+
if !models.IsErrUserNotExist(err) {
68+
log.Error("GetUserById: %v", err)
69+
}
70+
return nil
71+
}
72+
73+
log.Trace("Session Authorization: Logged in user %-v", user)
74+
return user
75+
}

0 commit comments

Comments
 (0)