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