-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cw,am|#14] implemented user registration, login/auth, introduced
in-memory session token cache, modified user table to include hashed password and salt, implement session methods for correlating userIds to session tokens and vice versa ❀‿❀ -- yay!! (づ。◕‿‿◕。)づ -- wheeeeee ╰(◡‿◡✿╰) -- whoooooo TODOS: * use CookieJar instead of Cookie? * standardize form vs. data for requests * implement session token expiration policy goroutine * actually send responses to clients (send session token cookie as well) * write unit test for all of this ;___;
- Loading branch information
connorwalsh
committed
Feb 22, 2018
1 parent
02e00a4
commit bddad2f
Showing
7 changed files
with
265 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package consts | ||
|
||
const ( | ||
LOGIN = "login" | ||
CREATE = "create" | ||
READ = "read" | ||
UPDATE = "update" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package core | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
uuid "github.com/satori/go.uuid" | ||
) | ||
|
||
type Sessions struct { | ||
sync.Mutex | ||
TokenToUser map[string]string | ||
UserToToken map[string]string | ||
TokenLastSeen map[string]time.Time | ||
} | ||
|
||
func NewSessions() *Sessions { | ||
return &Sessions{ | ||
TokenToUser: map[string]string{}, | ||
UserToToken: map[string]string{}, | ||
TokenLastSeen: map[string]time.Time{}, | ||
} | ||
} | ||
|
||
func (s *Sessions) GetTokenByUser(userID string) string { | ||
var ( | ||
token string | ||
exists bool | ||
) | ||
|
||
// lock writer (since this will be called concurrently) | ||
s.Lock() | ||
defer s.Unlock() | ||
|
||
token, exists = s.UserToToken[userID] | ||
if !exists { | ||
// create new session token for user | ||
token = uuid.NewV4().String() | ||
s.TokenToUser[token] = userID | ||
s.UserToToken[userID] = token | ||
} | ||
|
||
// update the token last seen | ||
s.TokenLastSeen[token] = time.Now() | ||
|
||
return token | ||
} | ||
|
||
func (s *Sessions) GetUserByToken(token string) (string, bool) { | ||
var ( | ||
userId string | ||
exists bool | ||
) | ||
|
||
s.Lock() | ||
defer s.Unlock() | ||
|
||
userId, exists = s.TokenToUser[token] | ||
if exists { | ||
// update the token last seen | ||
s.TokenLastSeen[token] = time.Now() | ||
} | ||
|
||
return userId, exists | ||
} | ||
|
||
// TODO we eventually want to have a go-routine constantly running in the background | ||
// at a specified interval which will expire and evict session tokens if no requests | ||
// have been made by a user in a certain time window. | ||
func (s *Sessions) ExpireSessions() { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.