History

refactor auth interface to return error when verify failure (#22119)

This PR changed the Auth interface signature from `Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *user_model.User` to `Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) (*user_model.User, error)`. There is a new return argument `error` which means the verification condition matched but verify process failed, we should stop the auth process. Before this PR, when return a `nil` user, we don't know the reason why it returned `nil`. If the match condition is not satisfied or it verified failure? For these two different results, we should have different handler. If the match condition is not satisfied, we should try next auth method and if there is no more auth method, it's an anonymous user. If the condition matched but verify failed, the auth process should be stop and return immediately. This will fix #20563

Lunny Xiao committed 3 years ago

Commit ca67c5a1 parent 7cc7db715 files · +111 −79Browse files

Showing the changes to services/auth/httpsign.go (1 of 15 files). Show all

Unified · Split

services/auth/httpsign.go+7−7
@@ -39,10 +39,10 @@ func (h *HTTPSign) Name() string { · 38 unmodified lines
39// Verify extracts and validates HTTPsign from the Signature header of the request and returns39// Verify extracts and validates HTTPsign from the Signature header of the request and returns
40// the corresponding user object on successful validation.40// the corresponding user object on successful validation.
41// Returns nil if header is empty or validation fails.41// Returns nil if header is empty or validation fails.
42func (h *HTTPSign) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *user_model.User {42func (h *HTTPSign) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) (*user_model.User, error) {
43 sigHead := req.Header.Get("Signature")43 sigHead := req.Header.Get("Signature")
44 if len(sigHead) == 0 {44 if len(sigHead) == 0 {
45 return nil45 return nil, nil
46 }46 }
47 47
48 var (48 var (
@@ -53,14 +53,14 @@ func (h *HTTPSign) Verify(req *http.Request, w http.ResponseWriter, store DataSt · 4 unmodified lines
53 if len(req.Header.Get("X-Ssh-Certificate")) != 0 {53 if len(req.Header.Get("X-Ssh-Certificate")) != 0 {
54 // Handle Signature signed by SSH certificates54 // Handle Signature signed by SSH certificates
55 if len(setting.SSH.TrustedUserCAKeys) == 0 {55 if len(setting.SSH.TrustedUserCAKeys) == 0 {
56 return nil56 return nil, nil
57 }57 }
58 58
59 publicKey, err = VerifyCert(req)59 publicKey, err = VerifyCert(req)
60 if err != nil {60 if err != nil {
61 log.Debug("VerifyCert on request from %s: failed: %v", req.RemoteAddr, err)61 log.Debug("VerifyCert on request from %s: failed: %v", req.RemoteAddr, err)
62 log.Warn("Failed authentication attempt from %s", req.RemoteAddr)62 log.Warn("Failed authentication attempt from %s", req.RemoteAddr)
63 return nil63 return nil, nil
64 }64 }
65 } else {65 } else {
66 // Handle Signature signed by Public Key66 // Handle Signature signed by Public Key
@@ -68,21 +68,21 @@ func (h *HTTPSign) Verify(req *http.Request, w http.ResponseWriter, store DataSt · 1 unmodified line
68 if err != nil {68 if err != nil {
69 log.Debug("VerifyPubKey on request from %s: failed: %v", req.RemoteAddr, err)69 log.Debug("VerifyPubKey on request from %s: failed: %v", req.RemoteAddr, err)
70 log.Warn("Failed authentication attempt from %s", req.RemoteAddr)70 log.Warn("Failed authentication attempt from %s", req.RemoteAddr)
71 return nil71 return nil, nil
72 }72 }
73 }73 }
74 74
75 u, err := user_model.GetUserByID(req.Context(), publicKey.OwnerID)75 u, err := user_model.GetUserByID(req.Context(), publicKey.OwnerID)
76 if err != nil {76 if err != nil {
77 log.Error("GetUserByID: %v", err)77 log.Error("GetUserByID: %v", err)
78 return nil78 return nil, err
79 }79 }
80 80
81 store.GetData()["IsApiToken"] = true81 store.GetData()["IsApiToken"] = true
82 82
83 log.Trace("HTTP Sign: Logged in user %-v", u)83 log.Trace("HTTP Sign: Logged in user %-v", u)
84 84
85 return u85 return u, nil
86}86}
87 87
88func VerifyPubKey(r *http.Request) (*asymkey_model.PublicKey, error) {88func VerifyPubKey(r *http.Request) (*asymkey_model.PublicKey, error) {