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