Skip to content

Commit

Permalink
federated authentication better logging + tidying (#232)
Browse files Browse the repository at this point in the history
* change trace logging in authenticator

* messing about

* lil changes

* go fmt

* error fix

* Fix broken test
  • Loading branch information
tsmethurst committed Sep 16, 2021
1 parent 2e5dcc2 commit 92186c8
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 123 deletions.
59 changes: 59 additions & 0 deletions internal/api/s2s/user/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
GoToSocial
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package user

import (
"context"
"fmt"

"github.com/gin-gonic/gin"
"github.com/superseriousbusiness/gotosocial/internal/util"
)

// ActivityPubAcceptHeaders represents the Accept headers mentioned here:
// https://www.w3.org/TR/activitypub/#retrieving-objects
var ActivityPubAcceptHeaders = []string{
`application/activity+json`,
`application/ld+json; profile="https://www.w3.org/ns/activitystreams"`,
}

// populateContext transfers the signature verifier and signature from the gin context to the request context
func populateContext(c *gin.Context) context.Context {
ctx := c.Request.Context()

verifier, signed := c.Get(string(util.APRequestingPublicKeyVerifier))
if signed {
ctx = context.WithValue(ctx, util.APRequestingPublicKeyVerifier, verifier)
}

signature, signed := c.Get(string(util.APRequestingPublicKeySignature))
if signed {
ctx = context.WithValue(ctx, util.APRequestingPublicKeySignature, signature)
}

return ctx
}

func negotiateFormat(c *gin.Context) (string, error) {
format := c.NegotiateFormat(ActivityPubAcceptHeaders...)
if format == "" {
return "", fmt.Errorf("no format can be offered for Accept headers %s", c.Request.Header.Get("Accept"))
}
return format, nil
}
24 changes: 8 additions & 16 deletions internal/api/s2s/user/followers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@
package user

import (
"context"
"encoding/json"
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/util"
)

// FollowersGETHandler returns a collection of URIs for followers of the target user, formatted so that other AP servers can understand it.
Expand All @@ -42,25 +40,19 @@ func (m *Module) FollowersGETHandler(c *gin.Context) {
return
}

// make sure this actually an AP request
format := c.NegotiateFormat(ActivityPubAcceptHeaders...)
if format == "" {
c.JSON(http.StatusNotAcceptable, gin.H{"error": "could not negotiate format with given Accept header(s)"})
format, err := negotiateFormat(c)
if err != nil {
c.JSON(http.StatusNotAcceptable, gin.H{"error": fmt.Sprintf("could not negotiate format with given Accept header(s): %s", err)})
return
}
l.Tracef("negotiated format: %s", format)

// transfer the signature verifier from the gin context to the request context
ctx := c.Request.Context()
verifier, signed := c.Get(string(util.APRequestingPublicKeyVerifier))
if signed {
ctx = context.WithValue(ctx, util.APRequestingPublicKeyVerifier, verifier)
}
ctx := populateContext(c)

followers, err := m.processor.GetFediFollowers(ctx, requestedUsername, c.Request.URL) // GetFediUser handles auth as well
if err != nil {
l.Info(err.Error())
c.JSON(err.Code(), gin.H{"error": err.Safe()})
followers, errWithCode := m.processor.GetFediFollowers(ctx, requestedUsername, c.Request.URL)
if errWithCode != nil {
l.Info(errWithCode.Error())
c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()})
return
}

Expand Down
24 changes: 8 additions & 16 deletions internal/api/s2s/user/following.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@
package user

import (
"context"
"encoding/json"
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/util"
)

// FollowingGETHandler returns a collection of URIs for accounts that the target user follows, formatted so that other AP servers can understand it.
Expand All @@ -42,25 +40,19 @@ func (m *Module) FollowingGETHandler(c *gin.Context) {
return
}

// make sure this actually an AP request
format := c.NegotiateFormat(ActivityPubAcceptHeaders...)
if format == "" {
c.JSON(http.StatusNotAcceptable, gin.H{"error": "could not negotiate format with given Accept header(s)"})
format, err := negotiateFormat(c)
if err != nil {
c.JSON(http.StatusNotAcceptable, gin.H{"error": fmt.Sprintf("could not negotiate format with given Accept header(s): %s", err)})
return
}
l.Tracef("negotiated format: %s", format)

// transfer the signature verifier from the gin context to the request context
ctx := c.Request.Context()
verifier, signed := c.Get(string(util.APRequestingPublicKeyVerifier))
if signed {
ctx = context.WithValue(ctx, util.APRequestingPublicKeyVerifier, verifier)
}
ctx := populateContext(c)

following, err := m.processor.GetFediFollowing(ctx, requestedUsername, c.Request.URL) // handles auth as well
if err != nil {
l.Info(err.Error())
c.JSON(err.Code(), gin.H{"error": err.Safe()})
following, errWithCode := m.processor.GetFediFollowing(ctx, requestedUsername, c.Request.URL)
if errWithCode != nil {
l.Info(errWithCode.Error())
c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()})
return
}

Expand Down
9 changes: 1 addition & 8 deletions internal/api/s2s/user/inboxpost.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@
package user

import (
"context"
"net/http"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/util"
)

// InboxPOSTHandler deals with incoming POST requests to an actor's inbox.
Expand All @@ -42,12 +40,7 @@ func (m *Module) InboxPOSTHandler(c *gin.Context) {
return
}

// transfer the signature verifier from the gin context to the request context
ctx := c.Request.Context()
verifier, signed := c.Get(string(util.APRequestingPublicKeyVerifier))
if signed {
ctx = context.WithValue(ctx, util.APRequestingPublicKeyVerifier, verifier)
}
ctx := populateContext(c)

posted, err := m.processor.InboxPost(ctx, c.Writer, c.Request)
if err != nil {
Expand Down
24 changes: 8 additions & 16 deletions internal/api/s2s/user/publickeyget.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package user

import (
"context"
"encoding/json"
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/util"
)

// PublicKeyGETHandler should be served at eg https://example.org/users/:username/main-key.
Expand All @@ -28,25 +26,19 @@ func (m *Module) PublicKeyGETHandler(c *gin.Context) {
return
}

// make sure this actually an AP request
format := c.NegotiateFormat(ActivityPubAcceptHeaders...)
if format == "" {
c.JSON(http.StatusNotAcceptable, gin.H{"error": "could not negotiate format with given Accept header(s)"})
format, err := negotiateFormat(c)
if err != nil {
c.JSON(http.StatusNotAcceptable, gin.H{"error": fmt.Sprintf("could not negotiate format with given Accept header(s): %s", err)})
return
}
l.Tracef("negotiated format: %s", format)

// transfer the signature verifier from the gin context to the request context
ctx := c.Request.Context()
verifier, signed := c.Get(string(util.APRequestingPublicKeyVerifier))
if signed {
ctx = context.WithValue(ctx, util.APRequestingPublicKeyVerifier, verifier)
}
ctx := populateContext(c)

user, err := m.processor.GetFediUser(ctx, requestedUsername, c.Request.URL) // GetFediUser handles auth as well
if err != nil {
l.Info(err.Error())
c.JSON(err.Code(), gin.H{"error": err.Safe()})
user, errWithCode := m.processor.GetFediUser(ctx, requestedUsername, c.Request.URL)
if errWithCode != nil {
l.Info(errWithCode.Error())
c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()})
return
}

Expand Down
24 changes: 8 additions & 16 deletions internal/api/s2s/user/repliesget.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package user

import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/util"
)

// StatusRepliesGETHandler swagger:operation GET /users/{username}/statuses/{status}/replies s2sRepliesGet
Expand Down Expand Up @@ -116,25 +114,19 @@ func (m *Module) StatusRepliesGETHandler(c *gin.Context) {
minID = minIDString
}

// make sure this actually an AP request
format := c.NegotiateFormat(ActivityPubAcceptHeaders...)
if format == "" {
c.JSON(http.StatusNotAcceptable, gin.H{"error": "could not negotiate format with given Accept header(s)"})
format, err := negotiateFormat(c)
if err != nil {
c.JSON(http.StatusNotAcceptable, gin.H{"error": fmt.Sprintf("could not negotiate format with given Accept header(s): %s", err)})
return
}
l.Tracef("negotiated format: %s", format)

// transfer the signature verifier from the gin context to the request context
ctx := c.Request.Context()
verifier, signed := c.Get(string(util.APRequestingPublicKeyVerifier))
if signed {
ctx = context.WithValue(ctx, util.APRequestingPublicKeyVerifier, verifier)
}
ctx := populateContext(c)

replies, err := m.processor.GetFediStatusReplies(ctx, requestedUsername, requestedStatusID, page, onlyOtherAccounts, minID, c.Request.URL)
if err != nil {
l.Info(err.Error())
c.JSON(err.Code(), gin.H{"error": err.Safe()})
replies, errWithCode := m.processor.GetFediStatusReplies(ctx, requestedUsername, requestedStatusID, page, onlyOtherAccounts, minID, c.Request.URL)
if errWithCode != nil {
l.Info(errWithCode.Error())
c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()})
return
}

Expand Down
24 changes: 8 additions & 16 deletions internal/api/s2s/user/statusget.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package user

import (
"context"
"encoding/json"
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/util"
)

// StatusGETHandler serves the target status as an activitystreams NOTE so that other AP servers can parse it.
Expand All @@ -30,25 +28,19 @@ func (m *Module) StatusGETHandler(c *gin.Context) {
return
}

// make sure this actually an AP request
format := c.NegotiateFormat(ActivityPubAcceptHeaders...)
if format == "" {
c.JSON(http.StatusNotAcceptable, gin.H{"error": "could not negotiate format with given Accept header(s)"})
format, err := negotiateFormat(c)
if err != nil {
c.JSON(http.StatusNotAcceptable, gin.H{"error": fmt.Sprintf("could not negotiate format with given Accept header(s): %s", err)})
return
}
l.Tracef("negotiated format: %s", format)

// transfer the signature verifier from the gin context to the request context
ctx := c.Request.Context()
verifier, signed := c.Get(string(util.APRequestingPublicKeyVerifier))
if signed {
ctx = context.WithValue(ctx, util.APRequestingPublicKeyVerifier, verifier)
}
ctx := populateContext(c)

status, err := m.processor.GetFediStatus(ctx, requestedUsername, requestedStatusID, c.Request.URL) // handles auth as well
if err != nil {
l.Info(err.Error())
c.JSON(err.Code(), gin.H{"error": err.Safe()})
status, errWithCode := m.processor.GetFediStatus(ctx, requestedUsername, requestedStatusID, c.Request.URL)
if errWithCode != nil {
l.Info(errWithCode.Error())
c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()})
return
}

Expand Down
7 changes: 0 additions & 7 deletions internal/api/s2s/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,6 @@ const (
UsersStatusRepliesPath = UsersStatusPath + "/replies"
)

// ActivityPubAcceptHeaders represents the Accept headers mentioned here:
// https://www.w3.org/TR/activitypub/#retrieving-objects
var ActivityPubAcceptHeaders = []string{
`application/activity+json`,
`application/ld+json; profile="https://www.w3.org/ns/activitystreams"`,
}

// Module implements the FederationAPIModule interface
type Module struct {
config *config.Config
Expand Down
24 changes: 8 additions & 16 deletions internal/api/s2s/user/userget.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@
package user

import (
"context"
"encoding/json"
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/util"
)

// UsersGETHandler should be served at https://example.org/users/:username.
Expand All @@ -50,25 +48,19 @@ func (m *Module) UsersGETHandler(c *gin.Context) {
return
}

// make sure this actually an AP request
format := c.NegotiateFormat(ActivityPubAcceptHeaders...)
if format == "" {
c.JSON(http.StatusNotAcceptable, gin.H{"error": "could not negotiate format with given Accept header(s)"})
format, err := negotiateFormat(c)
if err != nil {
c.JSON(http.StatusNotAcceptable, gin.H{"error": fmt.Sprintf("could not negotiate format with given Accept header(s): %s", err)})
return
}
l.Tracef("negotiated format: %s", format)

// transfer the signature verifier from the gin context to the request context
ctx := c.Request.Context()
verifier, signed := c.Get(string(util.APRequestingPublicKeyVerifier))
if signed {
ctx = context.WithValue(ctx, util.APRequestingPublicKeyVerifier, verifier)
}
ctx := populateContext(c)

user, err := m.processor.GetFediUser(ctx, requestedUsername, c.Request.URL) // GetFediUser handles auth as well
if err != nil {
l.Info(err.Error())
c.JSON(err.Code(), gin.H{"error": err.Safe()})
user, errWithCode := m.processor.GetFediUser(ctx, requestedUsername, c.Request.URL) // GetFediUser handles auth as well
if errWithCode != nil {
l.Info(errWithCode.Error())
c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()})
return
}

Expand Down
Loading

0 comments on commit 92186c8

Please sign in to comment.