Skip to content

Commit

Permalink
chore: remove cache package
Browse files Browse the repository at this point in the history
  • Loading branch information
shaj13 committed Oct 17, 2020
1 parent 716d43f commit 3a9dc08
Show file tree
Hide file tree
Showing 27 changed files with 78 additions and 1,417 deletions.
13 changes: 7 additions & 6 deletions _examples/basic_bearer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import (
"github.com/google/uuid"
"github.com/gorilla/mux"

"github.com/shaj13/libcache"
_ "github.com/shaj13/libcache/fifo"

"github.com/shaj13/go-guardian/v2/auth"
"github.com/shaj13/go-guardian/v2/auth/strategies/basic"
"github.com/shaj13/go-guardian/v2/auth/strategies/token"
"github.com/shaj13/go-guardian/v2/auth/strategies/union"
"github.com/shaj13/go-guardian/v2/cache"
"github.com/shaj13/go-guardian/v2/cache/container/fifo"
)

// Usage:
Expand All @@ -29,7 +30,7 @@ import (

var strategy union.Union
var tokenStrategy auth.Strategy
var cacheObj cache.Cache
var cacheObj libcache.Cache

func main() {
setupGoGuardian()
Expand Down Expand Up @@ -61,11 +62,11 @@ func getBookAuthor(w http.ResponseWriter, r *http.Request) {
}

func setupGoGuardian() {
ttl := fifo.TTL(time.Minute * 5)
exp := fifo.RegisterOnExpired(func(key interface{}) {
cacheObj = libcache.FIFO.New(0)
cacheObj.SetTTL(time.Minute * 5)
cacheObj.RegisterOnExpired(func(key, _ interface{}) {
cacheObj.Peek(key)
})
cacheObj = cache.FIFO.New(ttl, exp)
basicStrategy := basic.NewCached(validateUser, cacheObj)
tokenStrategy = token.New(token.NoOpAuthenticate, cacheObj)
strategy = union.New(tokenStrategy, basicStrategy)
Expand Down
12 changes: 6 additions & 6 deletions _examples/digest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"time"

"github.com/gorilla/mux"
"github.com/shaj13/libcache"
_ "github.com/shaj13/libcache/fifo"

"github.com/shaj13/go-guardian/v2/auth"
"github.com/shaj13/go-guardian/v2/auth/strategies/digest"
"github.com/shaj13/go-guardian/v2/cache"
"github.com/shaj13/go-guardian/v2/cache/container/fifo"
)

// Usage:
Expand All @@ -21,12 +21,12 @@ import (
var strategy *digest.Digest

func init() {
var c cache.Cache
ttl := fifo.TTL(time.Minute * 3)
exp := fifo.RegisterOnExpired(func(key interface{}) {
var c libcache.Cache
c = libcache.FIFO.New(10)
c.SetTTL(time.Minute * 3)
c.RegisterOnExpired(func(key, _ interface{}) {
c.Delete(key)
})
c = cache.FIFO.New(ttl, exp)
strategy = digest.New(validateUser, c)
}

Expand Down
14 changes: 7 additions & 7 deletions _examples/kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ import (
"time"

"github.com/gorilla/mux"
"github.com/shaj13/libcache"
_ "github.com/shaj13/libcache/fifo"

"github.com/shaj13/go-guardian/v2/auth"
"github.com/shaj13/go-guardian/v2/auth/strategies/kubernetes"
"github.com/shaj13/go-guardian/v2/cache"
"github.com/shaj13/go-guardian/v2/cache/container/fifo"
)

// Usage:
// Run kubernetes mock api and get agent token
// go run mock.go
// Request server to verify token and get book author
// <agent-token-from-mock>"
// curl -k http://127.0.0.1:8080/v1/book/1449311601 -H "Authorization: Bearer <agent-token-from-mock>"

var strategy auth.Strategy
var cacheObj cache.Cache
var cacheObj libcache.Cache

func main() {
setupGoGuardian()
Expand All @@ -49,11 +49,11 @@ func getBookAuthor(w http.ResponseWriter, r *http.Request) {
}

func setupGoGuardian() {
ttl := fifo.TTL(time.Minute * 5)
exp := fifo.RegisterOnExpired(func(key interface{}) {
cacheObj = libcache.FIFO.New(0)
cacheObj.SetTTL(time.Minute * 5)
cacheObj.RegisterOnExpired(func(key, _ interface{}) {
cacheObj.Peek(key)
})
cacheObj = cache.FIFO.New(ttl, exp)
strategy = kubernetes.New(cacheObj)
}

Expand Down
12 changes: 6 additions & 6 deletions _examples/ldap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ import (
"time"

"github.com/gorilla/mux"
"github.com/shaj13/libcache"
_ "github.com/shaj13/libcache/fifo"

"github.com/shaj13/go-guardian/v2/auth"
"github.com/shaj13/go-guardian/v2/auth/strategies/ldap"
"github.com/shaj13/go-guardian/v2/cache"
"github.com/shaj13/go-guardian/v2/cache/container/fifo"
)

// Usage:
// curl -k http://127.0.0.1:8080/v1/book/1449311601 -u tesla:password

var strategy auth.Strategy
var cacheObj cache.Cache
var cacheObj libcache.Cache

func main() {
setupGoGuardian()
Expand Down Expand Up @@ -53,11 +53,11 @@ func setupGoGuardian() {
BindPassword: "password",
Filter: "(uid=%s)",
}
ttl := fifo.TTL(time.Minute * 5)
exp := fifo.RegisterOnExpired(func(key interface{}) {
cacheObj = libcache.FIFO.New(0)
cacheObj.SetTTL(time.Minute * 5)
cacheObj.RegisterOnExpired(func(key, _ interface{}) {
cacheObj.Peek(key)
})
cacheObj = cache.FIFO.New(ttl, exp)
strategy = ldap.NewCached(cfg, cacheObj)
}

Expand Down
8 changes: 6 additions & 2 deletions auth/cache.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package auth

import "time"

// Cache type describes the requirements for authentication strategies,
// that cache the authentication decisions.
type Cache interface {
// Load returns key's value.
// Load returns key value.
Load(key interface{}) (interface{}, bool)
// Store sets the value for a key.
// Store sets the key value.
Store(key interface{}, value interface{})
// StoreWithTTL sets the key value with TTL overrides the default.
StoreWithTTL(key interface{}, value interface{}, ttl time.Duration)
// Delete deletes the key value.
Delete(key interface{})
}
44 changes: 9 additions & 35 deletions auth/strategies/basic/cached_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
_ "crypto/sha256"
"fmt"
"net/http"
"sync"
"testing"

"github.com/shaj13/libcache"
_ "github.com/shaj13/libcache/lru"
"github.com/stretchr/testify/assert"

"github.com/shaj13/go-guardian/v2/auth"
Expand Down Expand Up @@ -66,20 +67,20 @@ func TestNewCached(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil)
tt.setCredentials(r)

c := newMockCache()
c.cache["predefined"] = "invalid-type"
c.cache["predefined2"] = auth.NewDefaultUser(
cache := libcache.LRU.New(0)
cache.Store("predefined", "invalid-type")
cache.Store("predefined2", auth.NewDefaultUser(
"predefined2",
"10",
nil,
map[string][]string{
ExtensionKey: {"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"},
},
)
c.cache["predefined3"] = auth.NewDefaultUser("predefined3", "10", nil, nil)
))
cache.Store("predefined3", auth.NewDefaultUser("predefined3", "10", nil, nil))

opt := SetHash(crypto.SHA256)
info, err := NewCached(authFunc, c, opt).Authenticate(r.Context(), r)
info, err := NewCached(authFunc, cache, opt).Authenticate(r.Context(), r)

assert.Equal(t, tt.expectedErr, err != nil, "%s: Got Unexpected error %v", tt.name, err)
assert.Equal(t, !tt.expectedErr, info != nil, "%s: Expected info object, got nil", tt.name)
Expand All @@ -91,7 +92,7 @@ func BenchmarkCachedBasic(b *testing.B) {
r, _ := http.NewRequest("GET", "/", nil)
r.SetBasicAuth("test", "test")

cache := newMockCache()
cache := libcache.LRU.New(1)

strategy := NewCached(exampleAuthFunc, cache)

Expand All @@ -105,30 +106,3 @@ func BenchmarkCachedBasic(b *testing.B) {
}
})
}

type mockCache struct {
cache map[interface{}]interface{}
mu *sync.Mutex
}

func (m mockCache) Load(key interface{}) (interface{}, bool) {
m.mu.Lock()
defer m.mu.Unlock()
v, ok := m.cache[key]
return v, ok
}

func (m mockCache) Store(key interface{}, value interface{}) {
m.mu.Lock()
defer m.mu.Unlock()
m.cache[key] = value
}

func (m mockCache) Delete(interface{}) {}

func newMockCache() mockCache {
return mockCache{
cache: make(map[interface{}]interface{}),
mu: new(sync.Mutex),
}
}
9 changes: 5 additions & 4 deletions auth/strategies/basic/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"fmt"
"net/http"

"github.com/shaj13/libcache"
_ "github.com/shaj13/libcache/lru"

"github.com/shaj13/go-guardian/v2/auth"
"github.com/shaj13/go-guardian/v2/auth/strategies/basic"
"github.com/shaj13/go-guardian/v2/cache"
_ "github.com/shaj13/go-guardian/v2/cache/container/lru"
)

func Example() {
Expand All @@ -33,7 +34,7 @@ func Example() {
func Example_second() {
// This example show how to caches the result of basic auth.
// With LRU cache
cache := cache.LRU.New()
cache := libcache.LRU.New(1)
strategy := basic.NewCached(exampleAuthFunc, cache)

// user request
Expand All @@ -53,7 +54,7 @@ func Example_second() {

func ExampleSetHash() {
opt := basic.SetHash(crypto.SHA256) // import _ crypto/sha256
cache := cache.LRU.New()
cache := libcache.LRU.New(1)
basic.NewCached(exampleAuthFunc, cache, opt)
}

Expand Down
17 changes: 3 additions & 14 deletions auth/strategies/digest/digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"net/http"
"testing"

"github.com/shaj13/libcache"
_ "github.com/shaj13/libcache/lru"
"github.com/stretchr/testify/assert"

"github.com/shaj13/go-guardian/v2/auth"
Expand Down Expand Up @@ -81,20 +83,7 @@ func BenchmarkStrategy(b *testing.B) {
func testDigest(fn FetchUser) auth.Strategy {
opaque := SetOpaque("1")
realm := SetRealm("t")
cache := make(mockCache)
cache := libcache.LRU.New(0)
cache.Store("1", nil)
return New(fn, cache, opaque, realm)
}

type mockCache map[interface{}]interface{}

func (m mockCache) Load(key interface{}) (interface{}, bool) {
v, ok := m[key]
return v, ok
}

func (m mockCache) Store(key, value interface{}) {
m[key] = value
}

func (m mockCache) Delete(key interface{}) {}
11 changes: 6 additions & 5 deletions auth/strategies/kubernetes/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"fmt"
"net/http"

"github.com/shaj13/libcache"
_ "github.com/shaj13/libcache/idle"

"github.com/shaj13/go-guardian/v2/auth/strategies/token"
"github.com/shaj13/go-guardian/v2/cache"
_ "github.com/shaj13/go-guardian/v2/cache/container/idle"
)

func ExampleNew() {
cache := cache.IDLE.NewUnsafe()
cache := libcache.IDLE.NewUnsafe(0)
kube := New(cache)
r, _ := http.NewRequest("", "/", nil)
_, err := kube.Authenticate(r.Context(), r)
Expand All @@ -20,7 +21,7 @@ func ExampleNew() {
}

func ExampleGetAuthenticateFunc() {
cache := cache.IDLE.NewUnsafe()
cache := libcache.IDLE.NewUnsafe(0)
fn := GetAuthenticateFunc()
kube := token.New(fn, cache)
r, _ := http.NewRequest("", "/", nil)
Expand All @@ -32,7 +33,7 @@ func ExampleGetAuthenticateFunc() {

func Example() {
st := SetServiceAccountToken("Service Account Token")
cache := cache.IDLE.NewUnsafe()
cache := libcache.IDLE.NewUnsafe(0)
kube := New(cache, st)
r, _ := http.NewRequest("", "/", nil)
_, err := kube.Authenticate(r.Context(), r)
Expand Down
Loading

0 comments on commit 3a9dc08

Please sign in to comment.