forked from ant1441/ldap_proxy
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ldap_session.go
59 lines (48 loc) · 1.49 KB
/
ldap_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
package main
import (
"errors"
"fmt"
"net/http"
"time"
"github.com/skybet/ldap_proxy/cookie"
)
func (p *LdapProxy) ClearSessionCookie(rw http.ResponseWriter, req *http.Request) {
http.SetCookie(rw, p.MakeSessionCookie(req, "", time.Hour*-1, time.Now()))
}
func (p *LdapProxy) SetSessionCookie(rw http.ResponseWriter, req *http.Request, val string) {
http.SetCookie(rw, p.MakeSessionCookie(req, val, p.CookieExpire, time.Now()))
}
func (p *LdapProxy) LoadCookiedSession(req *http.Request) (*SessionState, time.Duration, error) {
var age time.Duration
c, err := req.Cookie(p.CookieName)
if err != nil {
// always http.ErrNoCookie
return nil, age, fmt.Errorf("Cookie %q not present", p.CookieName)
}
val, timestamp, ok := cookie.Validate(c, p.CookieSeed, p.CookieExpire)
if !ok {
return nil, age, errors.New("Cookie Signature not valid")
}
session, err := SessionFromCookie(val, p.CookieCipher)
if err != nil {
return nil, age, err
}
age = time.Now().Truncate(time.Second).Sub(timestamp)
return session, age, nil
}
func (p *LdapProxy) SaveSession(rw http.ResponseWriter, req *http.Request, s *SessionState) error {
value, err := CookieForSession(s, p.CookieCipher)
if err != nil {
return err
}
p.SetSessionCookie(rw, req, value)
return nil
}
func (p *LdapProxy) RefreshSessionIfNeeded(s *SessionState) (bool, error) {
// TODO: RefreshSessionIfNeeded
return false, nil
}
func (p *LdapProxy) ValidateSessionState(s *SessionState) bool {
// TODO: ValidateSessionState
return true
}