package router import ( "fmt" "net/http" "strings" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" "github.com/gorilla/sessions" "github.com/markbates/goth" "github.com/markbates/goth/gothic" "github.com/markbates/goth/providers/gitea" "github.com/rs/zerolog/log" "go.jolheiser.com/invitea/static" ) type Config struct { Domain string SessionSecret string DatabasePath string 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) goth.UseProviders( 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), ), ) gothStore := sessions.NewCookieStore([]byte(cfg.GiteaClientSecret)) gothStore.Options.HttpOnly = true gothic.Store = gothStore gothic.GetProviderName = func(_ *http.Request) (string, error) { return "gitea", nil } store := NewSessionStore(cfg.SessionSecret, cfg.GiteaURL) r := chi.NewMux() r.Use(middleware.Logger) r.Use(middleware.Recoverer) middleware.DefaultLogger = middleware.RequestLogger(&middleware.DefaultLogFormatter{ Logger: &log.Logger, NoColor: true, }) r.Mount("/css", http.FileServer(http.FS(static.CSS))) r.Route("/", func(r chi.Router) { r.Use(store.Middleware) r.Get("/", func(w http.ResponseWriter, r *http.Request) { var isAdmin bool if ia, ok := r.Context().Value("isAdmin").(bool); ok { isAdmin = ia } if err := static.Templates.ExecuteTemplate(w, "index.tmpl", map[string]any{ "isAdmin": isAdmin, }); err != nil { log.Err(err).Msg("") } }) }) r.Route("/auth", func(r chi.Router) { r.Get("/login", func(w http.ResponseWriter, r *http.Request) { user, err := gothic.CompleteUserAuth(w, r) if err != nil { gothic.BeginAuthHandler(w, r) return } 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) }) r.Get("/callback", func(w http.ResponseWriter, r *http.Request) { user, err := gothic.CompleteUserAuth(w, r) if err != nil { http.Error(w, err.Error(), http.StatusUnauthorized) return } 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) }) }) return r }