Skip to content

Commit 5ac876a

Browse files
committed
Add a new method Match for auth
1 parent e546480 commit 5ac876a

File tree

12 files changed

+58
-4
lines changed

12 files changed

+58
-4
lines changed

routers/api/packages/chef/auth.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ func (a *Auth) Name() string {
5252
return "chef"
5353
}
5454

55+
func (a *Auth) Match(req *http.Request) bool {
56+
return true
57+
}
58+
5559
// Verify extracts the user from the signed request
5660
// If the request is signed with the user private key the user is verified.
5761
func (a *Auth) Verify(req *http.Request, w http.ResponseWriter, store auth.DataStore, sess auth.SessionStore) (*user_model.User, error) {

routers/api/packages/conan/auth.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ func (a *Auth) Name() string {
2020
return "conan"
2121
}
2222

23+
func (a *Auth) Match(req *http.Request) bool {
24+
return true
25+
}
26+
2327
// Verify extracts the user from the Bearer token
2428
func (a *Auth) Verify(req *http.Request, w http.ResponseWriter, store auth.DataStore, sess auth.SessionStore) (*user_model.User, error) {
2529
packageMeta, err := packages.ParseAuthorizationRequest(req)

routers/api/packages/container/auth.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ func (a *Auth) Name() string {
2020
return "container"
2121
}
2222

23+
func (a *Auth) Match(req *http.Request) bool {
24+
return true
25+
}
26+
2327
// Verify extracts the user from the Bearer token
2428
// If it's an anonymous session a ghost user is returned
2529
func (a *Auth) Verify(req *http.Request, w http.ResponseWriter, store auth.DataStore, sess auth.SessionStore) (*user_model.User, error) {

routers/api/packages/nuget/auth.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ func (a *Auth) Name() string {
2121
return "nuget"
2222
}
2323

24+
func (a *Auth) Match(req *http.Request) bool {
25+
return true
26+
}
27+
2428
// https://docs.microsoft.com/en-us/nuget/api/package-publish-resource#request-parameters
2529
func (a *Auth) Verify(req *http.Request, w http.ResponseWriter, store auth.DataStore, sess auth.SessionStore) (*user_model.User, error) {
2630
token, err := auth_model.GetAccessTokenBySHA(req.Context(), req.Header.Get("X-NuGet-ApiKey"))

services/auth/basic.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ func (b *Basic) Name() string {
4242
return BasicMethodName
4343
}
4444

45+
func (b *Basic) Match(req *http.Request) bool {
46+
return true
47+
}
48+
4549
// Verify extracts and validates Basic data (username and password/token) from the
4650
// "Authorization" header of the request and returns the corresponding user object for that
4751
// name/token on successful validation.

services/auth/group.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,22 @@ func (b *Group) Name() string {
4141
return strings.Join(names, ",")
4242
}
4343

44+
func (b *Group) Match(req *http.Request) bool {
45+
return true
46+
}
47+
4448
func (b *Group) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) (*user_model.User, error) {
45-
// Try to sign in with each of the enabled plugins
46-
var retErr error
49+
// find all methods that match the request
50+
matchedMethods := make([]Method, 0, len(b.methods))
4751
for _, m := range b.methods {
52+
if m.Match(req) {
53+
matchedMethods = append(matchedMethods, m)
54+
}
55+
}
56+
57+
var retErr error
58+
// Try to sign in with each of the matched plugins
59+
for _, m := range matchedMethods {
4860
user, err := m.Verify(req, w, store, sess)
4961
if err != nil {
5062
if retErr == nil {

services/auth/httpsign.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ func (h *HTTPSign) Name() string {
3636
return "httpsign"
3737
}
3838

39+
func (h *HTTPSign) Match(req *http.Request) bool {
40+
return true
41+
}
42+
3943
// Verify extracts and validates HTTPsign from the Signature header of the request and returns
4044
// the corresponding user object on successful validation.
4145
// Returns nil if header is empty or validation fails.

services/auth/interface.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ type Method interface {
2424
// If verification is successful returns either an existing user object (with id > 0)
2525
// or a new user object (with id = 0) populated with the information that was found
2626
// in the authentication data (username or email).
27-
// Second argument returns err if verification fails, otherwise
27+
// Third argument returns err if verification fails, otherwise
28+
// Second return argument returns true
2829
// First return argument returns nil if no matched verification condition
2930
Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) (*user_model.User, error)
30-
31+
// Match returns true if the request is a match for this method
32+
Match(*http.Request) bool
3133
Name() string
3234
}
3335

services/auth/oauth2.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ func (o *OAuth2) userIDFromToken(ctx context.Context, tokenSHA string, store Dat
131131
return t.UID
132132
}
133133

134+
func (o *OAuth2) Match(req *http.Request) bool {
135+
return true
136+
}
137+
134138
// Verify extracts the user ID from the OAuth token in the query parameters
135139
// or the "Authorization" header and returns the corresponding user object for that ID.
136140
// If verification is successful returns an existing user object.

services/auth/reverseproxy.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ func (r *ReverseProxy) getUserFromAuthEmail(req *http.Request) *user_model.User
100100
return user
101101
}
102102

103+
func (r *ReverseProxy) Match(req *http.Request) bool {
104+
return true
105+
}
106+
103107
// Verify attempts to load a user object based on headers sent by the reverse proxy.
104108
// First it will attempt to load it based on the username (see docs for getUserFromAuthUser),
105109
// and failing that it will attempt to load it based on the email (see docs for getUserFromAuthEmail).

0 commit comments

Comments
 (0)