activitypub.go (2781B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package api 19 20 import ( 21 "github.com/gin-gonic/gin" 22 "github.com/superseriousbusiness/gotosocial/internal/api/activitypub/emoji" 23 "github.com/superseriousbusiness/gotosocial/internal/api/activitypub/publickey" 24 "github.com/superseriousbusiness/gotosocial/internal/api/activitypub/users" 25 "github.com/superseriousbusiness/gotosocial/internal/db" 26 "github.com/superseriousbusiness/gotosocial/internal/middleware" 27 "github.com/superseriousbusiness/gotosocial/internal/processing" 28 "github.com/superseriousbusiness/gotosocial/internal/router" 29 ) 30 31 type ActivityPub struct { 32 emoji *emoji.Module 33 users *users.Module 34 publicKey *publickey.Module 35 signatureCheckMiddleware gin.HandlerFunc 36 } 37 38 func (a *ActivityPub) Route(r router.Router, m ...gin.HandlerFunc) { 39 // create groupings for the 'emoji' and 'users' prefixes 40 emojiGroup := r.AttachGroup("emoji") 41 usersGroup := r.AttachGroup("users") 42 43 // attach shared, non-global middlewares to both of these groups 44 cacheControlMiddleware := middleware.CacheControl("no-store") 45 emojiGroup.Use(m...) 46 usersGroup.Use(m...) 47 emojiGroup.Use(a.signatureCheckMiddleware, cacheControlMiddleware) 48 usersGroup.Use(a.signatureCheckMiddleware, cacheControlMiddleware) 49 50 a.emoji.Route(emojiGroup.Handle) 51 a.users.Route(usersGroup.Handle) 52 } 53 54 // Public key endpoint requires different middleware + cache policies from other AP endpoints. 55 func (a *ActivityPub) RoutePublicKey(r router.Router, m ...gin.HandlerFunc) { 56 publicKeyGroup := r.AttachGroup(publickey.PublicKeyPath) 57 publicKeyGroup.Use(a.signatureCheckMiddleware, middleware.CacheControl("public,max-age=604800")) 58 a.publicKey.Route(publicKeyGroup.Handle) 59 } 60 61 func NewActivityPub(db db.DB, p *processing.Processor) *ActivityPub { 62 return &ActivityPub{ 63 emoji: emoji.New(p), 64 users: users.New(p), 65 publicKey: publickey.New(p), 66 signatureCheckMiddleware: middleware.SignatureCheck(db.IsURIBlocked), 67 } 68 }