Documentation ¶
Overview ¶
Package x509 provides authentication strategy, to authenticate HTTPS requests and builds, extracts user informations from client certificates.
Example ¶
package main import ( "crypto/tls" crypto_x509 "crypto/x509" "encoding/pem" "fmt" "net/http" "github.com/shaj13/go-guardian/v2/auth/strategies/x509" ) const ( valid = ` -----BEGIN CERTIFICATE----- MIIEBDCCAuygAwIBAgIUDJgNc70lDeFv6f7B2Mwy9WEcK9QwDQYJKoZIhvcNAQEL BQAwbTELMAkGA1UEBhMCR0IxEDAOBgNVBAgTB0VuZ2xhbmQxDzANBgNVBAcTBkxv bmRvbjENMAsGA1UEChMEVEVTVDEVMBMGA1UECxMMVEVTVCBSb290IENBMRUwEwYD VQQDEwxURVNUIFJvb3QgQ0EwHhcNMjAwNTEzMTI0MjAwWhcNMjEwNTEzMTI0MjAw WjBsMQswCQYDVQQGEwJHQjEQMA4GA1UECBMHRW5nbGFuZDEPMA0GA1UEBxMGTG9u ZG9uMQ0wCwYDVQQKEwRUZXN0MRMwEQYDVQQLEwpUZXN0IEhvc3RzMRYwFAYDVQQD Ew1ob3N0LnRlc3QuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA oDiZOcVxTxOFZIwIAAjHsyXqTz3Hht5iySMyDbApw6C6aHHWVBur1uUnhTh8ek7f 8DbLZ3FiNqMhzrHhs4xT46xE23FNphZoahLaMINXOQ2jkdPU/RkEmcAMdfYfFKZd 85BXVgMjjoNOVsu/HpecO0X4ONLs1pCVpz3U1gBtAjSB9s7DOo2brsGCYbVxM+O9 a8nf1ayWcDJ5iwF5CYgQiPEsSs/pidpiPAMYBMw10JxO//gXk/j0hMz2HL5y7KMA DdLB3GoG7yRy1RNhhmS+chu9jvcKLhF1fIFHq2LVQGJ42DVhB9jk7jnZM82XjIFe qU6HhDo6FRiHDS+nuGUz5wIDAQABo4GcMIGZMA4GA1UdDwEB/wQEAwIFoDATBgNV HSUEDDAKBggrBgEFBQcDAjAMBgNVHRMBAf8EAjAAMB0GA1U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lKXhxGJnln//ummxEo+mGEoAV99Te0eN2TytAcgfJEGYxEqlR r7uiIYhWM3RzrY4wBqukqnaY -----END CERTIFICATE----- ` ) func main() { opts := CreateVerifyOptions() // create strategy and authenticator strategy := x509.New(opts) // user request req, _ := http.NewRequest("GET", "/", nil) req.TLS = &tls.ConnectionState{PeerCertificates: []*crypto_x509.Certificate{ParseCertificate(valid)}} // validate request info, err := strategy.Authenticate(req.Context(), req) fmt.Println(info.GetUserName(), err) // validate expired client certificate req.TLS = &tls.ConnectionState{PeerCertificates: []*crypto_x509.Certificate{ParseCertificate(expired)}} info, err = strategy.Authenticate(req.Context(), req) fmt.Println(info, err) } func ParseCertificate(str string) *crypto_x509.Certificate { p, _ := pem.Decode([]byte(str)) if p == nil { fmt.Println("Failed to decode certificate") return nil } cert, err := crypto_x509.ParseCertificate(p.Bytes) if err != nil { fmt.Println(err) } return cert } func CreateVerifyOptions() crypto_x509.VerifyOptions { opts := crypto_x509.VerifyOptions{} opts.KeyUsages = []crypto_x509.ExtKeyUsage{crypto_x509.ExtKeyUsageClientAuth} opts.Roots = crypto_x509.NewCertPool() opts.Roots.AddCert(ParseCertificate(ca)) return opts }
Output: host.test.com <nil> <nil> x509: certificate has expired or is not yet valid
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrInvalidRequest is returned by x509 strategy when a non TLS request received. ErrInvalidRequest = errors.New("strategy/x509: Invalid request, missing TLS parameters") // ErrMissingCN is returned by DefaultBuilder when Certificate CommonName missing. ErrMissingCN = errors.New("strategies/x509: Certificate subject CN missing") )
Functions ¶
func SetAllowEmptyCN ¶ added in v2.5.2
SetAllowEmptyCN prevent strategy from return ErrMissingCN when client certificate subject CN missing or empty.
func SetAllowedCN ¶ added in v2.5.2
SetAllowedCN sets the common names which a verified certificate is allowed to have.
Example ¶
package main import ( "crypto/tls" crypto_x509 "crypto/x509" "encoding/pem" "fmt" "net/http" "github.com/shaj13/go-guardian/v2/auth/strategies/x509" ) const (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DIn75QWCiX8MthY/SKLm 8mh945zxYxeFiy2SWw+Na1p5pfzmnGAE95eNvjaGe/PrqKPN+YwdL7cmk9jwwnbV /W+4WNUAGNK4xBpv2eODNH8CAwEAAaNmMGQwDgYDVR0PAQH/BAQDAgEGMBIGA1Ud EwEB/wQIMAYBAf8CAQIwHQYDVR0OBBYEFC8887mDh0b4nmMUa/GBmeu9byqHMB8G A1UdIwQYMBaAFC8887mDh0b4nmMUa/GBmeu9byqHMA0GCSqGSIb3DQEBCwUAA4IB AQBbvQFGcOPJzri2Q1Z+d9aEiBKSd2nmKaS7okAmbKFLKa8IsQMD/Tj8HVNvFTUf rG64Fzy4SKf9QoRxG34kX9uysY+3Eo/VkoMz2brO4041GjqFcvHjCcBOMESP/rjD wpi/qy+PU5YO1MQ8u5/M7sFlu46/ExmhevMI6j6DZzbJoDoF08Tr3ZKoP4wt0FB1 jLdiNKZM6byw3dHlNDZWxC78ztzNVcei4tl1iaK86PEQw5sK/tmzn5fMoxDGUlrY CfbpqGLiZ3CVvgBlKXhxGJnln//ummxEo+mGEoAV99Te0eN2TytAcgfJEGYxEqlR r7uiIYhWM3RzrY4wBqukqnaY -----END CERTIFICATE----- ` ) func main() { opts := CreateVerifyOptions() cn := x509.SetAllowedCN("1st.cn", "2nd.cn") // create strategy and authenticator strategy := x509.New(opts, cn) // user request req, _ := http.NewRequest("GET", "/", nil) req.TLS = &tls.ConnectionState{PeerCertificates: []*crypto_x509.Certificate{ParseCertificate(valid)}} // validate request _, err := strategy.Authenticate(req.Context(), req) fmt.Println(err) } func ParseCertificate(str string) *crypto_x509.Certificate { p, _ := pem.Decode([]byte(str)) if p == nil { fmt.Println("Failed to decode certificate") return nil } cert, err := crypto_x509.ParseCertificate(p.Bytes) if err != nil { fmt.Println(err) } return cert } func CreateVerifyOptions() crypto_x509.VerifyOptions { opts := crypto_x509.VerifyOptions{} opts.KeyUsages = []crypto_x509.ExtKeyUsage{crypto_x509.ExtKeyUsageClientAuth} opts.Roots = crypto_x509.NewCertPool() opts.Roots.AddCert(ParseCertificate(ca)) return opts }
Output: strategies/x509: Certificate subject host.test.com CN is not allowed
func SetAllowedCNRegex ¶ added in v2.5.2
SetAllowedCNRegex sets the common names regex which a verified certificate is allowed to have.
Example ¶
package main import ( "crypto/tls" crypto_x509 "crypto/x509" "encoding/pem" "fmt" "net/http" "github.com/shaj13/go-guardian/v2/auth/strategies/x509" ) const ( valid = ` -----BEGIN CERTIFICATE----- MIIEBDCCAuygAwIBAgIUDJgNc70lDeFv6f7B2Mwy9WEcK9QwDQYJKoZIhvcNAQEL BQAwbTELMAkGA1UEBhMCR0IxEDAOBgNVBAgTB0VuZ2xhbmQxDzANBgNVBAcTBkxv bmRvbjENMAsGA1UEChMEVEVTVDEVMBMGA1UECxMMVEVTVCBSb290IENBMRUwEwYD VQQDEwxURVNUIFJvb3QgQ0EwHhcNMjAwNTEzMTI0MjAwWhcNMjEwNTEzMTI0MjAw WjBsMQswCQYDVQQGEwJHQjEQMA4GA1UECBMHRW5nbGFuZDEPMA0GA1UEBxMGTG9u ZG9uMQ0wCwYDVQQKEwRUZXN0MRMwEQYDVQQLEwpUZXN0IEhvc3RzMRYwFAYDVQQD Ew1ob3N0LnRlc3QuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA oDiZOcVxTxOFZIwIAAjHsyXqTz3Hht5iySMyDbApw6C6aHHWVBur1uUnhTh8ek7f 8DbLZ3FiNqMhzrHhs4xT46xE23FNphZoahLaMINXOQ2jkdPU/RkEmcAMdfYfFKZd 85BXVgMjjoNOVsu/HpecO0X4ONLs1pCVpz3U1gBtAjSB9s7DOo2brsGCYbVxM+O9 a8nf1ayWcDJ5iwF5CYgQiPEsSs/pidpiPAMYBMw10JxO//gXk/j0hMz2HL5y7KMA DdLB3GoG7yRy1RNhhmS+chu9jvcKLhF1fIFHq2LVQGJ42DVhB9jk7jnZM82XjIFe qU6HhDo6FRiHDS+nuGUz5wIDAQABo4GcMIGZMA4G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+3HS1LAmnKoiiqddSrJFkYQx62M/HMxsye7fw8u4PpfLfXUetbaxtB CGZTjCI49OrF2W4W5OTBaeorumcwcsYRMjs4/O92LvsOScKzneAGHbAEqpISXiZE exZg2+jcWQdtXWxvafJ9wwvhB4jnd8SgioIlQB+oH+Z8eBDrnWXgUCqzceRMmW+/ ztjX85bfdvqhVIYUKt2+G5oyZ4LNMjZv95nZqIEbpoNpDIn75QWCiX8MthY/SKLm 8mh945zxYxeFiy2SWw+Na1p5pfzmnGAE95eNvjaGe/PrqKPN+YwdL7cmk9jwwnbV /W+4WNUAGNK4xBpv2eODNH8CAwEAAaNmMGQwDgYDVR0PAQH/BAQDAgEGMBIGA1Ud EwEB/wQIMAYBAf8CAQIwHQYDVR0OBBYEFC8887mDh0b4nmMUa/GBmeu9byqHMB8G A1UdIwQYMBaAFC8887mDh0b4nmMUa/GBmeu9byqHMA0GCSqGSIb3DQEBCwUAA4IB AQBbvQFGcOPJzri2Q1Z+d9aEiBKSd2nmKaS7okAmbKFLKa8IsQMD/Tj8HVNvFTUf rG64Fzy4SKf9QoRxG34kX9uysY+3Eo/VkoMz2brO4041GjqFcvHjCcBOMESP/rjD wpi/qy+PU5YO1MQ8u5/M7sFlu46/ExmhevMI6j6DZzbJoDoF08Tr3ZKoP4wt0FB1 jLdiNKZM6byw3dHlNDZWxC78ztzNVcei4tl1iaK86PEQw5sK/tmzn5fMoxDGUlrY CfbpqGLiZ3CVvgBlKXhxGJnln//ummxEo+mGEoAV99Te0eN2TytAcgfJEGYxEqlR r7uiIYhWM3RzrY4wBqukqnaY -----END CERTIFICATE----- ` ) func main() { opts := CreateVerifyOptions() cn := x509.SetAllowedCNRegex("(host.*.org)") // create strategy and authenticator strategy := x509.New(opts, cn) // user request req, _ := http.NewRequest("GET", "/", nil) req.TLS = &tls.ConnectionState{PeerCertificates: []*crypto_x509.Certificate{ParseCertificate(valid)}} // validate request _, err := strategy.Authenticate(req.Context(), req) fmt.Println(err) } func ParseCertificate(str string) *crypto_x509.Certificate { p, _ := pem.Decode([]byte(str)) if p == nil { fmt.Println("Failed to decode certificate") return nil } cert, err := crypto_x509.ParseCertificate(p.Bytes) if err != nil { fmt.Println(err) } return cert } func CreateVerifyOptions() crypto_x509.VerifyOptions { opts := crypto_x509.VerifyOptions{} opts.KeyUsages = []crypto_x509.ExtKeyUsage{crypto_x509.ExtKeyUsageClientAuth} opts.Roots = crypto_x509.NewCertPool() opts.Roots.AddCert(ParseCertificate(ca)) return opts }
Output: strategies/x509: Certificate subject host.test.com CN is not allowed
func SetInfoBuilder ¶
func SetInfoBuilder(ib InfoBuilder) auth.Option
SetInfoBuilder sets x509 info builder.
Types ¶
type InfoBuilder ¶
type InfoBuilder func(chain [][]*x509.Certificate) (auth.Info, error)
InfoBuilder declare a function signature for building Info from certificate chain.
Example ¶
package main import ( "crypto/tls" crypto_x509 "crypto/x509" "encoding/pem" "fmt" "net/http" "github.com/shaj13/go-guardian/v2/auth" "github.com/shaj13/go-guardian/v2/auth/strategies/x509" ) const ( valid = ` -----BEGIN CERTIFICATE----- MIIEBDCCAuygAwIBAgIUDJgNc70lDeFv6f7B2Mwy9WEcK9QwDQYJKoZIhvcNAQEL BQAwbTELMAkGA1UEBhMCR0IxEDAOBgNVBAgTB0VuZ2xhbmQxDzANBgNVBAcTBkxv bmRvbjENMAsGA1UEChMEVEVTVDEVMBMGA1UECxMMVEVTVCBSb290IENBMRUwEwYD VQQDEwxURVNUIFJvb3QgQ0EwHhcNMjAwNTEzMTI0MjAwWhcNMjEwNTEzMTI0MjAw WjBsMQswCQYDVQQGEwJHQjEQMA4GA1UECBMHRW5nbGFuZDEPMA0GA1UEBxMGTG9u ZG9uMQ0wCwYDVQQKEwRUZXN0MRMwEQYDVQQLEwpUZXN0IEhvc3RzMRYwFAYDVQQD Ew1ob3N0LnRlc3QuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA oDiZOcVxTxOFZIwIAAjHsyXqTz3Hht5iySMyDbAp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tmzn5fMoxDGUlrY CfbpqGLiZ3CVvgBlKXhxGJnln//ummxEo+mGEoAV99Te0eN2TytAcgfJEGYxEqlR r7uiIYhWM3RzrY4wBqukqnaY -----END CERTIFICATE----- ` ) func main() { opts := CreateVerifyOptions() builder := x509.SetInfoBuilder(func(chain [][]*crypto_x509.Certificate) (auth.Info, error) { return auth.NewDefaultUser("user-info-builder", "10", nil, nil), nil }) // create strategy and authenticator strategy := x509.New(opts, builder) // user request req, _ := http.NewRequest("GET", "/", nil) req.TLS = &tls.ConnectionState{PeerCertificates: []*crypto_x509.Certificate{ParseCertificate(valid)}} // validate request info, err := strategy.Authenticate(req.Context(), req) fmt.Println(info.GetUserName(), err) } func ParseCertificate(str string) *crypto_x509.Certificate { p, _ := pem.Decode([]byte(str)) if p == nil { fmt.Println("Failed to decode certificate") return nil } cert, err := crypto_x509.ParseCertificate(p.Bytes) if err != nil { fmt.Println(err) } return cert } func CreateVerifyOptions() crypto_x509.VerifyOptions { opts := crypto_x509.VerifyOptions{} opts.KeyUsages = []crypto_x509.ExtKeyUsage{crypto_x509.ExtKeyUsageClientAuth} opts.Roots = crypto_x509.NewCertPool() opts.Roots.AddCert(ParseCertificate(ca)) return opts }
Output: user-info-builder <nil>
Click to show internal directories.
Click to hide internal directories.