-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve x509 package and add more options
- Loading branch information
Showing
6 changed files
with
345 additions
and
68 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,55 @@ | ||
package x509 | ||
|
||
import ( | ||
"regexp" | ||
|
||
"github.com/shaj13/go-guardian/v2/auth" | ||
) | ||
|
||
// SetInfoBuilder sets x509 info builder. | ||
func SetInfoBuilder(ib InfoBuilder) auth.Option { | ||
return auth.OptionFunc(func(v interface{}) { | ||
if s, ok := v.(*strategy); ok { | ||
s.builder = ib | ||
} | ||
}) | ||
} | ||
|
||
// SetAllowEmptyCN prevent strategy from return ErrMissingCN | ||
// when client certificate subject CN missing or empty. | ||
func SetAllowEmptyCN() auth.Option { | ||
return auth.OptionFunc(func(v interface{}) { | ||
if s, ok := v.(*strategy); ok { | ||
s.emptyCN = true | ||
} | ||
}) | ||
} | ||
|
||
// SetAllowedCN sets the common names which a verified certificate is allowed to have. | ||
func SetAllowedCN(cns ...string) auth.Option { | ||
allowedCNS := map[string]struct{}{} | ||
for _, cn := range cns { | ||
allowedCNS[cn] = struct{}{} | ||
} | ||
|
||
return auth.OptionFunc(func(v interface{}) { | ||
if s, ok := v.(*strategy); ok { | ||
s.allowedCN = func(cn string) bool { | ||
_, ok := allowedCNS[cn] | ||
return ok | ||
} | ||
} | ||
}) | ||
} | ||
|
||
// SetAllowedCNRegex sets the common names regex which a verified certificate is allowed to have. | ||
func SetAllowedCNRegex(str string) auth.Option { | ||
regex := regexp.MustCompile(str) | ||
return auth.OptionFunc(func(v interface{}) { | ||
if s, ok := v.(*strategy); ok { | ||
s.allowedCN = func(cn string) bool { | ||
return regex.MatchString(cn) | ||
} | ||
} | ||
}) | ||
} |
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 x509 | ||
|
||
import ( | ||
"crypto/x509" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSetAllowEmptyCN(t *testing.T) { | ||
opt := SetAllowEmptyCN() | ||
s := New(x509.VerifyOptions{}, opt) | ||
assert.True(t, s.(*strategy).emptyCN) | ||
} | ||
|
||
func TestSetAllowedCN(t *testing.T) { | ||
cns := []string{"a", "b", "c"} | ||
opt := SetAllowedCN(cns...) | ||
s := New(x509.VerifyOptions{}, opt) | ||
for _, cn := range cns { | ||
assert.True(t, s.(*strategy).allowedCN(cn)) | ||
} | ||
} | ||
|
||
func TestSetAllowedCNRegex(t *testing.T) { | ||
cns := []string{"a", "b", "c"} | ||
opt := SetAllowedCNRegex("a|b|c") | ||
s := New(x509.VerifyOptions{}, opt) | ||
for _, cn := range cns { | ||
assert.True(t, s.(*strategy).allowedCN(cn)) | ||
} | ||
} |
Oops, something went wrong.