Mercurial > gemma
view auth/session.go @ 191:01c5794608e0
Added simple makefiles to build back end and client.
author | Sascha Wilde <wilde@intevation.de> |
---|---|
date | Fri, 20 Jul 2018 17:41:44 +0200 |
parents | 83d798ea9f58 |
children | 1585c334e8a7 |
line wrap: on
line source
package auth import ( "crypto/rand" "encoding/base64" "io" "time" ) type Session struct { ExpiresAt int64 `json:"expires"` User string `json:"user"` Password string `json:"password"` Roles []string `json:"roles"` } const ( sessionKeyLength = 20 maxTokenValid = time.Hour * 3 ) func NewSession(user, password string, roles []string) *Session { // Create the Claims return &Session{ ExpiresAt: time.Now().Add(maxTokenValid).Unix(), User: user, Password: password, Roles: roles, } } func GenerateSessionKey() string { return base64.URLEncoding.EncodeToString(GenerateRandomKey(sessionKeyLength)) } func GenerateRandomKey(length int) []byte { k := make([]byte, length) if _, err := io.ReadFull(rand.Reader, k); err != nil { return nil } return k } func GenerateSession(user, password string) (string, *Session, error) { roles, err := AllOtherRoles(user, password) if err != nil { return "", nil, err } token := GenerateSessionKey() session := NewSession(user, password, roles) ConnPool.Add(token, session) return token, session, nil }