`Named` is implemented by every `Method` and future implementations should implement the method too.
Unified · Split
| @@ -36,6 +36,8 @@ var ( · 35 unmodified lines | |||
| 36 | algorithmPattern = regexp.MustCompile(`algorithm=(\w+)`) | 36 | algorithmPattern = regexp.MustCompile(`algorithm=(\w+)`) |
| 37 | versionPattern = regexp.MustCompile(`version=(\d+\.\d+)`) | 37 | versionPattern = regexp.MustCompile(`version=(\d+\.\d+)`) |
| 38 | authorizationPattern = regexp.MustCompile(`\AX-Ops-Authorization-(\d+)`) | 38 | authorizationPattern = regexp.MustCompile(`\AX-Ops-Authorization-(\d+)`) |
| 39 | |||
| 40 | _ auth.Method = &Auth{} | ||
| 39 | ) | 41 | ) |
| 40 | 42 | ||
| 41 | // Documentation: | 43 | // Documentation: |
| @@ -12,6 +12,8 @@ import ( · 11 unmodified lines | |||
| 12 | "code.gitea.io/gitea/services/packages" | 12 | "code.gitea.io/gitea/services/packages" |
| 13 | ) | 13 | ) |
| 14 | 14 | ||
| 15 | var _ auth.Method = &Auth{} | ||
| 16 | |||
| 15 | type Auth struct{} | 17 | type Auth struct{} |
| 16 | 18 | ||
| 17 | func (a *Auth) Name() string { | 19 | func (a *Auth) Name() string { |
| @@ -12,6 +12,8 @@ import ( · 11 unmodified lines | |||
| 12 | "code.gitea.io/gitea/services/packages" | 12 | "code.gitea.io/gitea/services/packages" |
| 13 | ) | 13 | ) |
| 14 | 14 | ||
| 15 | var _ auth.Method = &Auth{} | ||
| 16 | |||
| 15 | type Auth struct{} | 17 | type Auth struct{} |
| 16 | 18 | ||
| 17 | func (a *Auth) Name() string { | 19 | func (a *Auth) Name() string { |
| @@ -13,6 +13,8 @@ import ( · 12 unmodified lines | |||
| 13 | "code.gitea.io/gitea/services/auth" | 13 | "code.gitea.io/gitea/services/auth" |
| 14 | ) | 14 | ) |
| 15 | 15 | ||
| 16 | var _ auth.Method = &Auth{} | ||
| 17 | |||
| 16 | type Auth struct{} | 18 | type Auth struct{} |
| 17 | 19 | ||
| 18 | func (a *Auth) Name() string { | 20 | func (a *Auth) Name() string { |
| @@ -21,7 +21,6 @@ import ( · 20 unmodified lines | |||
| 21 | // Ensure the struct implements the interface. | 21 | // Ensure the struct implements the interface. |
| 22 | var ( | 22 | var ( |
| 23 | _ Method = &Basic{} | 23 | _ Method = &Basic{} |
| 24 | _ Named = &Basic{} | ||
| 25 | ) | 24 | ) |
| 26 | 25 | ||
| 27 | // BasicMethodName is the constant name of the basic authentication method | 26 | // BasicMethodName is the constant name of the basic authentication method |
| @@ -5,7 +5,6 @@ package auth · 4 unmodified lines | |||
| 5 | 5 | ||
| 6 | import ( | 6 | import ( |
| 7 | "net/http" | 7 | "net/http" |
| 8 | "reflect" | ||
| 9 | "strings" | 8 | "strings" |
| 10 | 9 | ||
| 11 | user_model "code.gitea.io/gitea/models/user" | 10 | user_model "code.gitea.io/gitea/models/user" |
| @@ -37,21 +36,16 @@ func (b *Group) Add(method Method) { · 25 unmodified lines | |||
| 37 | func (b *Group) Name() string { | 36 | func (b *Group) Name() string { |
| 38 | names := make([]string, 0, len(b.methods)) | 37 | names := make([]string, 0, len(b.methods)) |
| 39 | for _, m := range b.methods { | 38 | for _, m := range b.methods { |
| 40 | if n, ok := m.(Named); ok { | 39 | names = append(names, m.Name()) |
| 41 | names = append(names, n.Name()) | ||
| 42 | } else { | ||
| 43 | names = append(names, reflect.TypeOf(m).Elem().Name()) | ||
| 44 | } | ||
| 45 | } | 40 | } |
| 46 | return strings.Join(names, ",") | 41 | return strings.Join(names, ",") |
| 47 | } | 42 | } |
| 48 | 43 | ||
| 49 | // Verify extracts and validates | ||
| 50 | func (b *Group) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) (*user_model.User, error) { | 44 | func (b *Group) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) (*user_model.User, error) { |
| 51 | // Try to sign in with each of the enabled plugins | 45 | // Try to sign in with each of the enabled plugins |
| 52 | var retErr error | 46 | var retErr error |
| 53 | for _, ssoMethod := range b.methods { | 47 | for _, m := range b.methods { |
| 54 | user, err := ssoMethod.Verify(req, w, store, sess) | 48 | user, err := m.Verify(req, w, store, sess) |
| 55 | if err != nil { | 49 | if err != nil { |
| 56 | if retErr == nil { | 50 | if retErr == nil { |
| 57 | retErr = err | 51 | retErr = err |
| @@ -67,9 +61,7 @@ func (b *Group) Verify(req *http.Request, w http.ResponseWriter, store DataStore · 9 unmodified lines | |||
| 67 | // Return the user and ignore any error returned by previous methods. | 61 | // Return the user and ignore any error returned by previous methods. |
| 68 | if user != nil { | 62 | if user != nil { |
| 69 | if store.GetData()["AuthedMethod"] == nil { | 63 | if store.GetData()["AuthedMethod"] == nil { |
| 70 | if named, ok := ssoMethod.(Named); ok { | 64 | store.GetData()["AuthedMethod"] = m.Name() |
| 71 | store.GetData()["AuthedMethod"] = named.Name() | ||
| 72 | } | ||
| 73 | } | 65 | } |
| 74 | return user, nil | 66 | return user, nil |
| 75 | } | 67 | } |
| @@ -23,7 +23,6 @@ import ( · 22 unmodified lines | |||
| 23 | // Ensure the struct implements the interface. | 23 | // Ensure the struct implements the interface. |
| 24 | var ( | 24 | var ( |
| 25 | _ Method = &HTTPSign{} | 25 | _ Method = &HTTPSign{} |
| 26 | _ Named = &HTTPSign{} | ||
| 27 | ) | 26 | ) |
| 28 | 27 | ||
| 29 | // HTTPSign implements the Auth interface and authenticates requests (API requests | 28 | // HTTPSign implements the Auth interface and authenticates requests (API requests |
| @@ -27,10 +27,7 @@ type Method interface { · 26 unmodified lines | |||
| 27 | // Second argument returns err if verification fails, otherwise | 27 | // Second argument returns err if verification fails, otherwise |
| 28 | // First return argument returns nil if no matched verification condition | 28 | // First return argument returns nil if no matched verification condition |
| 29 | Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) (*user_model.User, error) | 29 | Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) (*user_model.User, error) |
| 30 | } | ||
| 31 | 30 | ||
| 32 | // Named represents a named thing | ||
| 33 | type Named interface { | ||
| 34 | Name() string | 31 | Name() string |
| 35 | } | 32 | } |
| 36 | 33 | ||
| @@ -22,7 +22,6 @@ import ( · 21 unmodified lines | |||
| 22 | // Ensure the struct implements the interface. | 22 | // Ensure the struct implements the interface. |
| 23 | var ( | 23 | var ( |
| 24 | _ Method = &OAuth2{} | 24 | _ Method = &OAuth2{} |
| 25 | _ Named = &OAuth2{} | ||
| 26 | ) | 25 | ) |
| 27 | 26 | ||
| 28 | // CheckOAuthAccessToken returns uid of user from oauth token | 27 | // CheckOAuthAccessToken returns uid of user from oauth token |
| @@ -20,7 +20,6 @@ import ( · 19 unmodified lines | |||
| 20 | // Ensure the struct implements the interface. | 20 | // Ensure the struct implements the interface. |
| 21 | var ( | 21 | var ( |
| 22 | _ Method = &ReverseProxy{} | 22 | _ Method = &ReverseProxy{} |
| 23 | _ Named = &ReverseProxy{} | ||
| 24 | ) | 23 | ) |
| 25 | 24 | ||
| 26 | // ReverseProxyMethodName is the constant name of the ReverseProxy authentication method | 25 | // ReverseProxyMethodName is the constant name of the ReverseProxy authentication method |
| @@ -14,7 +14,6 @@ import ( · 13 unmodified lines | |||
| 14 | // Ensure the struct implements the interface. | 14 | // Ensure the struct implements the interface. |
| 15 | var ( | 15 | var ( |
| 16 | _ Method = &Session{} | 16 | _ Method = &Session{} |
| 17 | _ Named = &Session{} | ||
| 18 | ) | 17 | ) |
| 19 | 18 | ||
| 20 | // Session checks if there is a user uid stored in the session and returns the user | 19 | // Session checks if there is a user uid stored in the session and returns the user |
| @@ -37,7 +37,6 @@ var ( · 36 unmodified lines | |||
| 37 | 37 | ||
| 38 | // Ensure the struct implements the interface. | 38 | // Ensure the struct implements the interface. |
| 39 | _ Method = &SSPI{} | 39 | _ Method = &SSPI{} |
| 40 | _ Named = &SSPI{} | ||
| 41 | ) | 40 | ) |
| 42 | 41 | ||
| 43 | // SSPI implements the SingleSignOn interface and authenticates requests | 42 | // SSPI implements the SingleSignOn interface and authenticates requests |