106 lines
2.7 KiB
Go
106 lines
2.7 KiB
Go
package router
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"go.jolheiser.com/gistea/static"
|
|
|
|
"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"
|
|
)
|
|
|
|
type Config struct {
|
|
Domain string
|
|
SessionSecret string
|
|
|
|
GiteaURL string
|
|
GiteaClientKey string
|
|
GiteaClientSecret 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.Route("/", func(r chi.Router) {
|
|
r.Use(store.Middleware)
|
|
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
|
repos, err := store.Repos(r)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if err := static.Tmpl(w, static.Base, static.Context{
|
|
"repos": repos,
|
|
"page": static.New,
|
|
}); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
})
|
|
})
|
|
|
|
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
|
|
}
|