2021-07-21 23:04:00 +00:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-10-05 06:54:36 +00:00
|
|
|
"path"
|
2021-07-21 23:04:00 +00:00
|
|
|
"sync"
|
2021-10-13 21:53:31 +00:00
|
|
|
"time"
|
2021-07-21 23:04:00 +00:00
|
|
|
|
2021-09-29 20:35:39 +00:00
|
|
|
"source.toby3d.me/website/oauth/internal/domain"
|
2021-09-20 15:45:54 +00:00
|
|
|
"source.toby3d.me/website/oauth/internal/token"
|
2021-07-21 23:04:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type memoryTokenRepository struct {
|
2021-10-13 21:53:31 +00:00
|
|
|
store *sync.Map
|
2021-07-21 23:04:00 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 21:53:31 +00:00
|
|
|
const DefaultPathPrefix string = "tokens"
|
2021-10-05 06:54:36 +00:00
|
|
|
|
2021-10-13 21:53:31 +00:00
|
|
|
func NewMemoryTokenRepository(store *sync.Map) token.Repository {
|
2021-07-21 23:04:00 +00:00
|
|
|
return &memoryTokenRepository{
|
2021-10-13 21:53:31 +00:00
|
|
|
store: store,
|
2021-07-21 23:04:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-29 20:35:39 +00:00
|
|
|
func (repo *memoryTokenRepository) Create(ctx context.Context, accessToken *domain.Token) error {
|
2021-10-13 21:53:31 +00:00
|
|
|
key := path.Join(DefaultPathPrefix, accessToken.AccessToken)
|
2021-09-20 15:45:54 +00:00
|
|
|
|
2021-10-13 21:53:31 +00:00
|
|
|
if _, ok := repo.store.Load(key); ok {
|
2021-09-22 21:32:40 +00:00
|
|
|
return token.ErrExist
|
2021-09-20 15:45:54 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 21:53:31 +00:00
|
|
|
repo.store.Store(key, accessToken.Expiry)
|
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) {
|
|
|
|
expiry, ok := repo.store.Load(path.Join(DefaultPathPrefix, accessToken))
|
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2021-09-20 15:45:54 +00:00
|
|
|
|
2021-10-13 21:53:31 +00:00
|
|
|
return &domain.Token{
|
2021-10-13 22:00:28 +00:00
|
|
|
Expiry: expiry.(time.Time),
|
|
|
|
Scopes: []string{},
|
2021-10-13 21:53:31 +00:00
|
|
|
AccessToken: accessToken,
|
|
|
|
TokenType: "Bearer",
|
2021-10-13 22:00:28 +00:00
|
|
|
ClientID: "",
|
|
|
|
Me: "",
|
2021-10-13 21:53:31 +00:00
|
|
|
}, nil
|
2021-07-21 23:04:00 +00:00
|
|
|
}
|