65 lines
1.6 KiB
Go
65 lines
1.6 KiB
Go
package router
|
|
|
|
import (
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
"github.com/markbates/goth"
|
|
"github.com/markbates/goth/gothic"
|
|
"github.com/markbates/goth/providers/gitea"
|
|
"github.com/rs/zerolog/log"
|
|
"net/http"
|
|
)
|
|
|
|
func New() *chi.Mux {
|
|
// TODO pass in domain, strip ending slashes
|
|
domain := "https://git.jojodev.com"
|
|
clientKey := "18a5e9c3-fc3e-4d78-8564-74ec36531541"
|
|
clientSecret := "SZSd2QgciJL4wmPRxrVPnUwtrcGSn5nEI4QRh1aZM3JT"
|
|
callbackURL := "http://localhost:8080/auth/callback"
|
|
|
|
goth.UseProviders(
|
|
gitea.NewCustomisedURL(clientKey, clientSecret, callbackURL,
|
|
domain+"/login/oauth/authorize",
|
|
domain+"/login/oauth/access_token",
|
|
domain+"/api/v1/user",
|
|
),
|
|
)
|
|
gothic.GetProviderName = func(_ *http.Request) (string, error) {
|
|
return "gitea", nil
|
|
}
|
|
|
|
r := chi.NewMux()
|
|
r.Use(middleware.Logger)
|
|
r.Use(middleware.Recoverer)
|
|
|
|
middleware.DefaultLogger = middleware.RequestLogger(&middleware.DefaultLogFormatter{
|
|
Logger: &log.Logger,
|
|
NoColor: true,
|
|
})
|
|
|
|
r.Route("/auth", func(r chi.Router) {
|
|
r.Get("/login", func(w http.ResponseWriter, r *http.Request) {
|
|
_, err := gothic.CompleteUserAuth(w, r)
|
|
if err != nil {
|
|
gothic.BeginAuthHandler(w, r)
|
|
return
|
|
}
|
|
})
|
|
r.Get("/callback", func(w http.ResponseWriter, r *http.Request) {
|
|
_, err := gothic.CompleteUserAuth(w, r)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusUnauthorized)
|
|
return
|
|
}
|
|
})
|
|
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
|
|
})
|
|
})
|
|
|
|
return r
|
|
}
|