Skip to content

Commit 57ca76a

Browse files
committed
fix: issue 102 and prevent store user pass in ext
1 parent b4ee370 commit 57ca76a

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

auth/strategies/basic/cached.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010

1111
// ExtensionKey represents a key for the password in info extensions.
1212
// Typically used when basic strategy cache the authentication decisions.
13+
//
14+
// Deprecated: No longer used.
1315
const ExtensionKey = "x-go-guardian-basic-password"
1416

1517
// NewCached return new auth.Strategy.
@@ -26,6 +28,11 @@ func NewCached(f AuthenticateFunc, cache auth.Cache, opts ...auth.Option) auth.S
2628
return New(cb.authenticate, opts...)
2729
}
2830

31+
type entry struct {
32+
password string
33+
info auth.Info
34+
}
35+
2936
type cachedBasic struct {
3037
fn AuthenticateFunc
3138
comparator Comparator
@@ -42,18 +49,12 @@ func (c *cachedBasic) authenticate(ctx context.Context, r *http.Request, userNam
4249
return c.authenticatAndHash(ctx, r, hash, userName, pass)
4350
}
4451

45-
if _, ok := v.(auth.Info); !ok {
46-
return nil, auth.NewTypeError("strategies/basic:", (*auth.Info)(nil), v)
47-
}
48-
49-
info := v.(auth.Info)
50-
ext := info.GetExtensions()
51-
52-
if !ext.Has(ExtensionKey) {
53-
return c.authenticatAndHash(ctx, r, hash, userName, pass)
52+
ent, ok := v.(entry)
53+
if !ok {
54+
return nil, auth.NewTypeError("strategies/basic:", entry{}, v)
5455
}
5556

56-
return info, c.comparator.Compare(ext.Get(ExtensionKey), pass)
57+
return ent.info, c.comparator.Compare(ent.password, pass)
5758
}
5859

5960
func (c *cachedBasic) authenticatAndHash(ctx context.Context, r *http.Request, hash string, userName, pass string) (auth.Info, error) { //nolint:lll
@@ -63,8 +64,11 @@ func (c *cachedBasic) authenticatAndHash(ctx context.Context, r *http.Request, h
6364
}
6465

6566
hashedPass, _ := c.comparator.Hash(pass)
66-
info.GetExtensions().Set(ExtensionKey, hashedPass)
67-
c.cache.Store(hash, info)
67+
ent := entry{
68+
password: hashedPass,
69+
info: info,
70+
}
71+
c.cache.Store(hash, ent)
6872

6973
return info, nil
7074
}

auth/strategies/basic/cached_test.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ func TestNewCached(t *testing.T) {
3535
setCredentials: func(r *http.Request) { r.SetBasicAuth("predefined2", "test") },
3636
expectedErr: false,
3737
},
38-
{
39-
name: "it re-authenticate user when hash missing",
40-
setCredentials: func(r *http.Request) { r.SetBasicAuth("predefined3", "test") },
41-
expectedErr: false,
42-
},
4338
{
4439
name: "it return error when cache hold invalid user info",
4540
setCredentials: func(r *http.Request) { r.SetBasicAuth("predefined", "test") },
@@ -69,19 +64,16 @@ func TestNewCached(t *testing.T) {
6964

7065
cache := libcache.LRU.New(0)
7166
cache.Store("predefined", "invalid-type")
72-
cache.Store("predefined2", auth.NewDefaultUser(
67+
cache.Store(
7368
"predefined2",
74-
"10",
75-
nil,
76-
map[string][]string{
77-
ExtensionKey: {"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"},
69+
entry{
70+
password: "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
71+
info: auth.NewDefaultUser("predefined2", "10", nil, nil),
7872
},
79-
))
80-
cache.Store("predefined3", auth.NewDefaultUser("predefined3", "10", nil, nil))
73+
)
8174

8275
opt := SetHash(crypto.SHA256)
8376
info, err := NewCached(authFunc, cache, opt).Authenticate(r.Context(), r)
84-
8577
assert.Equal(t, tt.expectedErr, err != nil, "%s: Got Unexpected error %v", tt.name, err)
8678
assert.Equal(t, !tt.expectedErr, info != nil, "%s: Expected info object, got nil", tt.name)
8779
})

0 commit comments

Comments
 (0)