-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
96 additions
and
17 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
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,45 @@ | ||
package security | ||
|
||
import ( | ||
"encoding/hex" | ||
"sync" | ||
) | ||
|
||
// ServerSession is a session for a user. | ||
type ServerSession struct { | ||
AvatarURL string `json:"avatar_url"` | ||
EMail string `json:"email"` | ||
Login string `json:"login"` | ||
Name string `json:"name"` | ||
} | ||
|
||
// ServerSessions is a thread-safe map of email addresses to tokens. | ||
type ServerSessions struct { | ||
sessions map[string]ServerSession | ||
mutex sync.RWMutex | ||
} | ||
|
||
// NewServerSessions creates a new serverSessions. | ||
func NewServerSessions() *ServerSessions { | ||
return &ServerSessions{ | ||
sessions: make(map[string]ServerSession), | ||
} | ||
} | ||
|
||
// Update adds a new session to the serverSessions. | ||
func (a *ServerSessions) Update(info ServerSession) (sessionID string) { | ||
a.mutex.Lock() | ||
defer a.mutex.Unlock() | ||
bytes := GenerateKey() | ||
sessionID = hex.EncodeToString(bytes[:]) | ||
a.sessions[sessionID] = info | ||
return sessionID | ||
} | ||
|
||
// Get returns the session for the given sessionID. | ||
func (a *ServerSessions) Get(sessionID string) (ServerSession, bool) { | ||
a.mutex.RLock() | ||
defer a.mutex.RUnlock() | ||
info, ok := a.sessions[sessionID] | ||
return info, ok | ||
} |
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,23 @@ | ||
package security_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/andygeiss/cloud-native-utils/assert" | ||
"github.com/andygeiss/cloud-native-utils/security" | ||
) | ||
|
||
func TestServerSessions_Update(t *testing.T) { | ||
sessions := security.NewServerSessions() | ||
token := sessions.Update(security.ServerSession{AvatarURL: "avatar_url", EMail: "email", Login: "login", Name: "name"}) | ||
assert.That(t, "token is correct", len(token), 64) | ||
} | ||
|
||
func TestServerSessions_Get(t *testing.T) { | ||
sessions := security.NewServerSessions() | ||
session := security.ServerSession{AvatarURL: "avatar_url", EMail: "email", Login: "login", Name: "name"} | ||
token := sessions.Update(session) | ||
current, found := sessions.Get(token) | ||
assert.That(t, "session must be found", found, true) | ||
assert.That(t, "session is correct", current, session) | ||
} |