git.iko.soy/typeable/oauth2-proxy
on master help sign in
Simplify cookie creation form *options.CookieOptions
76bd237Joel Speed
pkg/cookies/cookies.go+7
@@ -7,6 +7,7 @@ import (
7 "time"
7 "time"
8
8
9 "github.com/pusher/oauth2_proxy/logger"
9 "github.com/pusher/oauth2_proxy/logger"
10+ "github.com/pusher/oauth2_proxy/pkg/apis/options"
10)
11)
11
12
12// MakeCookie constructs a cookie from the given parameters,
13// MakeCookie constructs a cookie from the given parameters,
@@ -32,3 +33,9 @@ func MakeCookie(req *http.Request, name string, value string, path string, domai
32 Expires: now.Add(expiration),
33 Expires: now.Add(expiration),
33 }
34 }
34}
35}
36+
37+// MakeCookieFromOptions constructs a cookie based on the givemn *options.CookieOptions,
38+// value and creation time
39+func MakeCookieFromOptions(req *http.Request, name string, value string, opts *options.CookieOptions, expiration time.Duration, now time.Time) *http.Cookie {
40+ return MakeCookie(req, name, value, opts.CookiePath, opts.CookieDomain, opts.CookieHTTPOnly, opts.CookieSecure, expiration, now)
41+}
pkg/sessions/cookie/session_store.go+15−30
@@ -27,14 +27,8 @@ var _ sessions.SessionStore = &SessionStore{}
27// SessionStore is an implementation of the sessions.SessionStore
27// SessionStore is an implementation of the sessions.SessionStore
28// interface that stores sessions in client side cookies
28// interface that stores sessions in client side cookies
29type SessionStore struct {
29type SessionStore struct {
30+ CookieOptions *options.CookieOptions
30− CookieCipher *cookie.Cipher
31+ CookieCipher *cookie.Cipher
31− CookieDomain string
32− CookieExpire time.Duration
33− CookieHTTPOnly bool
34− CookieName string
35− CookiePath string
36− CookieSecret string
37− CookieSecure bool
38}
32}
39
33
40// Save takes a sessions.SessionState and stores the information from it
34// Save takes a sessions.SessionState and stores the information from it
@@ -54,12 +48,12 @@ func (s *SessionStore) Save(rw http.ResponseWriter, req *http.Request, ss *sessi
54// Load reads sessions.SessionState information from Cookies within the
48// Load reads sessions.SessionState information from Cookies within the
55// HTTP request object
49// HTTP request object
56func (s *SessionStore) Load(req *http.Request) (*sessions.SessionState, error) {
50func (s *SessionStore) Load(req *http.Request) (*sessions.SessionState, error) {
57− c, err := loadCookie(req, s.CookieName)
51+ c, err := loadCookie(req, s.CookieOptions.CookieName)
58 if err != nil {
52 if err != nil {
59 // always http.ErrNoCookie
53 // always http.ErrNoCookie
60− return nil, fmt.Errorf("Cookie %q not present", s.CookieName)
54+ return nil, fmt.Errorf("Cookie %q not present", s.CookieOptions.CookieName)
61 }
55 }
62− val, _, ok := cookie.Validate(c, s.CookieSecret, s.CookieExpire)
56+ val, _, ok := cookie.Validate(c, s.CookieOptions.CookieSecret, s.CookieOptions.CookieExpire)
63 if !ok {
57 if !ok {
64 return nil, errors.New("Cookie Signature not valid")
58 return nil, errors.New("Cookie Signature not valid")
65 }
59 }
@@ -77,7 +71,7 @@ func (s *SessionStore) Clear(rw http.ResponseWriter, req *http.Request) error {
77 var cookies []*http.Cookie
71 var cookies []*http.Cookie
78
72
79 // matches CookieName, CookieName_<number>
73 // matches CookieName, CookieName_<number>
80− var cookieNameRegex = regexp.MustCompile(fmt.Sprintf("^%s(_\\d+)?$", s.CookieName))
74+ var cookieNameRegex = regexp.MustCompile(fmt.Sprintf("^%s(_\\d+)?$", s.CookieOptions.CookieName))
81
75
82 for _, c := range req.Cookies() {
76 for _, c := range req.Cookies() {
83 if cookieNameRegex.MatchString(c.Name) {
77 if cookieNameRegex.MatchString(c.Name) {
@@ -93,33 +87,30 @@ func (s *SessionStore) Clear(rw http.ResponseWriter, req *http.Request) error {
93
87
94// setSessionCookie adds the user's session cookie to the response
88// setSessionCookie adds the user's session cookie to the response
95func (s *SessionStore) setSessionCookie(rw http.ResponseWriter, req *http.Request, val string, created time.Time) {
89func (s *SessionStore) setSessionCookie(rw http.ResponseWriter, req *http.Request, val string, created time.Time) {
96− for _, c := range s.makeSessionCookie(req, val, s.CookieExpire, created) {
90+ for _, c := range s.makeSessionCookie(req, val, created) {
97 http.SetCookie(rw, c)
91 http.SetCookie(rw, c)
98 }
92 }
99}
93}
100
94
101// makeSessionCookie creates an http.Cookie containing the authenticated user's
95// makeSessionCookie creates an http.Cookie containing the authenticated user's
102// authentication details
96// authentication details
103−func (s *SessionStore) makeSessionCookie(req *http.Request, value string, expiration time.Duration, now time.Time) []*http.Cookie {
97+func (s *SessionStore) makeSessionCookie(req *http.Request, value string, now time.Time) []*http.Cookie {
104 if value != "" {
98 if value != "" {
105− value = cookie.SignedValue(s.CookieSecret, s.CookieName, value, now)
99+ value = cookie.SignedValue(s.CookieOptions.CookieSecret, s.CookieOptions.CookieName, value, now)
106 }
100 }
107− c := s.makeCookie(req, s.CookieName, value, expiration, now)
101+ c := s.makeCookie(req, s.CookieOptions.CookieName, value, s.CookieOptions.CookieExpire, now)
108− if len(c.Value) > 4096-len(s.CookieName) {
102+ if len(c.Value) > 4096-len(s.CookieOptions.CookieName) {
109 return splitCookie(c)
103 return splitCookie(c)
110 }
104 }
111 return []*http.Cookie{c}
105 return []*http.Cookie{c}
112}
106}
113
107
114func (s *SessionStore) makeCookie(req *http.Request, name string, value string, expiration time.Duration, now time.Time) *http.Cookie {
108func (s *SessionStore) makeCookie(req *http.Request, name string, value string, expiration time.Duration, now time.Time) *http.Cookie {
115− return cookies.MakeCookie(
109+ return cookies.MakeCookieFromOptions(
116 req,
110 req,
117 name,
111 name,
118 value,
112 value,
119− s.CookiePath,
113+ s.CookieOptions,
120− s.CookieDomain,
121− s.CookieHTTPOnly,
122− s.CookieSecure,
123 expiration,
114 expiration,
124 now,
115 now,
125 )
116 )
@@ -138,14 +129,8 @@ func NewCookieSessionStore(opts options.CookieStoreOptions, cookieOpts *options.
138 }
129 }
139
130
140 return &SessionStore{
131 return &SessionStore{
141− CookieCipher: cipher,
132+ CookieCipher: cipher,
142− CookieDomain: cookieOpts.CookieDomain,
133+ CookieOptions: cookieOpts,
143− CookieExpire: cookieOpts.CookieExpire,
144− CookieHTTPOnly: cookieOpts.CookieHTTPOnly,
145− CookieName: cookieOpts.CookieName,
146− CookiePath: cookieOpts.CookiePath,
147− CookieSecret: cookieOpts.CookieSecret,
148− CookieSecure: cookieOpts.CookieSecure,
149 }, nil
134 }, nil
150}
135}
151
136