mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2026-07-17 15:31:13 +00:00
refactor: export cache package (#1017)
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
|||||||
"github.com/tinyauthapp/tinyauth/internal/repository"
|
"github.com/tinyauthapp/tinyauth/internal/repository"
|
||||||
"github.com/tinyauthapp/tinyauth/internal/utils"
|
"github.com/tinyauthapp/tinyauth/internal/utils"
|
||||||
"github.com/tinyauthapp/tinyauth/internal/utils/logger"
|
"github.com/tinyauthapp/tinyauth/internal/utils/logger"
|
||||||
|
"github.com/tinyauthapp/tinyauth/pkg/cache"
|
||||||
"go.uber.org/dig"
|
"go.uber.org/dig"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@@ -71,9 +72,9 @@ type AuthService struct {
|
|||||||
dummyHash string
|
dummyHash string
|
||||||
|
|
||||||
caches struct {
|
caches struct {
|
||||||
login *CacheStore[LoginAttempt]
|
login *cache.CacheStore[LoginAttempt]
|
||||||
oauth *CacheStore[OAuthPendingSession]
|
oauth *cache.CacheStore[OAuthPendingSession]
|
||||||
ldap *CacheStore[[]string]
|
ldap *cache.CacheStore[[]string]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,9 +116,9 @@ func NewAuthService(i AuthServiceInput) (*AuthService, error) {
|
|||||||
service.dummyHash = string(dummyHash)
|
service.dummyHash = string(dummyHash)
|
||||||
|
|
||||||
// caches setup
|
// caches setup
|
||||||
oauthCache := NewCacheStore[OAuthPendingSession](256)
|
oauthCache := cache.NewCacheStore[OAuthPendingSession](256)
|
||||||
loginCache := NewCacheStore[LoginAttempt](service.calculateLockdownLimit())
|
loginCache := cache.NewCacheStore[LoginAttempt](service.calculateLockdownLimit())
|
||||||
ldapCache := NewCacheStore[[]string](1024)
|
ldapCache := cache.NewCacheStore[[]string](1024)
|
||||||
|
|
||||||
service.caches.oauth = oauthCache
|
service.caches.oauth = oauthCache
|
||||||
service.caches.login = loginCache
|
service.caches.login = loginCache
|
||||||
@@ -279,7 +280,7 @@ func (auth *AuthService) RecordLoginAttempt(identifier string, success bool) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
auth.caches.login.WithLock(func(actions CacheStoreActions[LoginAttempt]) {
|
auth.caches.login.WithLock(func(actions cache.CacheStoreActions[LoginAttempt]) {
|
||||||
entry, ok := actions.Get(identifier)
|
entry, ok := actions.Get(identifier)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import (
|
|||||||
"github.com/tinyauthapp/tinyauth/internal/repository"
|
"github.com/tinyauthapp/tinyauth/internal/repository"
|
||||||
"github.com/tinyauthapp/tinyauth/internal/utils"
|
"github.com/tinyauthapp/tinyauth/internal/utils"
|
||||||
"github.com/tinyauthapp/tinyauth/internal/utils/logger"
|
"github.com/tinyauthapp/tinyauth/internal/utils/logger"
|
||||||
|
"github.com/tinyauthapp/tinyauth/pkg/cache"
|
||||||
"go.uber.org/dig"
|
"go.uber.org/dig"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -158,9 +159,9 @@ type OIDCService struct {
|
|||||||
issuer string
|
issuer string
|
||||||
|
|
||||||
caches struct {
|
caches struct {
|
||||||
code *CacheStore[AuthorizeCodeEntry]
|
code *cache.CacheStore[AuthorizeCodeEntry]
|
||||||
usedCode *CacheStore[UsedCodeEntry]
|
usedCode *cache.CacheStore[UsedCodeEntry]
|
||||||
authorize *CacheStore[AuthorizeRequest]
|
authorize *cache.CacheStore[AuthorizeRequest]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -339,11 +340,11 @@ func NewOIDCService(i OIDCServiceInput) (*OIDCService, error) {
|
|||||||
i.Ding.Go(service.cleanupRoutine, ding.RingMinor)
|
i.Ding.Go(service.cleanupRoutine, ding.RingMinor)
|
||||||
|
|
||||||
// Create caches
|
// Create caches
|
||||||
codeCash := NewCacheStore[AuthorizeCodeEntry](256)
|
codeCache := cache.NewCacheStore[AuthorizeCodeEntry](256)
|
||||||
usedCode := NewCacheStore[UsedCodeEntry](256)
|
usedCode := cache.NewCacheStore[UsedCodeEntry](256)
|
||||||
authorize := NewCacheStore[AuthorizeRequest](256)
|
authorize := cache.NewCacheStore[AuthorizeRequest](256)
|
||||||
|
|
||||||
service.caches.code = codeCash
|
service.caches.code = codeCache
|
||||||
service.caches.usedCode = usedCode
|
service.caches.usedCode = usedCode
|
||||||
service.caches.authorize = authorize
|
service.caches.authorize = authorize
|
||||||
|
|
||||||
@@ -503,7 +504,7 @@ func (service *OIDCService) GetCodeEntry(codeHash string, clientId string) (*Aut
|
|||||||
var entry AuthorizeCodeEntry
|
var entry AuthorizeCodeEntry
|
||||||
var ok bool
|
var ok bool
|
||||||
|
|
||||||
service.caches.code.WithLock(func(actions CacheStoreActions[AuthorizeCodeEntry]) {
|
service.caches.code.WithLock(func(actions cache.CacheStoreActions[AuthorizeCodeEntry]) {
|
||||||
entry, ok = actions.Get(codeHash)
|
entry, ok = actions.Get(codeHash)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/tinyauthapp/tinyauth/internal/model"
|
"github.com/tinyauthapp/tinyauth/internal/model"
|
||||||
"github.com/tinyauthapp/tinyauth/internal/utils"
|
"github.com/tinyauthapp/tinyauth/internal/utils"
|
||||||
"github.com/tinyauthapp/tinyauth/internal/utils/logger"
|
"github.com/tinyauthapp/tinyauth/internal/utils/logger"
|
||||||
|
"github.com/tinyauthapp/tinyauth/pkg/cache"
|
||||||
"go.uber.org/dig"
|
"go.uber.org/dig"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -59,8 +60,8 @@ type TailscaleService struct {
|
|||||||
apiToken string
|
apiToken string
|
||||||
|
|
||||||
caches struct {
|
caches struct {
|
||||||
devices *CacheStore[tailscaleAPIDevices]
|
devices *cache.CacheStore[tailscaleAPIDevices]
|
||||||
users *CacheStore[tailscaleAPIUsers]
|
users *cache.CacheStore[tailscaleAPIUsers]
|
||||||
}
|
}
|
||||||
|
|
||||||
urls struct {
|
urls struct {
|
||||||
@@ -100,8 +101,8 @@ func NewTailscaleService(i TailscaleServiceInput) (*TailscaleService, error) {
|
|||||||
apiToken: apiToken,
|
apiToken: apiToken,
|
||||||
}
|
}
|
||||||
|
|
||||||
devicesCache := NewCacheStore[tailscaleAPIDevices](0)
|
devicesCache := cache.NewCacheStore[tailscaleAPIDevices](0)
|
||||||
usersCache := NewCacheStore[tailscaleAPIUsers](0)
|
usersCache := cache.NewCacheStore[tailscaleAPIUsers](0)
|
||||||
|
|
||||||
s.caches.devices = devicesCache
|
s.caches.devices = devicesCache
|
||||||
s.caches.users = usersCache
|
s.caches.users = usersCache
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package service
|
package cache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"slices"
|
"slices"
|
||||||
@@ -33,8 +33,8 @@ func NewCacheStore[T any](maxSize int) *CacheStore[T] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// With lock allows performing multiple operations on the cache store atomically.
|
// WithLock allows performing multiple operations on a single lock.
|
||||||
// The provided mutate function receives a set of actions (Set, Get, Delete) that
|
// The provided mutate function receives a set of actions (Set, Get, Delete, Update) that
|
||||||
// can be used to manipulate the cache store within the locked context.
|
// can be used to manipulate the cache store within the locked context.
|
||||||
func (cs *CacheStore[T]) WithLock(mutate func(actions CacheStoreActions[T])) {
|
func (cs *CacheStore[T]) WithLock(mutate func(actions CacheStoreActions[T])) {
|
||||||
cs.mu.Lock()
|
cs.mu.Lock()
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package service
|
package cache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
Reference in New Issue
Block a user