Skip to content

Commit 9477cf4

Browse files
authored
Merge pull request #96 from umayr/master
Add syntax highlight
2 parents 446329e + 6ab3142 commit 9477cf4

File tree

1 file changed

+52
-51
lines changed

1 file changed

+52
-51
lines changed

README.md

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -32,73 +32,74 @@ The core package contains the implementation of SAML. The package samlsp provide
3232
## Getting Started as a Service Provider
3333

3434
Let us assume we have a simple web appliation to protect. We'll modify this application so it uses SAML to authenticate users.
35+
```golang
36+
package main
3537

36-
package main
38+
import "net/http"
3739

38-
import "net/http"
39-
40-
func hello(w http.ResponseWriter, r *http.Request) {
41-
fmt.Fprintf(w, "Hello, World!")
42-
}
43-
44-
func main() {
45-
app := http.HandlerFunc(hello)
46-
http.Handle("/hello", app)
47-
http.ListenAndServe(":8000", nil)
48-
}
40+
func hello(w http.ResponseWriter, r *http.Request) {
41+
fmt.Fprintf(w, "Hello, World!")
42+
}
4943

44+
func main() {
45+
app := http.HandlerFunc(hello)
46+
http.Handle("/hello", app)
47+
http.ListenAndServe(":8000", nil)
48+
}
49+
```
5050
Each service provider must have an self-signed X.509 key pair established. You can generate your own with something like this:
5151

5252
openssl req -x509 -newkey rsa:2048 -keyout myservice.key -out myservice.cert -days 365 -nodes -subj "/CN=myservice.example.com"
5353

5454
We will use `samlsp.Middleware` to wrap the endpoint we want to protect. Middleware provides both an `http.Handler` to serve the SAML specific URLs **and** a set of wrappers to require the user to be logged in. We also provide the URL where the service provider can fetch the metadata from the IDP at startup. In our case, we'll use [testshib.org](https://www.testshib.org/), an identity provider designed for testing.
5555

56-
package main
56+
```golang
57+
package main
5758

58-
import (
59-
"fmt"
60-
"io/ioutil"
61-
"net/http"
59+
import (
60+
"fmt"
61+
"io/ioutil"
62+
"net/http"
6263

63-
"github.com/crewjam/saml/samlsp"
64-
)
64+
"github.com/crewjam/saml/samlsp"
65+
)
66+
67+
func hello(w http.ResponseWriter, r *http.Request) {
68+
fmt.Fprintf(w, "Hello, %s!", r.Header.Get("X-Saml-Cn"))
69+
}
70+
71+
func main() {
72+
keyPair, err := tls.LoadX509KeyPair("myservice.cert", "myservice.key")
73+
if err != nil {
74+
panic(err) // TODO handle error
75+
}
76+
keyPair.Leaf, err = x509.ParseCertificate(keyPair.Certificate[0])
77+
if err != nil {
78+
panic(err) // TODO handle error
79+
}
6580

66-
func hello(w http.ResponseWriter, r *http.Request) {
67-
fmt.Fprintf(w, "Hello, %s!", r.Header.Get("X-Saml-Cn"))
81+
idpMetadataURL, err := url.Parse("https://www.testshib.org/metadata/testshib-providers.xml")
82+
if err != nil {
83+
panic(err) // TODO handle error
6884
}
6985

70-
func main() {
71-
keyPair, err := tls.LoadX509KeyPair("myservice.cert", "myservice.key")
72-
if err != nil {
73-
panic(err) // TODO handle error
74-
}
75-
keyPair.Leaf, err = x509.ParseCertificate(keyPair.Certificate[0])
76-
if err != nil {
77-
panic(err) // TODO handle error
78-
}
79-
80-
idpMetadataURL, err := url.Parse("https://www.testshib.org/metadata/testshib-providers.xml")
81-
if err != nil {
82-
panic(err) // TODO handle error
83-
}
84-
85-
rootURL, err := url.Parse("http://localhost:8000")
86-
if err != nil {
87-
panic(err) // TODO handle error
88-
}
89-
90-
samlSP, _ := samlsp.New(samlsp.Options{
91-
URL: *rootURL,
92-
Key: kp.PrivateKey.(*rsa.PrivateKey),
93-
Certificate: kp.Leaf,
94-
IDPMetadataURL: idpMetadataURL,
95-
})
96-
app := http.HandlerFunc(hello)
97-
http.Handle("/hello", samlSP.RequireAccount(app))
98-
http.Handle("/saml/", samlSP)
99-
http.ListenAndServe(":8000", nil)
86+
rootURL, err := url.Parse("http://localhost:8000")
87+
if err != nil {
88+
panic(err) // TODO handle error
10089
}
10190

91+
samlSP, _ := samlsp.New(samlsp.Options{
92+
URL: *rootURL,
93+
Key: kp.PrivateKey.(*rsa.PrivateKey),
94+
Certificate: kp.Leaf,
95+
IDPMetadataURL: idpMetadataURL,
96+
})
97+
app := http.HandlerFunc(hello)
98+
http.Handle("/hello", samlSP.RequireAccount(app))
99+
http.Handle("/saml/", samlSP)
100+
http.ListenAndServe(":8000", nil)
101+
}
102+
```
102103

103104
Next we'll have to register our service provider with the identiy provider to establish trust from the service provider to the IDP. For [testshib.org](https://www.testshib.org/), you can do something like:
104105

0 commit comments

Comments
 (0)