gtsocial-umbx

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

util.go (2657B)


      1 package oauth
      2 
      3 import (
      4 	"github.com/gin-gonic/gin"
      5 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
      6 	"github.com/superseriousbusiness/oauth2/v4"
      7 	"github.com/superseriousbusiness/oauth2/v4/errors"
      8 )
      9 
     10 // Auth wraps an authorized token, application, user, and account.
     11 // It is used in the functions GetAuthed and MustAuth.
     12 // Because the user might *not* be authed, any of the fields in this struct
     13 // might be nil, so make sure to check that when you're using this struct anywhere.
     14 type Auth struct {
     15 	Token       oauth2.TokenInfo
     16 	Application *gtsmodel.Application
     17 	User        *gtsmodel.User
     18 	Account     *gtsmodel.Account
     19 }
     20 
     21 // Authed is a convenience function for returning an Authed struct from a gin context.
     22 // In essence, it tries to extract a token, application, user, and account from the context,
     23 // and then sets them on a struct for convenience.
     24 //
     25 // If any are not present in the context, they will be set to nil on the returned Authed struct.
     26 //
     27 // If *ALL* are not present, then nil and an error will be returned.
     28 //
     29 // If something goes wrong during parsing, then nil and an error will be returned (consider this not authed).
     30 // Authed is like GetAuthed, but will fail if one of the requirements is not met.
     31 func Authed(c *gin.Context, requireToken bool, requireApp bool, requireUser bool, requireAccount bool) (*Auth, error) {
     32 	ctx := c.Copy()
     33 	a := &Auth{}
     34 	var i interface{}
     35 	var ok bool
     36 
     37 	i, ok = ctx.Get(SessionAuthorizedToken)
     38 	if ok {
     39 		parsed, ok := i.(oauth2.TokenInfo)
     40 		if !ok {
     41 			return nil, errors.New("could not parse token from session context")
     42 		}
     43 		a.Token = parsed
     44 	}
     45 
     46 	i, ok = ctx.Get(SessionAuthorizedApplication)
     47 	if ok {
     48 		parsed, ok := i.(*gtsmodel.Application)
     49 		if !ok {
     50 			return nil, errors.New("could not parse application from session context")
     51 		}
     52 		a.Application = parsed
     53 	}
     54 
     55 	i, ok = ctx.Get(SessionAuthorizedUser)
     56 	if ok {
     57 		parsed, ok := i.(*gtsmodel.User)
     58 		if !ok {
     59 			return nil, errors.New("could not parse user from session context")
     60 		}
     61 		a.User = parsed
     62 	}
     63 
     64 	i, ok = ctx.Get(SessionAuthorizedAccount)
     65 	if ok {
     66 		parsed, ok := i.(*gtsmodel.Account)
     67 		if !ok {
     68 			return nil, errors.New("could not parse account from session context")
     69 		}
     70 		a.Account = parsed
     71 	}
     72 
     73 	if requireToken && a.Token == nil {
     74 		return nil, errors.New("token not supplied")
     75 	}
     76 
     77 	if requireApp && a.Application == nil {
     78 		return nil, errors.New("application not supplied")
     79 	}
     80 
     81 	if requireUser && a.User == nil {
     82 		return nil, errors.New("user not supplied or not authorized")
     83 	}
     84 
     85 	if requireAccount && a.Account == nil {
     86 		return nil, errors.New("account not supplied or not authorized")
     87 	}
     88 
     89 	return a, nil
     90 }