go-saml是Go语言的SAML开发包。
示例代码:
package maiimport ( "crypto/rsa" "crypto/tls" "crypto/x509" "fmt" "et/http" "et/url" "github.com/crewjam/saml/samlsp")fuc hello(w http.ResposeWriter, r *http.Request) { fmt.Fpritf(w, "Hello, %s!", samlsp.Toke(r.Cotext()).Attributes.Get("c"))}fuc mai() { keyPair, err := tls.LoadX509KeyPair("myservice.cert", "myservice.key") if err != il { paic(err) // TODO hadle error } keyPair.Leaf, err = x509.ParseCertificate(keyPair.Certificate[0]) if err != il { paic(err) // TODO hadle error } idpMetadataURL, err := url.Parse("https://www.testshib.org/metadata/testshib-providers.xml") if err != il { paic(err) // TODO hadle error } rootURL, err := url.Parse("https://localhost:8000") if err != il { paic(err) // TODO hadle error } samlSP, _ := samlsp.New(samlsp.Optios{ URL: *rootURL, Key: keyPair.PrivateKey.(*rsa.PrivateKey), Certificate: keyPair.Leaf, IDPMetadataURL: idpMetadataURL, }) app := http.HadlerFuc(hello) http.Hadle("/hello", samlSP.RequireAccout(app)) http.Hadle("/saml/", samlSP) http.ListeAdServe(":8000", il)}
评论