-
Notifications
You must be signed in to change notification settings - Fork 281
/
query_store.go
71 lines (61 loc) · 1.93 KB
/
query_store.go
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
// Package queryparam provides a query param based implementation of a both
// as session store and loader.
package queryparam
import (
"net/http"
"github.com/pomerium/pomerium/internal/encoding"
"github.com/pomerium/pomerium/internal/sessions"
)
var _ sessions.SessionStore = &Store{}
var _ sessions.SessionLoader = &Store{}
const (
defaultQueryParamKey = "pomerium_session"
)
// Store implements the load session store interface using http
// query strings / query parameters.
type Store struct {
queryParamKey string
encoder encoding.Marshaler
decoder encoding.Unmarshaler
}
// NewStore returns a new query param store for loading sessions from
// query strings / query parameters.
//
// NOTA BENE: By default, most servers _DO_ log query params, the leaking or
// accidental logging of which should be considered a security issue.
func NewStore(enc encoding.MarshalUnmarshaler, qp string) *Store {
if qp == "" {
qp = defaultQueryParamKey
}
return &Store{
queryParamKey: qp,
encoder: enc,
decoder: enc,
}
}
// LoadSession tries to retrieve the token string from URL query parameters.
func (qp *Store) LoadSession(r *http.Request) (string, error) {
jwt := r.URL.Query().Get(qp.queryParamKey)
if jwt == "" {
return "", sessions.ErrNoSessionFound
}
return jwt, nil
}
// ClearSession clears the session cookie from a request's query param key `pomerium_session`.
func (qp *Store) ClearSession(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()
params.Del(qp.queryParamKey)
r.URL.RawQuery = params.Encode()
}
// SaveSession sets a session to a request's query param key `pomerium_session`
func (qp *Store) SaveSession(w http.ResponseWriter, r *http.Request, x interface{}) error {
data, err := qp.encoder.Marshal(x)
if err != nil {
return err
}
r.URL.Query().Get(qp.queryParamKey)
params := r.URL.Query()
params.Set(qp.queryParamKey, string(data))
r.URL.RawQuery = params.Encode()
return nil
}