AuthFallback implements GET and POST /auth/{authType}/fallback/web?session={sessionID}
( w http.ResponseWriter, req *http.Request, authType string, cfg *config.ClientAPI, )
| 100 | |
| 101 | // AuthFallback implements GET and POST /auth/{authType}/fallback/web?session={sessionID} |
| 102 | func AuthFallback( |
| 103 | w http.ResponseWriter, req *http.Request, authType string, |
| 104 | cfg *config.ClientAPI, |
| 105 | ) *util.JSONResponse { |
| 106 | sessionID := req.URL.Query().Get("session") |
| 107 | |
| 108 | if sessionID == "" { |
| 109 | return writeHTTPMessage(w, req, |
| 110 | "Session ID not provided", |
| 111 | http.StatusBadRequest, |
| 112 | ) |
| 113 | } |
| 114 | |
| 115 | serveRecaptcha := func() { |
| 116 | data := map[string]string{ |
| 117 | "myUrl": req.URL.String(), |
| 118 | "session": sessionID, |
| 119 | "siteKey": cfg.RecaptchaPublicKey, |
| 120 | } |
| 121 | serveTemplate(w, recaptchaTemplate, data) |
| 122 | } |
| 123 | |
| 124 | serveSuccess := func() { |
| 125 | data := map[string]string{} |
| 126 | serveTemplate(w, successTemplate, data) |
| 127 | } |
| 128 | |
| 129 | if req.Method == http.MethodGet { |
| 130 | // Handle Recaptcha |
| 131 | if authType == authtypes.LoginTypeRecaptcha { |
| 132 | if err := checkRecaptchaEnabled(cfg, w, req); err != nil { |
| 133 | return err |
| 134 | } |
| 135 | |
| 136 | serveRecaptcha() |
| 137 | return nil |
| 138 | } |
| 139 | return &util.JSONResponse{ |
| 140 | Code: http.StatusNotFound, |
| 141 | JSON: jsonerror.NotFound("Unknown auth stage type"), |
| 142 | } |
| 143 | } else if req.Method == http.MethodPost { |
| 144 | // Handle Recaptcha |
| 145 | if authType == authtypes.LoginTypeRecaptcha { |
| 146 | if err := checkRecaptchaEnabled(cfg, w, req); err != nil { |
| 147 | return err |
| 148 | } |
| 149 | |
| 150 | clientIP := req.RemoteAddr |
| 151 | err := req.ParseForm() |
| 152 | if err != nil { |
| 153 | util.GetLogger(req.Context()).WithError(err).Error("req.ParseForm failed") |
| 154 | res := jsonerror.InternalServerError() |
| 155 | return &res |
| 156 | } |
| 157 | |
| 158 | response := req.Form.Get("g-recaptcha-response") |
| 159 | if err := validateRecaptcha(cfg, response, clientIP); err != nil { |
no test coverage detected