gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

app.go (2789B)


      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 processing
     19 
     20 import (
     21 	"context"
     22 
     23 	"github.com/google/uuid"
     24 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
     25 	"github.com/superseriousbusiness/gotosocial/internal/gtserror"
     26 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
     27 	"github.com/superseriousbusiness/gotosocial/internal/id"
     28 	"github.com/superseriousbusiness/gotosocial/internal/oauth"
     29 )
     30 
     31 func (p *Processor) AppCreate(ctx context.Context, authed *oauth.Auth, form *apimodel.ApplicationCreateRequest) (*apimodel.Application, gtserror.WithCode) {
     32 	// set default 'read' for scopes if it's not set
     33 	var scopes string
     34 	if form.Scopes == "" {
     35 		scopes = "read"
     36 	} else {
     37 		scopes = form.Scopes
     38 	}
     39 
     40 	// generate new IDs for this application and its associated client
     41 	clientID, err := id.NewRandomULID()
     42 	if err != nil {
     43 		return nil, gtserror.NewErrorInternalError(err)
     44 	}
     45 	clientSecret := uuid.NewString()
     46 
     47 	appID, err := id.NewRandomULID()
     48 	if err != nil {
     49 		return nil, gtserror.NewErrorInternalError(err)
     50 	}
     51 
     52 	// generate the application to put in the database
     53 	app := &gtsmodel.Application{
     54 		ID:           appID,
     55 		Name:         form.ClientName,
     56 		Website:      form.Website,
     57 		RedirectURI:  form.RedirectURIs,
     58 		ClientID:     clientID,
     59 		ClientSecret: clientSecret,
     60 		Scopes:       scopes,
     61 	}
     62 
     63 	// chuck it in the db
     64 	if err := p.state.DB.Put(ctx, app); err != nil {
     65 		return nil, gtserror.NewErrorInternalError(err)
     66 	}
     67 
     68 	// now we need to model an oauth client from the application that the oauth library can use
     69 	oc := &gtsmodel.Client{
     70 		ID:     clientID,
     71 		Secret: clientSecret,
     72 		Domain: form.RedirectURIs,
     73 		// This client isn't yet associated with a specific user,  it's just an app client right now
     74 		UserID: "",
     75 	}
     76 
     77 	// chuck it in the db
     78 	if err := p.state.DB.Put(ctx, oc); err != nil {
     79 		return nil, gtserror.NewErrorInternalError(err)
     80 	}
     81 
     82 	apiApp, err := p.tc.AppToAPIAppSensitive(ctx, app)
     83 	if err != nil {
     84 		return nil, gtserror.NewErrorInternalError(err)
     85 	}
     86 
     87 	return apiApp, nil
     88 }