clientstore.go (1994B)
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 oauth 19 20 import ( 21 "context" 22 23 "github.com/superseriousbusiness/gotosocial/internal/db" 24 "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" 25 "github.com/superseriousbusiness/oauth2/v4" 26 "github.com/superseriousbusiness/oauth2/v4/models" 27 ) 28 29 type clientStore struct { 30 db db.Basic 31 } 32 33 // NewClientStore returns an implementation of the oauth2 ClientStore interface, using the given db as a storage backend. 34 func NewClientStore(db db.Basic) oauth2.ClientStore { 35 pts := &clientStore{ 36 db: db, 37 } 38 return pts 39 } 40 41 func (cs *clientStore) GetByID(ctx context.Context, clientID string) (oauth2.ClientInfo, error) { 42 poc := >smodel.Client{} 43 if err := cs.db.GetByID(ctx, clientID, poc); err != nil { 44 return nil, err 45 } 46 return models.New(poc.ID, poc.Secret, poc.Domain, poc.UserID), nil 47 } 48 49 func (cs *clientStore) Set(ctx context.Context, id string, cli oauth2.ClientInfo) error { 50 poc := >smodel.Client{ 51 ID: cli.GetID(), 52 Secret: cli.GetSecret(), 53 Domain: cli.GetDomain(), 54 UserID: cli.GetUserID(), 55 } 56 return cs.db.Put(ctx, poc) 57 } 58 59 func (cs *clientStore) Delete(ctx context.Context, id string) error { 60 poc := >smodel.Client{ 61 ID: id, 62 } 63 return cs.db.DeleteByID(ctx, id, poc) 64 }