summaryrefslogtreecommitdiff
path: root/internal/router/session.go
blob: a1ac09d28b30f59d3643f6a7c9ed4f5e8ad6c319 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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 := &gtsmodel.RouterSession{
		ID:    rid,
		Auth:  auth,
		Crypt: crypt,
	}

	if err := dbService.Put(rs); err != nil {
		return nil, err
	}

	return rs, nil
}