You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

60 lines
1.0 KiB

package server
import (
"errors"
"time"
)
type Token struct {
UserID int64
Token string
Expires *time.Time
}
type TokenDB interface {
FindByUserID(id int64) (*Token, error)
FindByToken(token string) (*Token, error)
Save(token *Token) error
}
type inMemoryTokenDB struct {
tokens []*Token
}
func newInMemoryTokenDB() *inMemoryTokenDB {
return &inMemoryTokenDB{
tokens: make([]*Token, 0),
}
}
func (m *inMemoryTokenDB) FindByUserID(id int64) (*Token, error) {
for _, t := range m.tokens {
if t.UserID == id {
return t, nil
}
}
return nil, errors.New("Token not found.")
}
func (m *inMemoryTokenDB) FindByToken(token string) (*Token, error) {
for _, t := range m.tokens {
if t.Token == token {
return t, nil
}
}
return nil, errors.New("Token not found.")
}
func (m *inMemoryTokenDB) Save(token *Token) error {
for i, t := range m.tokens {
if t.UserID == token.UserID || t.Token == token.Token {
m.tokens[i] = token
return nil
}
}
m.tokens = append(m.tokens, token)
return nil
}