webfingerget.go (3623B)
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 webfinger 19 20 import ( 21 "encoding/json" 22 "errors" 23 "fmt" 24 "net/http" 25 26 "github.com/gin-gonic/gin" 27 apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util" 28 "github.com/superseriousbusiness/gotosocial/internal/config" 29 "github.com/superseriousbusiness/gotosocial/internal/gtserror" 30 "github.com/superseriousbusiness/gotosocial/internal/util" 31 ) 32 33 // WebfingerGETRequest swagger:operation GET /.well-known/webfinger webfingerGet 34 // 35 // Handles webfinger account lookup requests. 36 // 37 // For example, a GET to `https://goblin.technology/.well-known/webfinger?resource=acct:tobi@goblin.technology` would return: 38 // 39 // ``` 40 // 41 // {"subject":"acct:tobi@goblin.technology","aliases":["https://goblin.technology/users/tobi","https://goblin.technology/@tobi"],"links":[{"rel":"http://webfinger.net/rel/profile-page","type":"text/html","href":"https://goblin.technology/@tobi"},{"rel":"self","type":"application/activity+json","href":"https://goblin.technology/users/tobi"}]} 42 // 43 // ``` 44 // 45 // See: https://webfinger.net/ 46 // 47 // --- 48 // tags: 49 // - .well-known 50 // 51 // produces: 52 // - application/jrd+json 53 // 54 // responses: 55 // '200': 56 // schema: 57 // "$ref": "#/definitions/wellKnownResponse" 58 func (m *Module) WebfingerGETRequest(c *gin.Context) { 59 if _, err := apiutil.NegotiateAccept(c, apiutil.WebfingerJSONAcceptHeaders...); err != nil { 60 apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1) 61 return 62 } 63 64 resourceQuery, set := c.GetQuery("resource") 65 if !set || resourceQuery == "" { 66 err := errors.New("no 'resource' in request query") 67 apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1) 68 return 69 } 70 71 requestedUsername, requestedHost, err := util.ExtractWebfingerParts(resourceQuery) 72 if err != nil { 73 err := fmt.Errorf("bad webfinger request with resource query %s: %w", resourceQuery, err) 74 apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1) 75 return 76 } 77 78 if requestedHost != config.GetHost() && requestedHost != config.GetAccountDomain() { 79 err := fmt.Errorf("requested host %s does not belong to this instance", requestedHost) 80 apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1) 81 return 82 } 83 84 resp, errWithCode := m.processor.Fedi().WebfingerGet(c.Request.Context(), requestedUsername) 85 if errWithCode != nil { 86 apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) 87 return 88 } 89 90 b, err := json.Marshal(resp) 91 if err != nil { 92 apiutil.ErrorHandler(c, gtserror.NewErrorInternalError(err), m.processor.InstanceGetV1) 93 return 94 } 95 96 // Always return "application/jrd+json" regardless of negotiated 97 // format. See https://www.rfc-editor.org/rfc/rfc7033#section-10.2 98 c.Data(http.StatusOK, string(apiutil.AppJRDJSON), b) 99 }