bloat/service/auth.go

481 lines
9.3 KiB
Go
Raw Normal View History

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