2019-12-13 20:08:26 +02:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2019-12-14 22:19:02 +02:00
|
|
|
"mime/multipart"
|
2020-01-01 17:58:27 +02:00
|
|
|
|
|
|
|
"bloat/model"
|
|
|
|
"mastodon"
|
2019-12-13 20:08:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-01-28 19:51:00 +02:00
|
|
|
errInvalidSession = errors.New("invalid session")
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
func (s *as) authenticateClient(ctx context.Context, c *model.Client) (err error) {
|
2019-12-13 20:08:26 +02:00
|
|
|
sessionID, ok := ctx.Value("session_id").(string)
|
|
|
|
if !ok || len(sessionID) < 1 {
|
2020-01-28 19:51:00 +02:00
|
|
|
return errInvalidSession
|
2019-12-13 20:08:26 +02:00
|
|
|
}
|
|
|
|
session, err := s.sessionRepo.Get(sessionID)
|
|
|
|
if err != nil {
|
2020-01-28 19:51:00 +02:00
|
|
|
return errInvalidSession
|
2019-12-13 20:08:26 +02:00
|
|
|
}
|
2019-12-17 22:17:25 +02:00
|
|
|
client, 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{
|
2019-12-17 22:17:25 +02:00
|
|
|
Server: client.InstanceURL,
|
2019-12-13 20:08:26 +02:00
|
|
|
ClientID: client.ClientID,
|
|
|
|
ClientSecret: client.ClientSecret,
|
|
|
|
AccessToken: session.AccessToken,
|
|
|
|
})
|
2020-01-28 19:51:00 +02:00
|
|
|
if c == nil {
|
|
|
|
c = &model.Client{}
|
|
|
|
}
|
|
|
|
c.Client = mc
|
|
|
|
c.Session = session
|
|
|
|
return nil
|
2019-12-13 20:08:26 +02:00
|
|
|
}
|
|
|
|
|
2020-01-25 12:07:06 +02:00
|
|
|
func checkCSRF(ctx context.Context, c *model.Client) (err error) {
|
2020-01-28 19:51:00 +02:00
|
|
|
token, ok := ctx.Value("csrf_token").(string)
|
|
|
|
if !ok || token != c.Session.CSRFToken {
|
|
|
|
return errInvalidCSRFToken
|
2020-01-25 12:07:06 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
func (s *as) ServeErrorPage(ctx context.Context, c *model.Client, err error) {
|
|
|
|
s.authenticateClient(ctx, c)
|
|
|
|
s.Service.ServeErrorPage(ctx, c, err)
|
2019-12-13 20:08:26 +02:00
|
|
|
}
|
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
func (s *as) ServeSigninPage(ctx context.Context, c *model.Client) (err error) {
|
|
|
|
return s.Service.ServeSigninPage(ctx, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *as) ServeTimelinePage(ctx context.Context, c *model.Client, tType string,
|
|
|
|
maxID string, minID string) (err error) {
|
|
|
|
err = s.authenticateClient(ctx, c)
|
2019-12-13 20:08:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-28 19:51:00 +02:00
|
|
|
return s.Service.ServeTimelinePage(ctx, c, tType, maxID, minID)
|
|
|
|
}
|
2019-12-13 20:08:26 +02:00
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
func (s *as) ServeThreadPage(ctx context.Context, c *model.Client, id string, reply bool) (err error) {
|
|
|
|
err = s.authenticateClient(ctx, c)
|
2019-12-13 20:08:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-28 19:51:00 +02:00
|
|
|
return s.Service.ServeThreadPage(ctx, c, id, reply)
|
|
|
|
}
|
2019-12-13 20:08:26 +02:00
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
func (s *as) ServeLikedByPage(ctx context.Context, c *model.Client, id string) (err error) {
|
|
|
|
err = s.authenticateClient(ctx, c)
|
2019-12-13 20:08:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-28 19:51:00 +02:00
|
|
|
return s.Service.ServeLikedByPage(ctx, c, id)
|
2019-12-13 20:08:26 +02:00
|
|
|
}
|
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
func (s *as) ServeRetweetedByPage(ctx context.Context, c *model.Client, id string) (err error) {
|
|
|
|
err = s.authenticateClient(ctx, c)
|
2019-12-13 20:08:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-28 19:51:00 +02:00
|
|
|
return s.Service.ServeRetweetedByPage(ctx, c, id)
|
2019-12-13 20:08:26 +02:00
|
|
|
}
|
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
func (s *as) ServeFollowingPage(ctx context.Context, c *model.Client, id string,
|
|
|
|
maxID string, minID string) (err error) {
|
|
|
|
err = s.authenticateClient(ctx, c)
|
2019-12-13 20:08:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-28 19:51:00 +02:00
|
|
|
return s.Service.ServeFollowingPage(ctx, c, id, maxID, minID)
|
2019-12-13 20:08:26 +02:00
|
|
|
}
|
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
func (s *as) ServeFollowersPage(ctx context.Context, c *model.Client, id string,
|
|
|
|
maxID string, minID string) (err error) {
|
|
|
|
err = s.authenticateClient(ctx, c)
|
2019-12-15 19:37:58 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-28 19:51:00 +02:00
|
|
|
return s.Service.ServeFollowersPage(ctx, c, id, maxID, minID)
|
2019-12-15 19:37:58 +02:00
|
|
|
}
|
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
func (s *as) ServeNotificationPage(ctx context.Context, c *model.Client,
|
|
|
|
maxID string, minID string) (err error) {
|
|
|
|
err = s.authenticateClient(ctx, c)
|
2019-12-20 20:30:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-28 19:51:00 +02:00
|
|
|
return s.Service.ServeNotificationPage(ctx, c, maxID, minID)
|
2019-12-20 20:30:20 +02:00
|
|
|
}
|
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
func (s *as) ServeUserPage(ctx context.Context, c *model.Client, id string,
|
|
|
|
maxID string, minID string) (err error) {
|
|
|
|
err = s.authenticateClient(ctx, c)
|
2019-12-21 07:48:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-28 19:51:00 +02:00
|
|
|
return s.Service.ServeUserPage(ctx, c, id, maxID, minID)
|
2019-12-21 07:48:48 +02:00
|
|
|
}
|
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
func (s *as) ServeAboutPage(ctx context.Context, c *model.Client) (err error) {
|
|
|
|
err = s.authenticateClient(ctx, c)
|
2019-12-22 20:10:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-28 19:51:00 +02:00
|
|
|
return s.Service.ServeAboutPage(ctx, c)
|
2019-12-22 20:10:42 +02:00
|
|
|
}
|
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
func (s *as) ServeEmojiPage(ctx context.Context, c *model.Client) (err error) {
|
|
|
|
err = s.authenticateClient(ctx, c)
|
2019-12-26 11:11:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-28 19:51:00 +02:00
|
|
|
return s.Service.ServeEmojiPage(ctx, c)
|
2019-12-26 11:11:24 +02:00
|
|
|
}
|
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
func (s *as) ServeSearchPage(ctx context.Context, c *model.Client, q string,
|
|
|
|
qType string, offset int) (err error) {
|
|
|
|
err = s.authenticateClient(ctx, c)
|
2019-12-26 11:11:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-28 19:51:00 +02:00
|
|
|
return s.Service.ServeSearchPage(ctx, c, q, qType, offset)
|
2019-12-26 11:11:24 +02:00
|
|
|
}
|
|
|
|
|
2020-01-30 17:32:37 +02:00
|
|
|
func (s *as) ServeUserSearchPage(ctx context.Context, c *model.Client,
|
|
|
|
id string, q string, offset int) (err error) {
|
|
|
|
err = s.authenticateClient(ctx, c)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return s.Service.ServeUserSearchPage(ctx, c, id, q, offset)
|
|
|
|
}
|
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
func (s *as) ServeSettingsPage(ctx context.Context, c *model.Client) (err error) {
|
|
|
|
err = s.authenticateClient(ctx, c)
|
2019-12-29 05:43:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-28 19:51:00 +02:00
|
|
|
return s.Service.ServeSettingsPage(ctx, c)
|
2019-12-29 05:43:57 +02:00
|
|
|
}
|
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
func (s *as) NewSession(ctx context.Context, instance string) (redirectUrl string,
|
|
|
|
sessionID string, err error) {
|
|
|
|
return s.Service.NewSession(ctx, instance)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *as) Signin(ctx context.Context, c *model.Client, sessionID string,
|
|
|
|
code string) (token string, err error) {
|
|
|
|
err = s.authenticateClient(ctx, c)
|
2019-12-29 05:43:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
token, err = s.Service.Signin(ctx, 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
|
|
|
|
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-01-28 19:51:00 +02:00
|
|
|
func (s *as) Post(ctx context.Context, c *model.Client, content string,
|
|
|
|
replyToID string, format string, visibility string, isNSFW bool,
|
|
|
|
files []*multipart.FileHeader) (id string, err error) {
|
|
|
|
err = s.authenticateClient(ctx, c)
|
2019-12-27 10:06:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-25 12:07:06 +02:00
|
|
|
err = checkCSRF(ctx, c)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-28 19:51:00 +02:00
|
|
|
return s.Service.Post(ctx, c, content, replyToID, format, visibility, isNSFW, files)
|
2019-12-27 10:06:43 +02:00
|
|
|
}
|
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
func (s *as) Like(ctx context.Context, c *model.Client, id string) (count int64, err error) {
|
|
|
|
err = s.authenticateClient(ctx, c)
|
2019-12-13 20:08:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-25 12:07:06 +02:00
|
|
|
err = checkCSRF(ctx, c)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-28 19:51:00 +02:00
|
|
|
return s.Service.Like(ctx, c, id)
|
2019-12-13 20:08:26 +02:00
|
|
|
}
|
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
func (s *as) UnLike(ctx context.Context, c *model.Client, id string) (count int64, err error) {
|
|
|
|
err = s.authenticateClient(ctx, c)
|
2019-12-13 20:08:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-25 12:07:06 +02:00
|
|
|
err = checkCSRF(ctx, c)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-28 19:51:00 +02:00
|
|
|
return s.Service.UnLike(ctx, c, id)
|
2019-12-13 20:08:26 +02:00
|
|
|
}
|
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
func (s *as) Retweet(ctx context.Context, c *model.Client, id string) (count int64, err error) {
|
|
|
|
err = s.authenticateClient(ctx, c)
|
2019-12-13 20:08:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-25 12:07:06 +02:00
|
|
|
err = checkCSRF(ctx, c)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-28 19:51:00 +02:00
|
|
|
return s.Service.Retweet(ctx, c, id)
|
2019-12-13 20:08:26 +02:00
|
|
|
}
|
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
func (s *as) UnRetweet(ctx context.Context, c *model.Client, id string) (count int64, err error) {
|
|
|
|
err = s.authenticateClient(ctx, c)
|
2019-12-13 20:08:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-25 12:07:06 +02:00
|
|
|
err = checkCSRF(ctx, c)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-28 19:51:00 +02:00
|
|
|
return s.Service.UnRetweet(ctx, c, id)
|
2019-12-13 20:08:26 +02:00
|
|
|
}
|
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
func (s *as) Follow(ctx context.Context, c *model.Client, id string) (err error) {
|
|
|
|
err = s.authenticateClient(ctx, c)
|
2019-12-13 20:08:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-25 12:07:06 +02:00
|
|
|
err = checkCSRF(ctx, c)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-28 19:51:00 +02:00
|
|
|
return s.Service.Follow(ctx, c, id)
|
2019-12-13 20:08:26 +02:00
|
|
|
}
|
2019-12-20 20:30:20 +02:00
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
func (s *as) UnFollow(ctx context.Context, c *model.Client, id string) (err error) {
|
|
|
|
err = s.authenticateClient(ctx, c)
|
2019-12-20 20:30:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-25 12:07:06 +02:00
|
|
|
err = checkCSRF(ctx, c)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-28 19:51:00 +02:00
|
|
|
return s.Service.UnFollow(ctx, c, id)
|
2019-12-20 20:30:20 +02:00
|
|
|
}
|
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
func (s *as) SaveSettings(ctx context.Context, c *model.Client, settings *model.Settings) (err error) {
|
|
|
|
err = s.authenticateClient(ctx, c)
|
2019-12-20 20:30:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-25 12:07:06 +02:00
|
|
|
err = checkCSRF(ctx, c)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-28 19:51:00 +02:00
|
|
|
return s.Service.SaveSettings(ctx, c, settings)
|
2019-12-20 20:30:20 +02:00
|
|
|
}
|