mirror of
				https://github.com/superseriousbusiness/gotosocial.git
				synced 2025-10-30 21:12:24 -05:00 
			
		
		
		
	federated authentication better logging + tidying (#232)
* change trace logging in authenticator * messing about * lil changes * go fmt * error fix * Fix broken test
This commit is contained in:
		
					parent
					
						
							
								2e5dcc2929
							
						
					
				
			
			
				commit
				
					
						92186c8c6f
					
				
			
		
					 13 changed files with 134 additions and 123 deletions
				
			
		
							
								
								
									
										59
									
								
								internal/api/s2s/user/common.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								internal/api/s2s/user/common.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -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 | ||||||
|  | } | ||||||
|  | @ -19,14 +19,12 @@ | ||||||
| package user | package user | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"context" |  | ||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"net/http" | 	"net/http" | ||||||
| 
 | 
 | ||||||
| 	"github.com/gin-gonic/gin" | 	"github.com/gin-gonic/gin" | ||||||
| 	"github.com/sirupsen/logrus" | 	"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. | // FollowersGETHandler returns a collection of URIs for followers of the target user, formatted so that other AP servers can understand it. | ||||||
|  | @ -42,25 +40,19 @@ func (m *Module) FollowersGETHandler(c *gin.Context) { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// make sure this actually an AP request | 	format, err := negotiateFormat(c) | ||||||
| 	format := c.NegotiateFormat(ActivityPubAcceptHeaders...) | 	if err != nil { | ||||||
| 	if format == "" { | 		c.JSON(http.StatusNotAcceptable, gin.H{"error": fmt.Sprintf("could not negotiate format with given Accept header(s): %s", err)}) | ||||||
| 		c.JSON(http.StatusNotAcceptable, gin.H{"error": "could not negotiate format with given Accept header(s)"}) |  | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	l.Tracef("negotiated format: %s", format) | 	l.Tracef("negotiated format: %s", format) | ||||||
| 
 | 
 | ||||||
| 	// transfer the signature verifier from the gin context to the request context | 	ctx := populateContext(c) | ||||||
| 	ctx := c.Request.Context() |  | ||||||
| 	verifier, signed := c.Get(string(util.APRequestingPublicKeyVerifier)) |  | ||||||
| 	if signed { |  | ||||||
| 		ctx = context.WithValue(ctx, util.APRequestingPublicKeyVerifier, verifier) |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	followers, err := m.processor.GetFediFollowers(ctx, requestedUsername, c.Request.URL) // GetFediUser handles auth as well | 	followers, errWithCode := m.processor.GetFediFollowers(ctx, requestedUsername, c.Request.URL) | ||||||
| 	if err != nil { | 	if errWithCode != nil { | ||||||
| 		l.Info(err.Error()) | 		l.Info(errWithCode.Error()) | ||||||
| 		c.JSON(err.Code(), gin.H{"error": err.Safe()}) | 		c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -19,14 +19,12 @@ | ||||||
| package user | package user | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"context" |  | ||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"net/http" | 	"net/http" | ||||||
| 
 | 
 | ||||||
| 	"github.com/gin-gonic/gin" | 	"github.com/gin-gonic/gin" | ||||||
| 	"github.com/sirupsen/logrus" | 	"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. | // FollowingGETHandler returns a collection of URIs for accounts that the target user follows, formatted so that other AP servers can understand it. | ||||||
|  | @ -42,25 +40,19 @@ func (m *Module) FollowingGETHandler(c *gin.Context) { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// make sure this actually an AP request | 	format, err := negotiateFormat(c) | ||||||
| 	format := c.NegotiateFormat(ActivityPubAcceptHeaders...) | 	if err != nil { | ||||||
| 	if format == "" { | 		c.JSON(http.StatusNotAcceptable, gin.H{"error": fmt.Sprintf("could not negotiate format with given Accept header(s): %s", err)}) | ||||||
| 		c.JSON(http.StatusNotAcceptable, gin.H{"error": "could not negotiate format with given Accept header(s)"}) |  | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	l.Tracef("negotiated format: %s", format) | 	l.Tracef("negotiated format: %s", format) | ||||||
| 
 | 
 | ||||||
| 	// transfer the signature verifier from the gin context to the request context | 	ctx := populateContext(c) | ||||||
| 	ctx := c.Request.Context() |  | ||||||
| 	verifier, signed := c.Get(string(util.APRequestingPublicKeyVerifier)) |  | ||||||
| 	if signed { |  | ||||||
| 		ctx = context.WithValue(ctx, util.APRequestingPublicKeyVerifier, verifier) |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	following, err := m.processor.GetFediFollowing(ctx, requestedUsername, c.Request.URL) // handles auth as well | 	following, errWithCode := m.processor.GetFediFollowing(ctx, requestedUsername, c.Request.URL) | ||||||
| 	if err != nil { | 	if errWithCode != nil { | ||||||
| 		l.Info(err.Error()) | 		l.Info(errWithCode.Error()) | ||||||
| 		c.JSON(err.Code(), gin.H{"error": err.Safe()}) | 		c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -19,13 +19,11 @@ | ||||||
| package user | package user | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"context" |  | ||||||
| 	"net/http" | 	"net/http" | ||||||
| 
 | 
 | ||||||
| 	"github.com/gin-gonic/gin" | 	"github.com/gin-gonic/gin" | ||||||
| 	"github.com/sirupsen/logrus" | 	"github.com/sirupsen/logrus" | ||||||
| 	"github.com/superseriousbusiness/gotosocial/internal/gtserror" | 	"github.com/superseriousbusiness/gotosocial/internal/gtserror" | ||||||
| 	"github.com/superseriousbusiness/gotosocial/internal/util" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // InboxPOSTHandler deals with incoming POST requests to an actor's inbox. | // InboxPOSTHandler deals with incoming POST requests to an actor's inbox. | ||||||
|  | @ -42,12 +40,7 @@ func (m *Module) InboxPOSTHandler(c *gin.Context) { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// transfer the signature verifier from the gin context to the request context | 	ctx := populateContext(c) | ||||||
| 	ctx := c.Request.Context() |  | ||||||
| 	verifier, signed := c.Get(string(util.APRequestingPublicKeyVerifier)) |  | ||||||
| 	if signed { |  | ||||||
| 		ctx = context.WithValue(ctx, util.APRequestingPublicKeyVerifier, verifier) |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	posted, err := m.processor.InboxPost(ctx, c.Writer, c.Request) | 	posted, err := m.processor.InboxPost(ctx, c.Writer, c.Request) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  |  | ||||||
|  | @ -1,14 +1,12 @@ | ||||||
| package user | package user | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"context" |  | ||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"net/http" | 	"net/http" | ||||||
| 
 | 
 | ||||||
| 	"github.com/gin-gonic/gin" | 	"github.com/gin-gonic/gin" | ||||||
| 	"github.com/sirupsen/logrus" | 	"github.com/sirupsen/logrus" | ||||||
| 	"github.com/superseriousbusiness/gotosocial/internal/util" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // PublicKeyGETHandler should be served at eg https://example.org/users/:username/main-key. | // PublicKeyGETHandler should be served at eg https://example.org/users/:username/main-key. | ||||||
|  | @ -28,25 +26,19 @@ func (m *Module) PublicKeyGETHandler(c *gin.Context) { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// make sure this actually an AP request | 	format, err := negotiateFormat(c) | ||||||
| 	format := c.NegotiateFormat(ActivityPubAcceptHeaders...) | 	if err != nil { | ||||||
| 	if format == "" { | 		c.JSON(http.StatusNotAcceptable, gin.H{"error": fmt.Sprintf("could not negotiate format with given Accept header(s): %s", err)}) | ||||||
| 		c.JSON(http.StatusNotAcceptable, gin.H{"error": "could not negotiate format with given Accept header(s)"}) |  | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	l.Tracef("negotiated format: %s", format) | 	l.Tracef("negotiated format: %s", format) | ||||||
| 
 | 
 | ||||||
| 	// transfer the signature verifier from the gin context to the request context | 	ctx := populateContext(c) | ||||||
| 	ctx := c.Request.Context() |  | ||||||
| 	verifier, signed := c.Get(string(util.APRequestingPublicKeyVerifier)) |  | ||||||
| 	if signed { |  | ||||||
| 		ctx = context.WithValue(ctx, util.APRequestingPublicKeyVerifier, verifier) |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	user, err := m.processor.GetFediUser(ctx, requestedUsername, c.Request.URL) // GetFediUser handles auth as well | 	user, errWithCode := m.processor.GetFediUser(ctx, requestedUsername, c.Request.URL) | ||||||
| 	if err != nil { | 	if errWithCode != nil { | ||||||
| 		l.Info(err.Error()) | 		l.Info(errWithCode.Error()) | ||||||
| 		c.JSON(err.Code(), gin.H{"error": err.Safe()}) | 		c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1,7 +1,6 @@ | ||||||
| package user | package user | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"context" |  | ||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"net/http" | 	"net/http" | ||||||
|  | @ -9,7 +8,6 @@ import ( | ||||||
| 
 | 
 | ||||||
| 	"github.com/gin-gonic/gin" | 	"github.com/gin-gonic/gin" | ||||||
| 	"github.com/sirupsen/logrus" | 	"github.com/sirupsen/logrus" | ||||||
| 	"github.com/superseriousbusiness/gotosocial/internal/util" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // StatusRepliesGETHandler swagger:operation GET /users/{username}/statuses/{status}/replies s2sRepliesGet | // StatusRepliesGETHandler swagger:operation GET /users/{username}/statuses/{status}/replies s2sRepliesGet | ||||||
|  | @ -116,25 +114,19 @@ func (m *Module) StatusRepliesGETHandler(c *gin.Context) { | ||||||
| 		minID = minIDString | 		minID = minIDString | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// make sure this actually an AP request | 	format, err := negotiateFormat(c) | ||||||
| 	format := c.NegotiateFormat(ActivityPubAcceptHeaders...) | 	if err != nil { | ||||||
| 	if format == "" { | 		c.JSON(http.StatusNotAcceptable, gin.H{"error": fmt.Sprintf("could not negotiate format with given Accept header(s): %s", err)}) | ||||||
| 		c.JSON(http.StatusNotAcceptable, gin.H{"error": "could not negotiate format with given Accept header(s)"}) |  | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	l.Tracef("negotiated format: %s", format) | 	l.Tracef("negotiated format: %s", format) | ||||||
| 
 | 
 | ||||||
| 	// transfer the signature verifier from the gin context to the request context | 	ctx := populateContext(c) | ||||||
| 	ctx := c.Request.Context() |  | ||||||
| 	verifier, signed := c.Get(string(util.APRequestingPublicKeyVerifier)) |  | ||||||
| 	if signed { |  | ||||||
| 		ctx = context.WithValue(ctx, util.APRequestingPublicKeyVerifier, verifier) |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	replies, err := m.processor.GetFediStatusReplies(ctx, requestedUsername, requestedStatusID, page, onlyOtherAccounts, minID, c.Request.URL) | 	replies, errWithCode := m.processor.GetFediStatusReplies(ctx, requestedUsername, requestedStatusID, page, onlyOtherAccounts, minID, c.Request.URL) | ||||||
| 	if err != nil { | 	if errWithCode != nil { | ||||||
| 		l.Info(err.Error()) | 		l.Info(errWithCode.Error()) | ||||||
| 		c.JSON(err.Code(), gin.H{"error": err.Safe()}) | 		c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1,14 +1,12 @@ | ||||||
| package user | package user | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"context" |  | ||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"net/http" | 	"net/http" | ||||||
| 
 | 
 | ||||||
| 	"github.com/gin-gonic/gin" | 	"github.com/gin-gonic/gin" | ||||||
| 	"github.com/sirupsen/logrus" | 	"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. | // StatusGETHandler serves the target status as an activitystreams NOTE so that other AP servers can parse it. | ||||||
|  | @ -30,25 +28,19 @@ func (m *Module) StatusGETHandler(c *gin.Context) { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// make sure this actually an AP request | 	format, err := negotiateFormat(c) | ||||||
| 	format := c.NegotiateFormat(ActivityPubAcceptHeaders...) | 	if err != nil { | ||||||
| 	if format == "" { | 		c.JSON(http.StatusNotAcceptable, gin.H{"error": fmt.Sprintf("could not negotiate format with given Accept header(s): %s", err)}) | ||||||
| 		c.JSON(http.StatusNotAcceptable, gin.H{"error": "could not negotiate format with given Accept header(s)"}) |  | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	l.Tracef("negotiated format: %s", format) | 	l.Tracef("negotiated format: %s", format) | ||||||
| 
 | 
 | ||||||
| 	// transfer the signature verifier from the gin context to the request context | 	ctx := populateContext(c) | ||||||
| 	ctx := c.Request.Context() |  | ||||||
| 	verifier, signed := c.Get(string(util.APRequestingPublicKeyVerifier)) |  | ||||||
| 	if signed { |  | ||||||
| 		ctx = context.WithValue(ctx, util.APRequestingPublicKeyVerifier, verifier) |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	status, err := m.processor.GetFediStatus(ctx, requestedUsername, requestedStatusID, c.Request.URL) // handles auth as well | 	status, errWithCode := m.processor.GetFediStatus(ctx, requestedUsername, requestedStatusID, c.Request.URL) | ||||||
| 	if err != nil { | 	if errWithCode != nil { | ||||||
| 		l.Info(err.Error()) | 		l.Info(errWithCode.Error()) | ||||||
| 		c.JSON(err.Code(), gin.H{"error": err.Safe()}) | 		c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -61,13 +61,6 @@ const ( | ||||||
| 	UsersStatusRepliesPath = UsersStatusPath + "/replies" | 	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 | // Module implements the FederationAPIModule interface | ||||||
| type Module struct { | type Module struct { | ||||||
| 	config    *config.Config | 	config    *config.Config | ||||||
|  |  | ||||||
|  | @ -19,14 +19,12 @@ | ||||||
| package user | package user | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"context" |  | ||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"net/http" | 	"net/http" | ||||||
| 
 | 
 | ||||||
| 	"github.com/gin-gonic/gin" | 	"github.com/gin-gonic/gin" | ||||||
| 	"github.com/sirupsen/logrus" | 	"github.com/sirupsen/logrus" | ||||||
| 	"github.com/superseriousbusiness/gotosocial/internal/util" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // UsersGETHandler should be served at https://example.org/users/:username. | // UsersGETHandler should be served at https://example.org/users/:username. | ||||||
|  | @ -50,25 +48,19 @@ func (m *Module) UsersGETHandler(c *gin.Context) { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// make sure this actually an AP request | 	format, err := negotiateFormat(c) | ||||||
| 	format := c.NegotiateFormat(ActivityPubAcceptHeaders...) | 	if err != nil { | ||||||
| 	if format == "" { | 		c.JSON(http.StatusNotAcceptable, gin.H{"error": fmt.Sprintf("could not negotiate format with given Accept header(s): %s", err)}) | ||||||
| 		c.JSON(http.StatusNotAcceptable, gin.H{"error": "could not negotiate format with given Accept header(s)"}) |  | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	l.Tracef("negotiated format: %s", format) | 	l.Tracef("negotiated format: %s", format) | ||||||
| 
 | 
 | ||||||
| 	// transfer the signature verifier from the gin context to the request context | 	ctx := populateContext(c) | ||||||
| 	ctx := c.Request.Context() |  | ||||||
| 	verifier, signed := c.Get(string(util.APRequestingPublicKeyVerifier)) |  | ||||||
| 	if signed { |  | ||||||
| 		ctx = context.WithValue(ctx, util.APRequestingPublicKeyVerifier, verifier) |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	user, err := m.processor.GetFediUser(ctx, requestedUsername, c.Request.URL) // GetFediUser handles auth as well | 	user, errWithCode := m.processor.GetFediUser(ctx, requestedUsername, c.Request.URL) // GetFediUser handles auth as well | ||||||
| 	if err != nil { | 	if errWithCode != nil { | ||||||
| 		l.Info(err.Error()) | 		l.Info(errWithCode.Error()) | ||||||
| 		c.JSON(err.Code(), gin.H{"error": err.Safe()}) | 		c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -11,13 +11,10 @@ import ( | ||||||
| 
 | 
 | ||||||
| // SignatureCheck checks whether an incoming http request has been signed. If so, it will check if the domain | // SignatureCheck checks whether an incoming http request has been signed. If so, it will check if the domain | ||||||
| // that signed the request is permitted to access the server. If it is permitted, the handler will set the key | // that signed the request is permitted to access the server. If it is permitted, the handler will set the key | ||||||
| // verifier in the gin context for use down the line. | // verifier and the signature in the gin context for use down the line. | ||||||
| func (m *Module) SignatureCheck(c *gin.Context) { | func (m *Module) SignatureCheck(c *gin.Context) { | ||||||
| 	l := m.log.WithField("func", "DomainBlockChecker") | 	l := m.log.WithField("func", "DomainBlockChecker") | ||||||
| 
 | 
 | ||||||
| 	// set this extra field for signature validation |  | ||||||
| 	c.Request.Header.Set("host", m.config.Host) |  | ||||||
| 
 |  | ||||||
| 	// create the verifier from the request | 	// create the verifier from the request | ||||||
| 	// if the request is signed, it will have a signature header | 	// if the request is signed, it will have a signature header | ||||||
| 	verifier, err := httpsig.NewVerifier(c.Request) | 	verifier, err := httpsig.NewVerifier(c.Request) | ||||||
|  | @ -43,8 +40,12 @@ func (m *Module) SignatureCheck(c *gin.Context) { | ||||||
| 				return | 				return | ||||||
| 			} | 			} | ||||||
| 
 | 
 | ||||||
| 			// set the verifier on the context here to save some work further down the line | 			// set the verifier and signature on the context here to save some work further down the line | ||||||
| 			c.Set(string(util.APRequestingPublicKeyVerifier), verifier) | 			c.Set(string(util.APRequestingPublicKeyVerifier), verifier) | ||||||
|  | 			signature := c.GetHeader("Signature") | ||||||
|  | 			if signature != "" { | ||||||
|  | 				c.Set(string(util.APRequestingPublicKeySignature), signature) | ||||||
|  | 			} | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -102,10 +102,6 @@ func getPublicKeyFromResponse(c context.Context, b []byte, keyID *url.URL) (voca | ||||||
| // Authenticate in this case is defined as making sure that the http request is actually signed by whoever claims | // Authenticate in this case is defined as making sure that the http request is actually signed by whoever claims | ||||||
| // to have signed it, by fetching the public key from the signature and checking it against the remote public key. | // to have signed it, by fetching the public key from the signature and checking it against the remote public key. | ||||||
| // | // | ||||||
| // To avoid making unnecessary http calls towards blocked domains, this function *does* bail early if an instance-level domain block exists |  | ||||||
| // for the request from the incoming domain. However, it does not check whether individual blocks exist between the requesting user or domain |  | ||||||
| // and the requested user: this should be done elsewhere. |  | ||||||
| // |  | ||||||
| // The provided username will be used to generate a transport for making remote requests/derefencing the public key ID of the request signature. | // The provided username will be used to generate a transport for making remote requests/derefencing the public key ID of the request signature. | ||||||
| // Ideally you should pass in the username of the user *being requested*, so that the remote server can decide how to handle the request based on who's making it. | // Ideally you should pass in the username of the user *being requested*, so that the remote server can decide how to handle the request based on who's making it. | ||||||
| // Ie., if the request on this server is for https://example.org/users/some_username then you should pass in the username 'some_username'. | // Ie., if the request on this server is for https://example.org/users/some_username then you should pass in the username 'some_username'. | ||||||
|  | @ -135,6 +131,19 @@ func (f *federator) AuthenticateFederatedRequest(ctx context.Context, requestedU | ||||||
| 		return nil, false, nil // couldn't extract the verifier | 		return nil, false, nil // couldn't extract the verifier | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	// we should have the signature itself set too | ||||||
|  | 	si := ctx.Value(util.APRequestingPublicKeySignature) | ||||||
|  | 	if vi == nil { | ||||||
|  | 		l.Debug("request wasn't signed") | ||||||
|  | 		return nil, false, nil // request wasn't signed | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	signature, ok := si.(string) | ||||||
|  | 	if !ok { | ||||||
|  | 		l.Debug("couldn't extract signature") | ||||||
|  | 		return nil, false, nil // couldn't extract the signature | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	requestingPublicKeyID, err := url.Parse(verifier.KeyId()) | 	requestingPublicKeyID, err := url.Parse(verifier.KeyId()) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		l.Debug("couldn't parse public key URL") | 		l.Debug("couldn't parse public key URL") | ||||||
|  | @ -227,13 +236,14 @@ func (f *federator) AuthenticateFederatedRequest(ctx context.Context, requestedU | ||||||
| 
 | 
 | ||||||
| 	for _, algo := range algos { | 	for _, algo := range algos { | ||||||
| 		l.Tracef("trying algo: %s", algo) | 		l.Tracef("trying algo: %s", algo) | ||||||
| 		if err := verifier.Verify(publicKey, algo); err == nil { | 		err := verifier.Verify(publicKey, algo) | ||||||
|  | 		if err == nil { | ||||||
| 			l.Tracef("authentication for %s PASSED with algorithm %s", pkOwnerURI, algo) | 			l.Tracef("authentication for %s PASSED with algorithm %s", pkOwnerURI, algo) | ||||||
| 			return pkOwnerURI, true, nil | 			return pkOwnerURI, true, nil | ||||||
| 		} | 		} | ||||||
| 		l.Tracef("authentication for %s NOT PASSED with algorithm %s: %s", pkOwnerURI, algo, err) | 		l.Tracef("authentication for %s NOT PASSED with algorithm %s: %s", pkOwnerURI, algo, err) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	l.Infof("authentication not passed for %s", pkOwnerURI) | 	l.Infof("authentication not passed for public key owner %s; signature value was '%s'", pkOwnerURI, signature) | ||||||
| 	return nil, false, nil | 	return nil, false, nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -128,12 +128,13 @@ func (suite *ProtocolTestSuite) TestAuthenticatePostInbox() { | ||||||
| 	ctxWithAccount := context.WithValue(ctx, util.APAccount, inboxAccount) | 	ctxWithAccount := context.WithValue(ctx, util.APAccount, inboxAccount) | ||||||
| 	ctxWithActivity := context.WithValue(ctxWithAccount, util.APActivity, activity) | 	ctxWithActivity := context.WithValue(ctxWithAccount, util.APActivity, activity) | ||||||
| 	ctxWithVerifier := context.WithValue(ctxWithActivity, util.APRequestingPublicKeyVerifier, verifier) | 	ctxWithVerifier := context.WithValue(ctxWithActivity, util.APRequestingPublicKeyVerifier, verifier) | ||||||
|  | 	ctxWithSignature := context.WithValue(ctxWithVerifier, util.APRequestingPublicKeySignature, activity.SignatureHeader) | ||||||
| 
 | 
 | ||||||
| 	// we can pass this recorder as a writer and read it back after | 	// we can pass this recorder as a writer and read it back after | ||||||
| 	recorder := httptest.NewRecorder() | 	recorder := httptest.NewRecorder() | ||||||
| 
 | 
 | ||||||
| 	// trigger the function being tested, and return the new context it creates | 	// trigger the function being tested, and return the new context it creates | ||||||
| 	newContext, authed, err := federator.AuthenticatePostInbox(ctxWithVerifier, recorder, request) | 	newContext, authed, err := federator.AuthenticatePostInbox(ctxWithSignature, recorder, request) | ||||||
| 	assert.NoError(suite.T(), err) | 	assert.NoError(suite.T(), err) | ||||||
| 	assert.True(suite.T(), authed) | 	assert.True(suite.T(), authed) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -72,6 +72,8 @@ const ( | ||||||
| 	APRequestingActorIRI APContextKey = "requestingActorIRI" | 	APRequestingActorIRI APContextKey = "requestingActorIRI" | ||||||
| 	// APRequestingPublicKeyVerifier can be used to set and retrieve the public key verifier of an incoming federation request. | 	// APRequestingPublicKeyVerifier can be used to set and retrieve the public key verifier of an incoming federation request. | ||||||
| 	APRequestingPublicKeyVerifier APContextKey = "requestingPublicKeyVerifier" | 	APRequestingPublicKeyVerifier APContextKey = "requestingPublicKeyVerifier" | ||||||
|  | 	// APRequestingPublicKeySignature can be used to set and retrieve the value of the signature header of an incoming federation request. | ||||||
|  | 	APRequestingPublicKeySignature APContextKey = "requestingPublicKeySignature" | ||||||
| 	// APFromFederatorChanKey can be used to pass a pointer to the fromFederator channel into the federator for use in callbacks. | 	// APFromFederatorChanKey can be used to pass a pointer to the fromFederator channel into the federator for use in callbacks. | ||||||
| 	APFromFederatorChanKey APContextKey = "fromFederatorChan" | 	APFromFederatorChanKey APContextKey = "fromFederatorChan" | ||||||
| ) | ) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue