2021-05-17 18:06:58 +01:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2021-12-20 17:42:19 +00:00
|
|
|
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
|
2021-05-17 18:06:58 +01: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 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"
|
|
|
|
)
|
|
|
|
|
2022-06-08 19:38:03 +01: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
|
2021-08-25 14:34:33 +01:00
|
|
|
if err := p.db.Put(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
|
2021-08-25 14:34:33 +01:00
|
|
|
if err := p.db.Put(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
|
|
|
}
|
|
|
|
|
2021-10-04 14:24:19 +01:00
|
|
|
apiApp, err := p.tc.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
|
|
|
}
|