sessions: give ability to the end-user to modify the cookie via context.CookieOption on Start and Update/ShiftExpiration as requested at: https://github.com/kataras/iris/issues/1186, add a StartWithPath helper as well

Former-commit-id: a9f8715b07049a5720a38c9352bb1ff781dfc04d
This commit is contained in:
Gerasimos (Makis) Maropoulos 2019-02-16 21:03:48 +02:00
parent 9cfaff07d6
commit 6ca19e0bca

View File

@ -35,7 +35,7 @@ func (s *Sessions) UseDatabase(db Database) {
} }
// updateCookie gains the ability of updating the session browser cookie to any method which wants to update it // updateCookie gains the ability of updating the session browser cookie to any method which wants to update it
func (s *Sessions) updateCookie(ctx context.Context, sid string, expires time.Duration) { func (s *Sessions) updateCookie(ctx context.Context, sid string, expires time.Duration, options ...context.CookieOption) {
cookie := &http.Cookie{} cookie := &http.Cookie{}
// The RFC makes no mention of encoding url value, so here I think to encode both sessionid key and the value using the safe(to put and to use as cookie) url-encoding // The RFC makes no mention of encoding url value, so here I think to encode both sessionid key and the value using the safe(to put and to use as cookie) url-encoding
@ -65,11 +65,16 @@ func (s *Sessions) updateCookie(ctx context.Context, sid string, expires time.Du
// encode the session id cookie client value right before send it. // encode the session id cookie client value right before send it.
cookie.Value = s.encodeCookieValue(cookie.Value) cookie.Value = s.encodeCookieValue(cookie.Value)
for _, opt := range options {
opt(cookie)
}
AddCookie(ctx, cookie, s.config.AllowReclaim) AddCookie(ctx, cookie, s.config.AllowReclaim)
} }
// Start should start the session for the particular request. // Start creates or retrieves an existing session for the particular request.
func (s *Sessions) Start(ctx context.Context) *Session { func (s *Sessions) Start(ctx context.Context, cookieOptions ...context.CookieOption) *Session {
cookieValue := s.decodeCookieValue(GetCookie(ctx, s.config.Cookie)) cookieValue := s.decodeCookieValue(GetCookie(ctx, s.config.Cookie))
if cookieValue == "" { // cookie doesn't exists, let's generate a session and add set a cookie if cookieValue == "" { // cookie doesn't exists, let's generate a session and add set a cookie
@ -78,7 +83,7 @@ func (s *Sessions) Start(ctx context.Context) *Session {
sess := s.provider.Init(sid, s.config.Expires) sess := s.provider.Init(sid, s.config.Expires)
sess.isNew = s.provider.db.Len(sid) == 0 sess.isNew = s.provider.db.Len(sid) == 0
s.updateCookie(ctx, sid, s.config.Expires) s.updateCookie(ctx, sid, s.config.Expires, cookieOptions...)
return sess return sess
} }
@ -88,18 +93,23 @@ func (s *Sessions) Start(ctx context.Context) *Session {
return sess return sess
} }
// StartWithPath same as `Start` but it explicitly accepts the cookie path option.
func (s *Sessions) StartWithPath(ctx context.Context, path string) *Session {
return s.Start(ctx, context.CookiePath(path))
}
// ShiftExpiration move the expire date of a session to a new date // ShiftExpiration move the expire date of a session to a new date
// by using session default timeout configuration. // by using session default timeout configuration.
// It will return `ErrNotImplemented` if a database is used and it does not support this feature, yet. // It will return `ErrNotImplemented` if a database is used and it does not support this feature, yet.
func (s *Sessions) ShiftExpiration(ctx context.Context) error { func (s *Sessions) ShiftExpiration(ctx context.Context, cookieOptions ...context.CookieOption) error {
return s.UpdateExpiration(ctx, s.config.Expires) return s.UpdateExpiration(ctx, s.config.Expires, cookieOptions...)
} }
// UpdateExpiration change expire date of a session to a new date // UpdateExpiration change expire date of a session to a new date
// by using timeout value passed by `expires` receiver. // by using timeout value passed by `expires` receiver.
// It will return `ErrNotFound` when trying to update expiration on a non-existence or not valid session entry. // It will return `ErrNotFound` when trying to update expiration on a non-existence or not valid session entry.
// It will return `ErrNotImplemented` if a database is used and it does not support this feature, yet. // It will return `ErrNotImplemented` if a database is used and it does not support this feature, yet.
func (s *Sessions) UpdateExpiration(ctx context.Context, expires time.Duration) error { func (s *Sessions) UpdateExpiration(ctx context.Context, expires time.Duration, cookieOptions ...context.CookieOption) error {
cookieValue := s.decodeCookieValue(GetCookie(ctx, s.config.Cookie)) cookieValue := s.decodeCookieValue(GetCookie(ctx, s.config.Cookie))
if cookieValue == "" { if cookieValue == "" {
return ErrNotFound return ErrNotFound
@ -108,7 +118,7 @@ func (s *Sessions) UpdateExpiration(ctx context.Context, expires time.Duration)
// we should also allow it to expire when the browser closed // we should also allow it to expire when the browser closed
err := s.provider.UpdateExpiration(cookieValue, expires) err := s.provider.UpdateExpiration(cookieValue, expires)
if err == nil || expires == -1 { if err == nil || expires == -1 {
s.updateCookie(ctx, cookieValue, expires) s.updateCookie(ctx, cookieValue, expires, cookieOptions...)
} }
return err return err