appcreate.go (4204B)
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 apps 19 20 import ( 21 "fmt" 22 "net/http" 23 24 "github.com/gin-gonic/gin" 25 apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" 26 apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util" 27 "github.com/superseriousbusiness/gotosocial/internal/gtserror" 28 "github.com/superseriousbusiness/gotosocial/internal/oauth" 29 ) 30 31 // these consts are used to ensure users can't spam huge entries into our database 32 const ( 33 formFieldLen = 1024 34 formRedirectLen = 2056 35 ) 36 37 // AppsPOSTHandler swagger:operation POST /api/v1/apps appCreate 38 // 39 // Register a new application on this instance. 40 // 41 // The registered application can be used to obtain an application token. 42 // This can then be used to register a new account, or (through user auth) obtain an access token. 43 // 44 // The parameters can also be given in the body of the request, as JSON, if the content-type is set to 'application/json'. 45 // The parameters can also be given in the body of the request, as XML, if the content-type is set to 'application/xml'. 46 // 47 // --- 48 // tags: 49 // - apps 50 // 51 // consumes: 52 // - application/json 53 // - application/xml 54 // - application/x-www-form-urlencoded 55 // 56 // produces: 57 // - application/json 58 // 59 // responses: 60 // '200': 61 // description: "The newly-created application." 62 // schema: 63 // "$ref": "#/definitions/application" 64 // '400': 65 // description: bad request 66 // '401': 67 // description: unauthorized 68 // '403': 69 // description: forbidden 70 // '404': 71 // description: not found 72 // '406': 73 // description: not acceptable 74 // '500': 75 // description: internal server error 76 func (m *Module) AppsPOSTHandler(c *gin.Context) { 77 authed, err := oauth.Authed(c, false, false, false, false) 78 if err != nil { 79 apiutil.ErrorHandler(c, gtserror.NewErrorUnauthorized(err, err.Error()), m.processor.InstanceGetV1) 80 return 81 } 82 83 if _, err := apiutil.NegotiateAccept(c, apiutil.JSONAcceptHeaders...); err != nil { 84 apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1) 85 return 86 } 87 88 form := &apimodel.ApplicationCreateRequest{} 89 if err := c.ShouldBind(form); err != nil { 90 apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1) 91 return 92 } 93 94 if len([]rune(form.ClientName)) > formFieldLen { 95 err := fmt.Errorf("client_name must be less than %d characters", formFieldLen) 96 apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1) 97 return 98 } 99 100 if len([]rune(form.RedirectURIs)) > formRedirectLen { 101 err := fmt.Errorf("redirect_uris must be less than %d characters", formRedirectLen) 102 apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1) 103 return 104 } 105 106 if len([]rune(form.Scopes)) > formFieldLen { 107 err := fmt.Errorf("scopes must be less than %d characters", formFieldLen) 108 apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1) 109 return 110 } 111 112 if len([]rune(form.Website)) > formFieldLen { 113 err := fmt.Errorf("website must be less than %d characters", formFieldLen) 114 apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1) 115 return 116 } 117 118 apiApp, errWithCode := m.processor.AppCreate(c.Request.Context(), authed, form) 119 if errWithCode != nil { 120 apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) 121 return 122 } 123 124 c.JSON(http.StatusOK, apiApp) 125 }