2021-07-21 23:04:00 +00:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
2021-10-17 22:51:10 +00:00
|
|
|
|
2022-03-13 10:58:34 +00:00
|
|
|
"source.toby3d.me/toby3d/auth/internal/domain"
|
|
|
|
"source.toby3d.me/toby3d/auth/internal/token"
|
2021-07-21 23:04:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type memoryTokenRepository struct {
|
2023-01-14 21:27:37 +00:00
|
|
|
mutex *sync.RWMutex
|
|
|
|
tokens map[string]domain.Token
|
2021-07-21 23:04:00 +00:00
|
|
|
}
|
|
|
|
|
2023-01-14 21:27:37 +00:00
|
|
|
func NewMemoryTokenRepository() token.Repository {
|
2021-07-21 23:04:00 +00:00
|
|
|
return &memoryTokenRepository{
|
2023-01-14 21:27:37 +00:00
|
|
|
mutex: new(sync.RWMutex),
|
|
|
|
tokens: make(map[string]domain.Token),
|
2021-07-21 23:04:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-14 21:27:37 +00:00
|
|
|
func (repo *memoryTokenRepository) Create(ctx context.Context, accessToken domain.Token) error {
|
|
|
|
repo.mutex.Lock()
|
|
|
|
defer repo.mutex.Unlock()
|
2021-09-20 15:45:54 +00:00
|
|
|
|
2023-01-14 21:27:37 +00:00
|
|
|
repo.tokens[accessToken.AccessToken] = accessToken
|
2021-09-20 15:45:54 +00:00
|
|
|
|
2021-09-22 21:32:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-07-21 23:04:00 +00:00
|
|
|
|
2021-10-13 21:53:31 +00:00
|
|
|
func (repo *memoryTokenRepository) Get(ctx context.Context, accessToken string) (*domain.Token, error) {
|
2023-01-14 21:27:37 +00:00
|
|
|
repo.mutex.RLock()
|
|
|
|
defer repo.mutex.RUnlock()
|
2021-10-17 22:51:10 +00:00
|
|
|
|
2023-01-14 21:27:37 +00:00
|
|
|
if t, ok := repo.tokens[accessToken]; ok {
|
|
|
|
return &t, nil
|
2021-10-13 21:53:31 +00:00
|
|
|
}
|
2021-09-20 15:45:54 +00:00
|
|
|
|
2023-01-14 21:27:37 +00:00
|
|
|
return nil, token.ErrNotExist
|
2021-07-21 23:04:00 +00:00
|
|
|
}
|