2022-05-02 08:19:37 -07:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2022-05-17 13:35:26 -07:00
|
|
|
"os"
|
2022-05-02 08:19:37 -07:00
|
|
|
|
2023-08-17 09:49:32 -07:00
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/common"
|
2023-06-03 07:18:47 -07:00
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/db"
|
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/log"
|
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/server"
|
2022-05-04 07:27:16 -07:00
|
|
|
"emperror.dev/errors"
|
2022-05-02 08:19:37 -07:00
|
|
|
"github.com/go-chi/chi/v5"
|
2022-05-04 07:27:16 -07:00
|
|
|
"github.com/go-chi/render"
|
2022-06-17 06:18:44 -07:00
|
|
|
"github.com/rs/xid"
|
2022-05-02 08:19:37 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
*server.Server
|
2022-05-17 13:35:26 -07:00
|
|
|
|
|
|
|
RequireInvite bool
|
2023-03-16 03:43:25 -07:00
|
|
|
BaseURL string
|
2023-04-24 07:51:55 -07:00
|
|
|
|
|
|
|
hcaptchaSitekey string
|
|
|
|
hcaptchaSecret string
|
2022-05-02 08:19:37 -07:00
|
|
|
}
|
|
|
|
|
2022-06-17 06:18:44 -07:00
|
|
|
type userResponse struct {
|
2023-03-11 16:31:31 -08:00
|
|
|
ID xid.ID `json:"id"`
|
2023-08-17 09:49:32 -07:00
|
|
|
SnowflakeID common.UserID `json:"id_new"`
|
2023-03-11 16:31:31 -08:00
|
|
|
Username string `json:"name"`
|
|
|
|
DisplayName *string `json:"display_name"`
|
|
|
|
Bio *string `json:"bio"`
|
2023-03-12 18:04:09 -07:00
|
|
|
Avatar *string `json:"avatar"`
|
2023-03-11 16:31:31 -08:00
|
|
|
Links []string `json:"links"`
|
|
|
|
Names []db.FieldEntry `json:"names"`
|
|
|
|
Pronouns []db.PronounEntry `json:"pronouns"`
|
|
|
|
Fields []db.Field `json:"fields"`
|
2022-06-17 06:18:44 -07:00
|
|
|
|
|
|
|
Discord *string `json:"discord"`
|
|
|
|
DiscordUsername *string `json:"discord_username"`
|
2023-03-18 07:19:53 -07:00
|
|
|
|
2023-04-17 18:49:37 -07:00
|
|
|
Tumblr *string `json:"tumblr"`
|
|
|
|
TumblrUsername *string `json:"tumblr_username"`
|
|
|
|
|
2023-04-18 13:52:58 -07:00
|
|
|
Google *string `json:"google"`
|
|
|
|
GoogleUsername *string `json:"google_username"`
|
|
|
|
|
2023-03-18 07:19:53 -07:00
|
|
|
Fediverse *string `json:"fediverse"`
|
|
|
|
FediverseUsername *string `json:"fediverse_username"`
|
|
|
|
FediverseInstance *string `json:"fediverse_instance"`
|
2022-06-17 06:18:44 -07:00
|
|
|
}
|
|
|
|
|
2023-03-11 16:31:31 -08:00
|
|
|
func dbUserToUserResponse(u db.User, fields []db.Field) *userResponse {
|
2022-06-17 06:18:44 -07:00
|
|
|
return &userResponse{
|
2023-03-18 07:19:53 -07:00
|
|
|
ID: u.ID,
|
2023-08-17 09:49:32 -07:00
|
|
|
SnowflakeID: u.SnowflakeID,
|
2023-03-18 07:19:53 -07:00
|
|
|
Username: u.Username,
|
|
|
|
DisplayName: u.DisplayName,
|
|
|
|
Bio: u.Bio,
|
|
|
|
Avatar: u.Avatar,
|
|
|
|
Links: db.NotNull(u.Links),
|
|
|
|
Names: db.NotNull(u.Names),
|
|
|
|
Pronouns: db.NotNull(u.Pronouns),
|
|
|
|
Fields: db.NotNull(fields),
|
|
|
|
Discord: u.Discord,
|
|
|
|
DiscordUsername: u.DiscordUsername,
|
2023-04-17 18:49:37 -07:00
|
|
|
Tumblr: u.Tumblr,
|
|
|
|
TumblrUsername: u.TumblrUsername,
|
2023-04-18 13:52:58 -07:00
|
|
|
Google: u.Google,
|
|
|
|
GoogleUsername: u.GoogleUsername,
|
2023-03-18 07:19:53 -07:00
|
|
|
Fediverse: u.Fediverse,
|
|
|
|
FediverseUsername: u.FediverseUsername,
|
|
|
|
FediverseInstance: u.FediverseInstance,
|
2022-06-17 06:18:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-02 08:19:37 -07:00
|
|
|
func Mount(srv *server.Server, r chi.Router) {
|
2022-05-17 13:35:26 -07:00
|
|
|
s := &Server{
|
2023-04-24 07:51:55 -07:00
|
|
|
Server: srv,
|
|
|
|
RequireInvite: os.Getenv("REQUIRE_INVITE") == "true",
|
|
|
|
BaseURL: os.Getenv("BASE_URL"),
|
|
|
|
hcaptchaSitekey: os.Getenv("HCAPTCHA_SITEKEY"),
|
|
|
|
hcaptchaSecret: os.Getenv("HCAPTCHA_SECRET"),
|
2022-05-17 13:35:26 -07:00
|
|
|
}
|
2022-05-02 08:19:37 -07:00
|
|
|
|
2022-05-04 07:27:16 -07:00
|
|
|
r.Route("/auth", func(r chi.Router) {
|
2022-05-17 13:35:26 -07:00
|
|
|
// check if username is taken
|
|
|
|
r.Get("/username", server.WrapHandler(s.usernameTaken))
|
|
|
|
|
2022-05-04 07:27:16 -07:00
|
|
|
// generate csrf token, returns all supported OAuth provider URLs
|
2022-05-12 07:41:32 -07:00
|
|
|
r.Post("/urls", server.WrapHandler(s.oauthURLs))
|
2023-03-16 03:43:25 -07:00
|
|
|
r.Get("/urls/fediverse", server.WrapHandler(s.getFediverseURL))
|
2022-05-02 08:19:37 -07:00
|
|
|
|
2022-05-04 07:27:16 -07:00
|
|
|
r.Route("/discord", func(r chi.Router) {
|
|
|
|
// takes code + state, validates it, returns token OR discord signup ticket
|
2022-05-12 07:41:32 -07:00
|
|
|
r.Post("/callback", server.WrapHandler(s.discordCallback))
|
2022-05-04 07:27:16 -07:00
|
|
|
// takes discord signup ticket to register account
|
2022-11-17 17:17:27 -08:00
|
|
|
r.Post("/signup", server.WrapHandler(s.discordSignup))
|
2023-03-16 03:43:25 -07:00
|
|
|
// takes discord signup ticket to link to existing account
|
2023-03-18 08:33:12 -07:00
|
|
|
r.With(server.MustAuth).Post("/add-provider", server.WrapHandler(s.discordLink))
|
2023-03-18 08:54:31 -07:00
|
|
|
// removes discord link from existing account
|
|
|
|
r.With(server.MustAuth).Post("/remove-provider", server.WrapHandler(s.discordUnlink))
|
2023-03-16 03:43:25 -07:00
|
|
|
})
|
|
|
|
|
2023-04-17 18:49:37 -07:00
|
|
|
r.Route("/tumblr", func(r chi.Router) {
|
|
|
|
r.Post("/callback", server.WrapHandler(s.tumblrCallback))
|
|
|
|
r.Post("/signup", server.WrapHandler(s.tumblrSignup))
|
|
|
|
r.With(server.MustAuth).Post("/add-provider", server.WrapHandler(s.tumblrLink))
|
|
|
|
r.With(server.MustAuth).Post("/remove-provider", server.WrapHandler(s.tumblrUnlink))
|
|
|
|
})
|
|
|
|
|
2023-04-18 13:52:58 -07:00
|
|
|
r.Route("/google", func(r chi.Router) {
|
|
|
|
r.Post("/callback", server.WrapHandler(s.googleCallback))
|
|
|
|
r.Post("/signup", server.WrapHandler(s.googleSignup))
|
|
|
|
r.With(server.MustAuth).Post("/add-provider", server.WrapHandler(s.googleLink))
|
|
|
|
r.With(server.MustAuth).Post("/remove-provider", server.WrapHandler(s.googleUnlink))
|
|
|
|
})
|
|
|
|
|
2023-03-16 03:43:25 -07:00
|
|
|
r.Route("/mastodon", func(r chi.Router) {
|
2023-03-16 07:50:39 -07:00
|
|
|
r.Post("/callback", server.WrapHandler(s.mastodonCallback))
|
|
|
|
r.Post("/signup", server.WrapHandler(s.mastodonSignup))
|
2023-03-18 07:19:53 -07:00
|
|
|
r.With(server.MustAuth).Post("/add-provider", server.WrapHandler(s.mastodonLink))
|
2023-03-18 08:54:31 -07:00
|
|
|
r.With(server.MustAuth).Post("/remove-provider", server.WrapHandler(s.mastodonUnlink))
|
2022-05-02 08:19:37 -07:00
|
|
|
})
|
2022-11-18 06:27:52 -08:00
|
|
|
|
2023-03-24 19:27:40 -07:00
|
|
|
r.Route("/misskey", func(r chi.Router) {
|
|
|
|
r.Post("/callback", server.WrapHandler(s.misskeyCallback))
|
|
|
|
r.Post("/signup", server.WrapHandler(s.misskeySignup))
|
|
|
|
r.With(server.MustAuth).Post("/add-provider", server.WrapHandler(s.misskeyLink))
|
|
|
|
})
|
|
|
|
|
2022-11-18 06:27:52 -08:00
|
|
|
// invite routes
|
|
|
|
r.With(server.MustAuth).Get("/invites", server.WrapHandler(s.getInvites))
|
|
|
|
r.With(server.MustAuth).Post("/invites", server.WrapHandler(s.createInvite))
|
2022-12-31 15:34:38 -08:00
|
|
|
|
|
|
|
// tokens
|
|
|
|
r.With(server.MustAuth).Get("/tokens", server.WrapHandler(s.getTokens))
|
|
|
|
r.With(server.MustAuth).Post("/tokens", server.WrapHandler(s.createToken))
|
2023-03-30 07:05:10 -07:00
|
|
|
r.With(server.MustAuth).Delete("/tokens", server.WrapHandler(s.deleteToken))
|
2023-03-14 08:16:07 -07:00
|
|
|
|
|
|
|
// cancel user delete
|
|
|
|
// uses a special token, so handled in the function itself
|
|
|
|
r.Get("/cancel-delete", server.WrapHandler(s.cancelDelete))
|
2023-03-20 07:04:32 -07:00
|
|
|
// force user delete
|
|
|
|
// uses a special token (same as above)
|
|
|
|
r.Get("/force-delete", server.WrapHandler(s.forceDelete))
|
2022-05-02 08:19:37 -07:00
|
|
|
})
|
|
|
|
}
|
2022-05-04 07:27:16 -07:00
|
|
|
|
|
|
|
type oauthURLsRequest struct {
|
2022-05-12 07:41:32 -07:00
|
|
|
CallbackDomain string `json:"callback_domain"`
|
2022-05-04 07:27:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type oauthURLsResponse struct {
|
2023-04-18 14:31:57 -07:00
|
|
|
Discord string `json:"discord,omitempty"`
|
|
|
|
Tumblr string `json:"tumblr,omitempty"`
|
|
|
|
Google string `json:"google,omitempty"`
|
2022-05-04 07:27:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) oauthURLs(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
req, err := Decode[oauthURLsRequest](r)
|
|
|
|
if err != nil {
|
2022-05-12 07:41:32 -07:00
|
|
|
log.Error(err)
|
|
|
|
|
2022-05-04 07:27:16 -07:00
|
|
|
return server.APIError{Code: server.ErrBadRequest}
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate CSRF state
|
|
|
|
state, err := s.setCSRFState(r.Context())
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "setting CSRF state")
|
|
|
|
}
|
2023-04-18 14:31:57 -07:00
|
|
|
var resp oauthURLsResponse
|
2022-05-04 07:27:16 -07:00
|
|
|
|
2023-04-18 14:31:57 -07:00
|
|
|
if discordOAuthConfig.ClientID != "" {
|
|
|
|
discordCfg := discordOAuthConfig
|
|
|
|
discordCfg.RedirectURL = req.CallbackDomain + "/auth/login/discord"
|
|
|
|
resp.Discord = discordCfg.AuthCodeURL(state) + "&prompt=none"
|
|
|
|
}
|
|
|
|
if tumblrOAuthConfig.ClientID != "" {
|
|
|
|
tumblrCfg := tumblrOAuthConfig
|
|
|
|
tumblrCfg.RedirectURL = req.CallbackDomain + "/auth/login/tumblr"
|
|
|
|
resp.Tumblr = tumblrCfg.AuthCodeURL(state)
|
|
|
|
}
|
|
|
|
if googleOAuthConfig.ClientID != "" {
|
|
|
|
googleCfg := googleOAuthConfig
|
|
|
|
googleCfg.RedirectURL = req.CallbackDomain + "/auth/login/google"
|
2023-12-29 19:41:22 -08:00
|
|
|
resp.Google = googleCfg.AuthCodeURL(state) + "&prompt=select_account"
|
2023-04-18 14:31:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
render.JSON(w, r, resp)
|
2022-05-04 07:27:16 -07:00
|
|
|
return nil
|
|
|
|
}
|
2022-05-17 13:35:26 -07:00
|
|
|
|
|
|
|
func (s *Server) usernameTaken(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
type Response struct {
|
|
|
|
Valid bool `json:"valid"`
|
|
|
|
Taken bool `json:"taken"`
|
|
|
|
}
|
|
|
|
|
|
|
|
name := r.FormValue("username")
|
|
|
|
if name == "" {
|
|
|
|
render.JSON(w, r, Response{
|
|
|
|
Valid: false,
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
valid, taken, err := s.DB.UsernameTaken(r.Context(), name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
render.JSON(w, r, Response{
|
|
|
|
Valid: valid,
|
|
|
|
Taken: taken,
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|