summaryrefslogtreecommitdiff
path: root/internal/session/provider.go
blob: c321d629872ad298f4c2507735562b20910155fd (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package session

import (
	"encoding/json"
	"github.com/authelia/authelia/internal/utils"
	"time"

	"github.com/authelia/authelia/internal/configuration/schema"
	fasthttpsession "github.com/fasthttp/session"
	"github.com/valyala/fasthttp"
)

// Provider a session provider.
type Provider struct {
	sessionHolder *fasthttpsession.Session
	RememberMe    time.Duration
}

// NewProvider instantiate a session provider given a configuration.
func NewProvider(configuration schema.SessionConfiguration) *Provider {
	providerConfig := NewProviderConfig(configuration)

	provider := new(Provider)
	provider.sessionHolder = fasthttpsession.New(providerConfig.config)
	duration, err := utils.ParseDurationString(configuration.RememberMeDuration)
	if err != nil {
		panic(err)
	}
	provider.RememberMe = duration
	err = provider.sessionHolder.SetProvider(providerConfig.providerName, providerConfig.providerConfig)
	if err != nil {
		panic(err)
	}
	return provider
}

// GetSession return the user session from a request
func (p *Provider) GetSession(ctx *fasthttp.RequestCtx) (UserSession, error) {
	store, err := p.sessionHolder.Get(ctx)

	if err != nil {
		return NewDefaultUserSession(), err
	}

	userSessionJSON, ok := store.Get(userSessionStorerKey).([]byte)

	// If userSession is not yet defined we create the new session with default values
	// and save it in the store.
	if !ok {
		userSession := NewDefaultUserSession()
		store.Set(userSessionStorerKey, userSession)
		return userSession, nil
	}

	var userSession UserSession
	err = json.Unmarshal(userSessionJSON, &userSession)

	if err != nil {
		return NewDefaultUserSession(), err
	}

	return userSession, nil
}

// SaveSession save the user session.
func (p *Provider) SaveSession(ctx *fasthttp.RequestCtx, userSession UserSession) error {
	store, err := p.sessionHolder.Get(ctx)

	if err != nil {
		return err
	}

	userSessionJSON, err := json.Marshal(userSession)

	if err != nil {
		return err
	}

	store.Set(userSessionStorerKey, userSessionJSON)
	p.sessionHolder.Save(ctx, store)
	return nil
}

// RegenerateSession regenerate a session ID.
func (p *Provider) RegenerateSession(ctx *fasthttp.RequestCtx) error {
	_, err := p.sessionHolder.Regenerate(ctx)
	return err
}

// DestroySession destroy a session ID and delete the cookie.
func (p *Provider) DestroySession(ctx *fasthttp.RequestCtx) error {
	return p.sessionHolder.Destroy(ctx)
}

// UpdateExpiration update the expiration of the cookie and session.
func (p *Provider) UpdateExpiration(ctx *fasthttp.RequestCtx, expiration time.Duration) error {
	store, err := p.sessionHolder.Get(ctx)

	if err != nil {
		return err
	}

	err = store.SetExpiration(expiration)

	if err != nil {
		return err
	}

	p.sessionHolder.Save(ctx, store)
	return nil
}

// GetExpiration get the expiration of the current session.
func (p *Provider) GetExpiration(ctx *fasthttp.RequestCtx) (time.Duration, error) {
	store, err := p.sessionHolder.Get(ctx)

	if err != nil {
		return time.Duration(0), err
	}

	return store.GetExpiration(), nil
}