2023-03-12 15:00:57 +00:00
|
|
|
// GoToSocial
|
|
|
|
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
//
|
|
|
|
// 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/>.
|
2021-05-17 18:06:58 +01:00
|
|
|
|
2021-05-30 12:12:00 +01:00
|
|
|
package processing
|
2021-05-08 13:25:55 +01:00
|
|
|
|
|
|
|
import (
|
2021-08-25 14:34:33 +01:00
|
|
|
"context"
|
|
|
|
|
2021-05-08 13:25:55 +01:00
|
|
|
"github.com/google/uuid"
|
|
|
|
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
2022-06-08 19:38:03 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
2021-05-08 13:25:55 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2021-06-13 17:42:28 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/id"
|
2021-05-08 13:25:55 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/oauth"
|
|
|
|
)
|
|
|
|
|
2023-02-22 15:05:26 +00:00
|
|
|
func (p *Processor) AppCreate(ctx context.Context, authed *oauth.Auth, form *apimodel.ApplicationCreateRequest) (*apimodel.Application, gtserror.WithCode) {
|
2021-10-04 14:24:19 +01:00
|
|
|
// set default 'read' for scopes if it's not set
|
2021-05-08 13:25:55 +01:00
|
|
|
var scopes string
|
|
|
|
if form.Scopes == "" {
|
|
|
|
scopes = "read"
|
|
|
|
} else {
|
|
|
|
scopes = form.Scopes
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate new IDs for this application and its associated client
|
2021-06-13 17:42:28 +01:00
|
|
|
clientID, err := id.NewRandomULID()
|
|
|
|
if err != nil {
|
2022-06-08 19:38:03 +01:00
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
2021-06-13 17:42:28 +01:00
|
|
|
}
|
2021-05-08 13:25:55 +01:00
|
|
|
clientSecret := uuid.NewString()
|
|
|
|
|
2021-06-13 17:42:28 +01:00
|
|
|
appID, err := id.NewRandomULID()
|
|
|
|
if err != nil {
|
2022-06-08 19:38:03 +01:00
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
2021-06-13 17:42:28 +01:00
|
|
|
}
|
|
|
|
|
2021-05-08 13:25:55 +01:00
|
|
|
// generate the application to put in the database
|
|
|
|
app := >smodel.Application{
|
2021-06-13 17:42:28 +01:00
|
|
|
ID: appID,
|
2021-05-08 13:25:55 +01:00
|
|
|
Name: form.ClientName,
|
|
|
|
Website: form.Website,
|
|
|
|
RedirectURI: form.RedirectURIs,
|
|
|
|
ClientID: clientID,
|
|
|
|
ClientSecret: clientSecret,
|
|
|
|
Scopes: scopes,
|
|
|
|
}
|
|
|
|
|
|
|
|
// chuck it in the db
|
2023-08-10 15:08:41 +01:00
|
|
|
if err := p.state.DB.PutApplication(ctx, app); err != nil {
|
2022-06-08 19:38:03 +01:00
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
2021-05-08 13:25:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// now we need to model an oauth client from the application that the oauth library can use
|
2021-09-01 10:45:01 +01:00
|
|
|
oc := >smodel.Client{
|
2021-05-08 13:25:55 +01:00
|
|
|
ID: clientID,
|
|
|
|
Secret: clientSecret,
|
|
|
|
Domain: form.RedirectURIs,
|
2022-06-08 19:38:03 +01:00
|
|
|
// This client isn't yet associated with a specific user, it's just an app client right now
|
|
|
|
UserID: "",
|
2021-05-08 13:25:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// chuck it in the db
|
2024-04-15 14:22:21 +01:00
|
|
|
if err := p.state.DB.PutClient(ctx, oc); err != nil {
|
2022-06-08 19:38:03 +01:00
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
2021-05-08 13:25:55 +01:00
|
|
|
}
|
|
|
|
|
2023-09-23 17:44:11 +01:00
|
|
|
apiApp, err := p.converter.AppToAPIAppSensitive(ctx, app)
|
2021-05-08 13:25:55 +01:00
|
|
|
if err != nil {
|
2022-06-08 19:38:03 +01:00
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
2021-05-08 13:25:55 +01:00
|
|
|
}
|
|
|
|
|
2021-10-04 14:24:19 +01:00
|
|
|
return apiApp, nil
|
2021-05-08 13:25:55 +01:00
|
|
|
}
|