diff options
author | 2021-07-07 15:46:42 +0200 | |
---|---|---|
committer | 2021-07-07 15:46:42 +0200 | |
commit | c71e55ecc4c2381785b5f8ae10af74d8a537d6c3 (patch) | |
tree | d58bfed57b7232a9b254f8582f9725e2583f8ecd /internal/router/session.go | |
parent | Blocklist import (#77) (diff) | |
download | gotosocial-c71e55ecc4c2381785b5f8ae10af74d8a537d6c3.tar.xz |
clean up some weirdness in the router (#80)
Diffstat (limited to 'internal/router/session.go')
-rw-r--r-- | internal/router/session.go | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/internal/router/session.go b/internal/router/session.go new file mode 100644 index 000000000..a1ac09d28 --- /dev/null +++ b/internal/router/session.go @@ -0,0 +1,100 @@ +/* + GoToSocial + Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + + 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/>. +*/ + +package router + +import ( + "crypto/rand" + "errors" + "fmt" + + "github.com/gin-contrib/sessions" + "github.com/gin-contrib/sessions/memstore" + "github.com/gin-gonic/gin" + "github.com/superseriousbusiness/gotosocial/internal/config" + "github.com/superseriousbusiness/gotosocial/internal/db" + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/id" +) + +func useSession(cfg *config.Config, dbService db.DB, engine *gin.Engine) error { + // check if we have a saved router session already + routerSessions := []*gtsmodel.RouterSession{} + if err := dbService.GetAll(&routerSessions); err != nil { + if _, ok := err.(db.ErrNoEntries); !ok { + // proper error occurred + return err + } + } + + var rs *gtsmodel.RouterSession + if len(routerSessions) == 1 { + // we have a router session stored + rs = routerSessions[0] + } else if len(routerSessions) == 0 { + // we have no router sessions so we need to create a new one + var err error + rs, err = routerSession(dbService) + if err != nil { + return fmt.Errorf("error creating new router session: %s", err) + } + } else { + // we should only have one router session stored ever + return errors.New("we had more than one router session in the db") + } + + if rs == nil { + return errors.New("error getting or creating router session: router session was nil") + } + + store := memstore.NewStore(rs.Auth, rs.Crypt) + sessionName := fmt.Sprintf("gotosocial-%s", cfg.Host) + engine.Use(sessions.Sessions(sessionName, store)) + return nil +} + +// routerSession generates a new router session with random auth and crypt bytes, +// puts it in the database for persistence, and returns it for use. +func routerSession(dbService db.DB) (*gtsmodel.RouterSession, error) { + auth := make([]byte, 32) + crypt := make([]byte, 32) + + if _, err := rand.Read(auth); err != nil { + return nil, err + } + if _, err := rand.Read(crypt); err != nil { + return nil, err + } + + rid, err := id.NewULID() + if err != nil { + return nil, err + } + + rs := >smodel.RouterSession{ + ID: rid, + Auth: auth, + Crypt: crypt, + } + + if err := dbService.Put(rs); err != nil { + return nil, err + } + + return rs, nil +} |