authorize.go (2128B)
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 stream 19 20 import ( 21 "context" 22 "fmt" 23 24 "github.com/superseriousbusiness/gotosocial/internal/db" 25 "github.com/superseriousbusiness/gotosocial/internal/gtserror" 26 "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" 27 ) 28 29 // Authorize returns an oauth2 token info in response to an access token query from the streaming API 30 func (p *Processor) Authorize(ctx context.Context, accessToken string) (*gtsmodel.Account, gtserror.WithCode) { 31 ti, err := p.oauthServer.LoadAccessToken(ctx, accessToken) 32 if err != nil { 33 err := fmt.Errorf("could not load access token: %s", err) 34 return nil, gtserror.NewErrorUnauthorized(err) 35 } 36 37 uid := ti.GetUserID() 38 if uid == "" { 39 err := fmt.Errorf("no userid in token") 40 return nil, gtserror.NewErrorUnauthorized(err) 41 } 42 43 user, err := p.state.DB.GetUserByID(ctx, uid) 44 if err != nil { 45 if err == db.ErrNoEntries { 46 err := fmt.Errorf("no user found for validated uid %s", uid) 47 return nil, gtserror.NewErrorUnauthorized(err) 48 } 49 return nil, gtserror.NewErrorInternalError(err) 50 } 51 52 acct, err := p.state.DB.GetAccountByID(ctx, user.AccountID) 53 if err != nil { 54 if err == db.ErrNoEntries { 55 err := fmt.Errorf("no account found for validated uid %s", uid) 56 return nil, gtserror.NewErrorUnauthorized(err) 57 } 58 return nil, gtserror.NewErrorInternalError(err) 59 } 60 61 return acct, nil 62 }