2022-03-11 18:03:01 +00:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2022-03-12 04:34:27 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2023-03-03 05:51:53 +00:00
|
|
|
"go.jolheiser.com/invitea/database"
|
|
|
|
"go.jolheiser.com/invitea/static"
|
|
|
|
|
2023-03-04 06:02:24 +00:00
|
|
|
sdk "code.gitea.io/sdk/gitea"
|
2022-03-11 18:03:01 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
2022-03-12 04:34:27 +00:00
|
|
|
"github.com/gorilla/sessions"
|
2022-03-11 18:03:01 +00:00
|
|
|
"github.com/markbates/goth"
|
|
|
|
"github.com/markbates/goth/gothic"
|
|
|
|
"github.com/markbates/goth/providers/gitea"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
2022-03-12 04:34:27 +00:00
|
|
|
type Config struct {
|
|
|
|
Domain string
|
|
|
|
SessionSecret string
|
2023-03-03 05:51:53 +00:00
|
|
|
Database *database.Queries
|
2023-03-04 06:02:24 +00:00
|
|
|
GiteaClient *sdk.Client
|
2022-03-12 04:34:27 +00:00
|
|
|
|
|
|
|
GiteaURL string
|
|
|
|
GiteaClientKey string
|
|
|
|
GiteaClientSecret string
|
|
|
|
GiteaToken string
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(cfg Config) *chi.Mux {
|
|
|
|
cfg.GiteaURL = strings.TrimRight(cfg.GiteaURL, "/")
|
|
|
|
cfg.Domain = strings.TrimRight(cfg.Domain, "/")
|
|
|
|
callbackURL := fmt.Sprintf("%s/auth/callback", cfg.Domain)
|
2022-03-11 18:03:01 +00:00
|
|
|
|
|
|
|
goth.UseProviders(
|
2022-03-12 04:34:27 +00:00
|
|
|
gitea.NewCustomisedURL(cfg.GiteaClientKey, cfg.GiteaClientSecret, callbackURL,
|
|
|
|
fmt.Sprintf("%s/login/oauth/authorize", cfg.GiteaURL),
|
|
|
|
fmt.Sprintf("%s/login/oauth/access_token", cfg.GiteaURL),
|
|
|
|
fmt.Sprintf("%s/api/v1/user", cfg.GiteaURL),
|
2022-03-11 18:03:01 +00:00
|
|
|
),
|
|
|
|
)
|
2022-03-12 04:34:27 +00:00
|
|
|
gothStore := sessions.NewCookieStore([]byte(cfg.GiteaClientSecret))
|
|
|
|
gothStore.Options.HttpOnly = true
|
|
|
|
gothic.Store = gothStore
|
2022-03-11 18:03:01 +00:00
|
|
|
gothic.GetProviderName = func(_ *http.Request) (string, error) {
|
|
|
|
return "gitea", nil
|
|
|
|
}
|
|
|
|
|
2022-03-12 04:34:27 +00:00
|
|
|
store := NewSessionStore(cfg.SessionSecret, cfg.GiteaURL)
|
|
|
|
|
2023-03-03 05:51:53 +00:00
|
|
|
routes := Routes{
|
2023-03-04 06:02:24 +00:00
|
|
|
DB: cfg.Database,
|
|
|
|
Gitea: cfg.GiteaClient,
|
|
|
|
GiteaURL: cfg.GiteaURL,
|
2023-03-03 05:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mux := chi.NewMux()
|
|
|
|
mux.Use(middleware.Logger)
|
|
|
|
mux.Use(middleware.Recoverer)
|
2022-03-11 18:03:01 +00:00
|
|
|
|
|
|
|
middleware.DefaultLogger = middleware.RequestLogger(&middleware.DefaultLogFormatter{
|
|
|
|
Logger: &log.Logger,
|
|
|
|
NoColor: true,
|
|
|
|
})
|
|
|
|
|
2023-03-03 05:51:53 +00:00
|
|
|
mux.Mount("/css", http.FileServer(http.FS(static.CSS)))
|
2023-03-04 06:29:56 +00:00
|
|
|
mux.Mount("/img", http.FileServer(http.FS(static.Img)))
|
2023-03-01 04:47:18 +00:00
|
|
|
|
2023-03-03 05:51:53 +00:00
|
|
|
mux.Route("/", func(r chi.Router) {
|
2022-03-12 04:34:27 +00:00
|
|
|
r.Use(store.Middleware)
|
2023-03-03 05:51:53 +00:00
|
|
|
r.Get("/", routes.Index)
|
2023-03-04 06:02:24 +00:00
|
|
|
r.With(store.RequireAuth).Post("/", routes.IndexPost)
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.Route("/invite/{code}", func(r chi.Router) {
|
|
|
|
r.Get("/", routes.Invite)
|
|
|
|
r.Post("/", routes.InvitePost)
|
2022-03-12 04:34:27 +00:00
|
|
|
})
|
|
|
|
|
2023-03-03 05:51:53 +00:00
|
|
|
mux.Route("/auth", func(r chi.Router) {
|
2022-03-11 18:03:01 +00:00
|
|
|
r.Get("/login", func(w http.ResponseWriter, r *http.Request) {
|
2022-03-12 04:34:27 +00:00
|
|
|
user, err := gothic.CompleteUserAuth(w, r)
|
2022-03-11 18:03:01 +00:00
|
|
|
if err != nil {
|
|
|
|
gothic.BeginAuthHandler(w, r)
|
|
|
|
return
|
|
|
|
}
|
2022-03-12 04:34:27 +00:00
|
|
|
|
|
|
|
if err := store.Auth(w, r, user.AccessToken); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, cfg.Domain, http.StatusFound)
|
2022-03-11 18:03:01 +00:00
|
|
|
})
|
2023-03-04 06:02:24 +00:00
|
|
|
r.Get("/logout", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if err := gothic.Logout(w, r); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := store.Logout(w, r); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, cfg.Domain, http.StatusFound)
|
|
|
|
})
|
2022-03-11 18:03:01 +00:00
|
|
|
r.Get("/callback", func(w http.ResponseWriter, r *http.Request) {
|
2022-03-12 04:34:27 +00:00
|
|
|
user, err := gothic.CompleteUserAuth(w, r)
|
2022-03-11 18:03:01 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
2022-03-12 04:34:27 +00:00
|
|
|
if err := store.Auth(w, r, user.AccessToken); err != nil {
|
2022-03-11 18:03:01 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2022-03-12 04:34:27 +00:00
|
|
|
return
|
2022-03-11 18:03:01 +00:00
|
|
|
}
|
2022-03-12 04:34:27 +00:00
|
|
|
http.Redirect(w, r, cfg.Domain, http.StatusFound)
|
2022-03-11 18:03:01 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-03-03 05:51:53 +00:00
|
|
|
return mux
|
2022-03-11 18:03:01 +00:00
|
|
|
}
|