-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor token startegy and consolidate static and cache logic…
… overlaps under single core
- Loading branch information
Showing
7 changed files
with
208 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package token | ||
|
||
import ( | ||
"crypto" | ||
|
||
"github.com/shaj13/go-guardian/v2/auth" | ||
"github.com/shaj13/go-guardian/v2/auth/internal" | ||
) | ||
|
||
// SetType sets the authentication token type or scheme, | ||
// used for HTTP WWW-Authenticate header. | ||
// | ||
// Deprecated: No longer used. | ||
func SetType(t Type) auth.Option { | ||
return auth.OptionFunc(func(v interface{}) { | ||
}) | ||
} | ||
|
||
// SetParser sets the strategy token parser. | ||
func SetParser(p Parser) auth.Option { | ||
return auth.OptionFunc(func(v interface{}) { | ||
if v, ok := v.(*core); ok { | ||
v.parser = p | ||
} | ||
}) | ||
} | ||
|
||
// SetScopes sets the scopes to be used when verifying user access token. | ||
func SetScopes(scopes ...Scope) auth.Option { | ||
return auth.OptionFunc(func(v interface{}) { | ||
if v, ok := v.(*core); ok { | ||
v.verify = verifyScopes(scopes...) | ||
} | ||
}) | ||
} | ||
|
||
// SetHash apply token hashing based on HMAC with h and key, | ||
// To prevent precomputation and length extension attacks, | ||
// and to mitigates hash map DOS attacks via collisions. | ||
func SetHash(h crypto.Hash, key []byte) auth.Option { | ||
return auth.OptionFunc(func(v interface{}) { | ||
if v, ok := v.(*core); ok { | ||
v.hasher = internal.NewHMACHasher(h, key) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package token | ||
|
||
import ( | ||
"crypto" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSetParser(t *testing.T) { | ||
c := new(core) | ||
p := XHeaderParser("") | ||
opt := SetParser(p) | ||
opt.Apply(c) | ||
assert.True(t, c.parser != nil) | ||
} | ||
|
||
func TestSetScopes(t *testing.T) { | ||
c := new(core) | ||
opt := SetScopes(NewScope("admin", "", "")) | ||
opt.Apply(c) | ||
assert.True(t, c.verify != nil) | ||
} | ||
|
||
func TestSetHash(t *testing.T) { | ||
const token = "token" | ||
c := new(core) | ||
opt := SetHash(crypto.SHA256, []byte("key")) | ||
opt.Apply(c) | ||
assert.True(t, c.hasher != nil) | ||
assert.NotEqual(t, token, c.hasher.Hash(token)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.