1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
  2//
  3// SPDX-License-Identifier: Apache-2.0
  4
  5package ws
  6
  7import (
  8	"database/sql"
  9	"embed"
 10	"fmt"
 11	"io"
 12	"net/http"
 13	"net/url"
 14	"strings"
 15	"sync"
 16	"text/template"
 17	"time"
 18
 19	"git.sr.ht/~amolith/willow/project"
 20	"git.sr.ht/~amolith/willow/users"
 21	"github.com/microcosm-cc/bluemonday"
 22)
 23
 24type Handler struct {
 25	DbConn        *sql.DB
 26	Req           *chan struct{}
 27	ManualRefresh *chan struct{}
 28	Res           *chan []project.Project
 29	Mu            *sync.Mutex
 30	Version       *string
 31}
 32
 33//go:embed static
 34var fs embed.FS
 35
 36var bmStrict = bluemonday.StrictPolicy()
 37
 38func (h Handler) RootHandler(w http.ResponseWriter, r *http.Request) {
 39	if !h.isAuthorised(r) {
 40		http.Redirect(w, r, "/login", http.StatusSeeOther)
 41		return
 42	}
 43	projectsWithReleases, err := project.GetProjectsWithReleases(h.DbConn, h.Mu)
 44	if err != nil {
 45		fmt.Println(err)
 46		w.WriteHeader(http.StatusInternalServerError)
 47		_, err := w.Write([]byte("Internal Server Error"))
 48		if err != nil {
 49			fmt.Println(err)
 50		}
 51		return
 52	}
 53
 54	data := struct {
 55		Version     string
 56		Projects    []project.Project
 57		IsDashboard bool
 58	}{
 59		Version:     *h.Version,
 60		Projects:    projectsWithReleases,
 61		IsDashboard: true,
 62	}
 63
 64	tmpl := template.Must(template.ParseFS(fs, "static/dashboard.html.tmpl", "static/head.html.tmpl", "static/header.html.tmpl", "static/footer.html.tmpl"))
 65	if err := tmpl.Execute(w, data); err != nil {
 66		fmt.Println(err)
 67	}
 68}
 69
 70func (h Handler) NewHandler(w http.ResponseWriter, r *http.Request) {
 71	if !h.isAuthorised(r) {
 72		http.Redirect(w, r, "/login", http.StatusSeeOther)
 73		return
 74	}
 75	params := r.URL.Query()
 76	action := bmStrict.Sanitize(params.Get("action"))
 77	if r.Method == http.MethodGet {
 78		switch action {
 79		case "":
 80			data := struct{ Version string }{Version: *h.Version}
 81			tmpl := template.Must(template.ParseFS(fs, "static/new.html.tmpl", "static/head.html.tmpl", "static/header.html.tmpl", "static/footer.html.tmpl"))
 82			if err := tmpl.Execute(w, data); err != nil {
 83				fmt.Println(err)
 84			}
 85		case "delete":
 86			submittedID := params.Get("id")
 87			if submittedID == "" {
 88				w.WriteHeader(http.StatusBadRequest)
 89				_, err := w.Write([]byte("No URL provided"))
 90				if err != nil {
 91					fmt.Println(err)
 92				}
 93				return
 94			}
 95
 96			project.Untrack(h.DbConn, h.Mu, submittedID)
 97			http.Redirect(w, r, "/", http.StatusSeeOther)
 98		default:
 99			submittedURL := bmStrict.Sanitize(params.Get("url"))
100			if submittedURL == "" {
101				w.WriteHeader(http.StatusBadRequest)
102				_, err := w.Write([]byte("No URL provided"))
103				if err != nil {
104					fmt.Println(err)
105				}
106				return
107			}
108
109			forge := bmStrict.Sanitize(params.Get("forge"))
110			if forge == "" {
111				w.WriteHeader(http.StatusBadRequest)
112				_, err := w.Write([]byte("No forge provided"))
113				if err != nil {
114					fmt.Println(err)
115				}
116				return
117			}
118
119			name := bmStrict.Sanitize(params.Get("name"))
120			if name == "" {
121				w.WriteHeader(http.StatusBadRequest)
122				_, err := w.Write([]byte("No name provided"))
123				if err != nil {
124					fmt.Println(err)
125				}
126				return
127			}
128
129			proj := project.Project{
130				ID:    project.GenProjectID(submittedURL, name, forge),
131				URL:   submittedURL,
132				Name:  name,
133				Forge: forge,
134			}
135
136			proj, err := project.GetProject(h.DbConn, proj)
137			if err != nil && err != sql.ErrNoRows {
138				w.WriteHeader(http.StatusBadRequest)
139				_, err := w.Write([]byte(fmt.Sprintf("Error getting project: %s", err)))
140				if err != nil {
141					fmt.Println(err)
142				}
143				return
144			}
145
146			proj, err = project.GetReleases(h.DbConn, h.Mu, proj)
147			if err != nil {
148				w.WriteHeader(http.StatusBadRequest)
149				_, err := w.Write([]byte(fmt.Sprintf("Error getting releases: %s", err)))
150				if err != nil {
151					fmt.Println(err)
152				}
153				return
154			}
155
156			data := struct {
157				Version string
158				Project project.Project
159			}{
160				Version: *h.Version,
161				Project: proj,
162			}
163
164			tmpl := template.Must(template.ParseFS(fs, "static/select-release.html.tmpl", "static/head.html.tmpl", "static/header.html.tmpl", "static/footer.html.tmpl"))
165			if err := tmpl.Execute(w, data); err != nil {
166				fmt.Println(err)
167			}
168		}
169	}
170
171	if r.Method == http.MethodPost {
172		err := r.ParseForm()
173		if err != nil {
174			fmt.Println(err)
175		}
176		idValue := bmStrict.Sanitize(r.FormValue("id"))
177		nameValue := bmStrict.Sanitize(r.FormValue("name"))
178		urlValue := bmStrict.Sanitize(r.FormValue("url"))
179		forgeValue := bmStrict.Sanitize(r.FormValue("forge"))
180		releaseValue := bmStrict.Sanitize(r.FormValue("release"))
181
182		// If releaseValue is not empty, we're updating an existing project
183		if idValue != "" && nameValue != "" && urlValue != "" && forgeValue != "" && releaseValue != "" {
184			project.Track(h.DbConn, h.Mu, h.ManualRefresh, nameValue, urlValue, forgeValue, releaseValue)
185			http.Redirect(w, r, "/", http.StatusSeeOther)
186			return
187		}
188
189		// If releaseValue is empty, we're creating a new project
190		if idValue == "" && nameValue != "" && urlValue != "" && forgeValue != "" && releaseValue == "" {
191			http.Redirect(w, r, "/new?action=yoink&name="+url.QueryEscape(nameValue)+"&url="+url.QueryEscape(urlValue)+"&forge="+url.QueryEscape(forgeValue), http.StatusSeeOther)
192			return
193		}
194
195		w.WriteHeader(http.StatusBadRequest)
196		_, err = w.Write([]byte("No data provided"))
197		if err != nil {
198			fmt.Println(err)
199		}
200	}
201}
202
203func (h Handler) LoginHandler(w http.ResponseWriter, r *http.Request) {
204	if r.Method == http.MethodGet {
205		if h.isAuthorised(r) {
206			http.Redirect(w, r, "/", http.StatusSeeOther)
207			return
208		}
209
210		data := struct {
211			Version string
212		}{
213			Version: *h.Version,
214		}
215		tmpl := template.Must(template.ParseFS(fs, "static/login.html.tmpl", "static/head.html.tmpl", "static/footer.html.tmpl"))
216		if err := tmpl.Execute(w, data); err != nil {
217			fmt.Println(err)
218		}
219	}
220
221	if r.Method == http.MethodPost {
222		err := r.ParseForm()
223		if err != nil {
224			fmt.Println(err)
225		}
226		username := bmStrict.Sanitize(r.FormValue("username"))
227		password := bmStrict.Sanitize(r.FormValue("password"))
228
229		if username == "" || password == "" {
230			w.WriteHeader(http.StatusBadRequest)
231			_, err := w.Write([]byte("No data provided"))
232			if err != nil {
233				fmt.Println(err)
234			}
235			return
236		}
237
238		authorised, err := users.UserAuthorised(h.DbConn, username, password)
239		if err != nil {
240			w.WriteHeader(http.StatusBadRequest)
241			_, err := w.Write([]byte(fmt.Sprintf("Error logging in: %s", err)))
242			if err != nil {
243				fmt.Println(err)
244			}
245			return
246		}
247
248		if !authorised {
249			w.WriteHeader(http.StatusUnauthorized)
250			_, err := w.Write([]byte("Incorrect username or password"))
251			if err != nil {
252				fmt.Println(err)
253			}
254			return
255		}
256
257		session, expiry, err := users.CreateSession(h.DbConn, username)
258		if err != nil {
259			w.WriteHeader(http.StatusBadRequest)
260			_, err := w.Write([]byte(fmt.Sprintf("Error creating session: %s", err)))
261			if err != nil {
262				fmt.Println(err)
263			}
264			return
265		}
266
267		maxAge := int(time.Until(expiry))
268
269		cookie := http.Cookie{
270			Name:     "id",
271			Value:    session,
272			MaxAge:   maxAge,
273			HttpOnly: true,
274			SameSite: http.SameSiteStrictMode,
275			Secure:   true,
276		}
277
278		http.SetCookie(w, &cookie)
279		http.Redirect(w, r, "/", http.StatusSeeOther)
280	}
281}
282
283func (h Handler) LogoutHandler(w http.ResponseWriter, r *http.Request) {
284	cookie, err := r.Cookie("id")
285	if err != nil {
286		fmt.Println(err)
287	}
288
289	err = users.InvalidateSession(h.DbConn, cookie.Value)
290	if err != nil {
291		fmt.Println(err)
292		_, err = w.Write([]byte(fmt.Sprintf("Error logging out: %s", err)))
293		if err != nil {
294			fmt.Println(err)
295		}
296		return
297	}
298	cookie.MaxAge = -1
299	http.SetCookie(w, cookie)
300	http.Redirect(w, r, "/login", http.StatusSeeOther)
301}
302
303// isAuthorised makes a database request to the sessions table to see if the
304// user has a valid session cookie.
305func (h Handler) isAuthorised(r *http.Request) bool {
306	cookie, err := r.Cookie("id")
307	if err != nil {
308		return false
309	}
310
311	authorised, err := users.SessionAuthorised(h.DbConn, cookie.Value)
312	if err != nil {
313		fmt.Println("Error checking session:", err)
314		return false
315	}
316
317	return authorised
318}
319
320func StaticHandler(writer http.ResponseWriter, request *http.Request) {
321	resource := strings.TrimPrefix(request.URL.Path, "/")
322	if strings.HasSuffix(resource, ".css") {
323		writer.Header().Set("Content-Type", "text/css")
324	} else if strings.HasSuffix(resource, ".js") {
325		writer.Header().Set("Content-Type", "text/javascript")
326	}
327	home, err := fs.ReadFile(resource)
328	if err != nil {
329		fmt.Println(err)
330	}
331	if _, err = io.Writer.Write(writer, home); err != nil {
332		fmt.Println(err)
333	}
334}