Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session: multisession and metadata #5

Merged
merged 2 commits into from
Oct 16, 2018
Merged

Session: multisession and metadata #5

merged 2 commits into from
Oct 16, 2018

Conversation

abraithwaite
Copy link
Owner

No description provided.

This CR nests multiple logins under one key.  This is a nice property
for operators because it keeps an individual's login session in one
place instead of having to build a lookup index elsewhere mapping
user->active sessions.

It uses lazy deletion and expiration checks.  There's a global
expiration which exists for memcache or redis auto-expiration.

There exists an inherent race if multiple sessions are being set at
once.  This race results in the last Set succeeding.  In practice, an
individual user shouldn't realistically be logging into two or more
devices at the same time.
This CR adds a field to the session which enables storing additional
optional metadata with the message. It's a byteslice so any data must be
pre-serialized before getting stored into the backend.

It can be retrieved through the ActiveSession method.
@@ -25,7 +25,7 @@ type server struct {
var email = []byte("super@exa::mple.com")

func (s *server) login(w http.ResponseWriter, r *http.Request) {
err := s.j.Set(r.Context(), w, email)
err := s.j.Set(r.Context(), w, email, []byte(r.UserAgent()))
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌

@@ -26,6 +26,7 @@ details.
- CSRF Protection
- Context aware
- Fast
- Multiple sessions under one key
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expand on this.

@@ -27,6 +27,9 @@ func (s *Store) Store(ctx context.Context, key, value []byte, exp time.Time) err

func (s *Store) Fetch(ctx context.Context, key []byte) ([]byte, error) {
i, err := s.mc.Get(string(key))
if err == memcache.ErrCacheMiss {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Satisfies the documented Fetch in the Storage interface.

@abraithwaite abraithwaite merged commit 2acf63d into master Oct 16, 2018
@abraithwaite abraithwaite deleted the multi-session branch October 16, 2018 00:29
This was referenced Oct 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant