-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsession.go
73 lines (61 loc) · 1.63 KB
/
session.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
72
73
package oidcauth
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/gorilla/sessions"
"golang.org/x/oauth2"
)
func getSession(store sessions.Store, r *http.Request, sessionName string) (*sessions.Session, error) {
session, err := store.Get(r, sessionName)
if err != nil {
log.Printf("Failed to get session %s from store: %v", sessionName, err)
return nil, err
}
return session, nil
}
func getSessionStr(session *sessions.Session, valueName string) (string, error) {
value, ok := session.Values[valueName]
if !ok {
err := fmt.Errorf("Could not find value %s in session", valueName)
return "", err
}
str, ok := value.(string)
if !ok {
err := fmt.Errorf("Value %s is not a string", valueName)
return "", err
}
return str, nil
}
func setSessionStr(session *sessions.Session, valueName, value string) {
session.Values[valueName] = value
}
func getSessionToken(session *sessions.Session, valueName string) (*oauth2.Token, error) {
value, ok := session.Values[valueName]
if !ok {
err := fmt.Errorf("Could not find value %s in session", valueName)
return nil, err
}
b, ok := value.([]byte)
if !ok {
err := fmt.Errorf("Value %s is not a []byte", valueName)
return nil, err
}
token := new(oauth2.Token)
err := json.Unmarshal(b, token)
if err != nil {
log.Printf("Could not decode token value: %v", err)
return nil, err
}
return token, nil
}
func setSessionToken(session *sessions.Session, valueName string, value *oauth2.Token) error {
b, err := json.Marshal(value)
if err != nil {
log.Printf("Could not convert token to json: %v", err)
return err
}
session.Values[valueName] = b
return nil
}