bloat/service/auth.go

489 lines
9.5 KiB
Go
Raw Normal View History

2019-12-13 18:08:26 +00:00
package service
import (
"errors"
2019-12-14 20:19:02 +00:00
"mime/multipart"
2020-01-01 15:58:27 +00:00
2020-02-01 11:31:44 +00:00
"bloat/mastodon"
2020-01-01 15:58:27 +00:00
"bloat/model"
2019-12-13 18:08:26 +00:00
)
var (
2020-01-28 17:51:00 +00:00
errInvalidSession = errors.New("invalid session")
errInvalidAccessToken = errors.New("invalid access token")
2020-01-28 17:51:00 +00:00
errInvalidCSRFToken = errors.New("invalid csrf token")
2019-12-13 18:08:26 +00:00
)
2020-01-28 17:51:00 +00:00
type as struct {
sessionRepo model.SessionRepo
appRepo model.AppRepo
2019-12-13 18:08:26 +00:00
Service
}
2020-01-28 17:51:00 +00:00
func NewAuthService(sessionRepo model.SessionRepo, appRepo model.AppRepo, s Service) Service {
return &as{sessionRepo, appRepo, s}
2019-12-13 18:08:26 +00:00
}
func (s *as) initClient(c *model.Client) (err error) {
2020-05-24 04:38:34 +00:00
if len(c.Ctx.SessionID) < 1 {
2020-01-28 17:51:00 +00:00
return errInvalidSession
2019-12-13 18:08:26 +00:00
}
2020-05-24 04:38:34 +00:00
session, err := s.sessionRepo.Get(c.Ctx.SessionID)
2019-12-13 18:08:26 +00:00
if err != nil {
2020-01-28 17:51:00 +00:00
return errInvalidSession
2019-12-13 18:08:26 +00:00
}
2020-05-24 04:38:34 +00:00
app, err := s.appRepo.Get(session.InstanceDomain)
2019-12-13 18:08:26 +00:00
if err != nil {
return
}
2019-12-21 11:13:21 +00:00
mc := mastodon.NewClient(&mastodon.Config{
2020-05-24 04:38:34 +00:00
Server: app.InstanceURL,
ClientID: app.ClientID,
ClientSecret: app.ClientSecret,
2019-12-13 18:08:26 +00:00
AccessToken: session.AccessToken,
})
2020-01-28 17:51:00 +00:00
c.Client = mc
c.Session = session
return nil
2019-12-13 18:08:26 +00:00
}
func (s *as) authenticateClient(c *model.Client) (err error) {
err = s.initClient(c)
if err != nil {
return
}
if len(c.Session.AccessToken) < 1 {
return errInvalidAccessToken
}
return nil
}
2020-05-24 04:38:34 +00:00
func checkCSRF(c *model.Client) (err error) {
if c.Ctx.CSRFToken != c.Session.CSRFToken {
2020-01-28 17:51:00 +00:00
return errInvalidCSRFToken
2020-01-25 10:07:06 +00:00
}
return nil
}
2020-05-24 04:38:34 +00:00
func (s *as) ServeErrorPage(c *model.Client, err error) {
s.authenticateClient(c)
s.Service.ServeErrorPage(c, err)
2019-12-13 18:08:26 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) ServeSigninPage(c *model.Client) (err error) {
return s.Service.ServeSigninPage(c)
2020-01-28 17:51:00 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) ServeRootPage(c *model.Client) (err error) {
err = s.authenticateClient(c)
2020-02-18 22:15:37 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.ServeRootPage(c)
2020-02-18 22:15:37 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) ServeNavPage(c *model.Client) (err error) {
err = s.authenticateClient(c)
2020-02-18 22:15:37 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.ServeNavPage(c)
2020-02-18 22:15:37 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) ServeTimelinePage(c *model.Client, tType string,
2020-01-28 17:51:00 +00:00
maxID string, minID string) (err error) {
2020-05-24 04:38:34 +00:00
err = s.authenticateClient(c)
2019-12-13 18:08:26 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.ServeTimelinePage(c, tType, maxID, minID)
2020-01-28 17:51:00 +00:00
}
2019-12-13 18:08:26 +00:00
2020-05-24 04:38:34 +00:00
func (s *as) ServeThreadPage(c *model.Client, id string, reply bool) (err error) {
err = s.authenticateClient(c)
2019-12-13 18:08:26 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.ServeThreadPage(c, id, reply)
2020-01-28 17:51:00 +00:00
}
2019-12-13 18:08:26 +00:00
2020-11-09 12:10:29 +00:00
func (s *as) ServeStatusPopup(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
return s.Service.ServeStatusPopup(c, id)
}
2020-05-24 04:38:34 +00:00
func (s *as) ServeLikedByPage(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
2019-12-13 18:08:26 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.ServeLikedByPage(c, id)
2019-12-13 18:08:26 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) ServeRetweetedByPage(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
2019-12-13 18:08:26 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.ServeRetweetedByPage(c, id)
2019-12-13 18:08:26 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) ServeNotificationPage(c *model.Client,
2020-01-28 17:51:00 +00:00
maxID string, minID string) (err error) {
2020-05-24 04:38:34 +00:00
err = s.authenticateClient(c)
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.ServeNotificationPage(c, maxID, minID)
}
2020-05-24 04:38:34 +00:00
func (s *as) ServeUserPage(c *model.Client, id string,
pageType string, maxID string, minID string) (err error) {
2020-05-24 04:38:34 +00:00
err = s.authenticateClient(c)
2019-12-21 05:48:48 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.ServeUserPage(c, id, pageType, maxID, minID)
2019-12-21 05:48:48 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) ServeAboutPage(c *model.Client) (err error) {
err = s.authenticateClient(c)
2019-12-22 18:10:42 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.ServeAboutPage(c)
2019-12-22 18:10:42 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) ServeEmojiPage(c *model.Client) (err error) {
err = s.authenticateClient(c)
2019-12-26 09:11:24 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.ServeEmojiPage(c)
2019-12-26 09:11:24 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) ServeSearchPage(c *model.Client, q string,
2020-01-28 17:51:00 +00:00
qType string, offset int) (err error) {
2020-05-24 04:38:34 +00:00
err = s.authenticateClient(c)
2019-12-26 09:11:24 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.ServeSearchPage(c, q, qType, offset)
2019-12-26 09:11:24 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) ServeUserSearchPage(c *model.Client,
2020-01-30 15:32:37 +00:00
id string, q string, offset int) (err error) {
2020-05-24 04:38:34 +00:00
err = s.authenticateClient(c)
2020-01-30 15:32:37 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.ServeUserSearchPage(c, id, q, offset)
2020-01-30 15:32:37 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) ServeSettingsPage(c *model.Client) (err error) {
err = s.authenticateClient(c)
2019-12-29 03:43:57 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.ServeSettingsPage(c)
2019-12-29 03:43:57 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) NewSession(instance string) (redirectUrl string,
2020-01-28 17:51:00 +00:00
sessionID string, err error) {
2020-05-24 04:38:34 +00:00
return s.Service.NewSession(instance)
2020-01-28 17:51:00 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) Signin(c *model.Client, sessionID string,
2020-02-02 08:30:40 +00:00
code string) (token string, userID string, err error) {
2020-05-24 04:38:34 +00:00
err = s.authenticateClient(c)
if err != nil && err != errInvalidAccessToken {
2019-12-29 03:43:57 +00:00
return
}
2020-05-24 04:38:34 +00:00
token, userID, err = s.Service.Signin(c, c.Session.ID, code)
2019-12-26 19:18:09 +00:00
if err != nil {
return
}
2020-01-28 17:51:00 +00:00
c.Session.AccessToken = token
2020-02-02 08:30:40 +00:00
c.Session.UserID = userID
2020-05-24 04:38:34 +00:00
2020-01-28 17:51:00 +00:00
err = s.sessionRepo.Add(c.Session)
2019-12-27 08:06:43 +00:00
if err != nil {
return
}
2020-01-28 17:51:00 +00:00
return
2019-12-27 08:06:43 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) Signout(c *model.Client) (err error) {
err = s.authenticateClient(c)
2020-03-04 15:59:59 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
err = checkCSRF(c)
2020-03-04 15:59:59 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
s.Service.Signout(c)
2020-03-04 15:59:59 +00:00
return
}
2020-05-24 04:38:34 +00:00
func (s *as) Post(c *model.Client, content string,
2020-01-28 17:51:00 +00:00
replyToID string, format string, visibility string, isNSFW bool,
files []*multipart.FileHeader) (id string, err error) {
2020-05-24 04:38:34 +00:00
err = s.authenticateClient(c)
2019-12-27 08:06:43 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
err = checkCSRF(c)
2020-01-25 10:07:06 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.Post(c, content, replyToID, format, visibility, isNSFW, files)
2019-12-27 08:06:43 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) Like(c *model.Client, id string) (count int64, err error) {
err = s.authenticateClient(c)
2019-12-13 18:08:26 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
err = checkCSRF(c)
2020-01-25 10:07:06 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.Like(c, id)
2019-12-13 18:08:26 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) UnLike(c *model.Client, id string) (count int64, err error) {
err = s.authenticateClient(c)
2019-12-13 18:08:26 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
err = checkCSRF(c)
2020-01-25 10:07:06 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.UnLike(c, id)
2019-12-13 18:08:26 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) Retweet(c *model.Client, id string) (count int64, err error) {
err = s.authenticateClient(c)
2019-12-13 18:08:26 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
err = checkCSRF(c)
2020-01-25 10:07:06 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.Retweet(c, id)
2019-12-13 18:08:26 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) UnRetweet(c *model.Client, id string) (count int64, err error) {
err = s.authenticateClient(c)
2019-12-13 18:08:26 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
err = checkCSRF(c)
2020-01-25 10:07:06 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.UnRetweet(c, id)
2019-12-13 18:08:26 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) Vote(c *model.Client, id string,
choices []string) (err error) {
2020-05-24 04:38:34 +00:00
err = s.authenticateClient(c)
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
err = checkCSRF(c)
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.Vote(c, id, choices)
}
2020-05-24 04:38:34 +00:00
func (s *as) Follow(c *model.Client, id string, reblogs *bool) (err error) {
err = s.authenticateClient(c)
2019-12-13 18:08:26 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
err = checkCSRF(c)
2020-01-25 10:07:06 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.Follow(c, id, reblogs)
2019-12-13 18:08:26 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) UnFollow(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
err = checkCSRF(c)
2020-01-25 10:07:06 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.UnFollow(c, id)
}
2020-05-24 04:38:34 +00:00
func (s *as) Mute(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
2020-02-08 10:49:06 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
err = checkCSRF(c)
2020-02-08 10:49:06 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.Mute(c, id)
2020-02-08 10:49:06 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) UnMute(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
2020-02-08 10:49:06 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
err = checkCSRF(c)
2020-02-08 10:49:06 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.UnMute(c, id)
2020-02-08 10:49:06 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) Block(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
2020-02-08 10:49:06 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
err = checkCSRF(c)
2020-02-08 10:49:06 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.Block(c, id)
2020-02-08 10:49:06 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) UnBlock(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
2020-02-08 10:49:06 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
err = checkCSRF(c)
2020-02-08 10:49:06 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.UnBlock(c, id)
2020-02-08 10:49:06 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) Subscribe(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
2020-04-17 17:19:11 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
err = checkCSRF(c)
2020-04-17 17:19:11 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.Subscribe(c, id)
2020-04-17 17:19:11 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) UnSubscribe(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
2020-04-17 17:19:11 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
err = checkCSRF(c)
2020-04-17 17:19:11 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.UnSubscribe(c, id)
2020-04-17 17:19:11 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) SaveSettings(c *model.Client, settings *model.Settings) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
err = checkCSRF(c)
2020-01-25 10:07:06 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.SaveSettings(c, settings)
}
2020-02-02 07:24:06 +00:00
2020-05-24 04:38:34 +00:00
func (s *as) MuteConversation(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
2020-02-02 07:24:06 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
err = checkCSRF(c)
2020-02-02 07:24:06 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.MuteConversation(c, id)
2020-02-02 07:24:06 +00:00
}
2020-05-24 04:38:34 +00:00
func (s *as) UnMuteConversation(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
2020-02-02 07:24:06 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
err = checkCSRF(c)
2020-02-02 07:24:06 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.UnMuteConversation(c, id)
2020-02-02 07:24:06 +00:00
}
2020-02-02 08:30:40 +00:00
2020-05-24 04:38:34 +00:00
func (s *as) Delete(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
2020-02-02 08:30:40 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
err = checkCSRF(c)
2020-02-02 08:30:40 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.Delete(c, id)
2020-02-02 08:30:40 +00:00
}
2020-02-18 22:15:37 +00:00
func (s *as) ReadNotifications(c *model.Client, maxID string) (err error) {
2020-05-24 04:38:34 +00:00
err = s.authenticateClient(c)
2020-02-18 22:15:37 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
err = checkCSRF(c)
2020-02-18 22:15:37 +00:00
if err != nil {
return
}
2020-05-24 04:38:34 +00:00
return s.Service.ReadNotifications(c, maxID)
2020-02-18 22:15:37 +00:00
}
func (s *as) Bookmark(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
err = checkCSRF(c)
if err != nil {
return
}
return s.Service.Bookmark(c, id)
}
func (s *as) UnBookmark(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
err = checkCSRF(c)
if err != nil {
return
}
return s.Service.UnBookmark(c, id)
}