package server import ( "fmt" "github.com/go-session/session" "github.com/julienschmidt/httprouter" "net/http" "net/url" ) func (h *HttpServer) authorizeEndpoint(rw http.ResponseWriter, req *http.Request, _ httprouter.Params) { ss, err := session.Start(req.Context(), rw, req) if err != nil { http.Error(rw, "Failed to load session", http.StatusInternalServerError) return } userID, err := h.oauthSrv.UserAuthorizationHandler(rw, req) if err != nil { http.Error(rw, "Failed to check user", http.StatusInternalServerError) return } else if userID == "" { return } // function is only called with GET or POST method isPost := req.Method == http.MethodPost var form url.Values if isPost { err = req.ParseForm() if err != nil { http.Error(rw, "Failed to parse form", http.StatusInternalServerError) return } form = req.PostForm } else { form = req.URL.Query() } clientID := form.Get("client_id") client, err := h.oauthMgr.GetClient(req.Context(), clientID) if err != nil { http.Error(rw, "Invalid client", http.StatusBadRequest) return } redirectUri := form.Get("redirect_uri") if redirectUri != client.GetDomain() { http.Error(rw, "Incorrect redirect URI", http.StatusBadRequest) return } if form.Has("cancel") { uCancel, err := url.Parse(client.GetDomain()) if err != nil { http.Error(rw, "Invalid redirect URI", http.StatusBadRequest) return } q := uCancel.Query() q.Set("error", "access_denied") uCancel.RawQuery = q.Encode() http.Redirect(rw, req, uCancel.String(), http.StatusFound) return } var isSSO bool if clientIsSSO, ok := client.(interface{ IsSSO() bool }); ok { isSSO = clientIsSSO.IsSSO() } switch { case isSSO && isPost: http.Error(rw, "400 Bad Request", http.StatusBadRequest) return case !isSSO && !isPost: f := func(key string) string { return form.Get(key) } rw.WriteHeader(http.StatusOK) _, _ = fmt.Fprintf(rw, `