Skip to content

Commit d0fc173

Browse files
committed
remove pointless dependency on ancient http router; use std library instead
1 parent efcf0ce commit d0fc173

File tree

10 files changed

+81
-107
lines changed

10 files changed

+81
-107
lines changed

example/idp/idp.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"crypto/x509"
77
"encoding/pem"
88
"flag"
9+
"net/http"
910
"net/url"
1011

11-
"github.com/zenazn/goji"
1212
"golang.org/x/crypto/bcrypt"
1313

1414
"github.com/crewjam/saml/logger"
@@ -118,6 +118,5 @@ func main() {
118118
logr.Fatalf("%s", err)
119119
}
120120

121-
goji.Handle("/*", idpServer)
122-
goji.Serve()
121+
http.ListenAndServe(":8080", idpServer)
123122
}

example/service.go

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@ import (
1414
"net/url"
1515
"strings"
1616

17+
"github.com/crewjam/saml/samlsp"
1718
"github.com/dchest/uniuri"
1819
"github.com/kr/pretty"
19-
"github.com/zenazn/goji"
20-
"github.com/zenazn/goji/web"
21-
22-
"github.com/crewjam/saml/samlsp"
2320
)
2421

2522
var links = map[string]Link{}
@@ -32,7 +29,7 @@ type Link struct {
3229
}
3330

3431
// CreateLink handles requests to create links
35-
func CreateLink(_ web.C, w http.ResponseWriter, r *http.Request) {
32+
func CreateLink(w http.ResponseWriter, r *http.Request) {
3633
account := r.Header.Get("X-Remote-User")
3734
l := Link{
3835
ShortLink: uniuri.New(),
@@ -45,7 +42,7 @@ func CreateLink(_ web.C, w http.ResponseWriter, r *http.Request) {
4542
}
4643

4744
// ServeLink handles requests to redirect to a link
48-
func ServeLink(_ web.C, w http.ResponseWriter, r *http.Request) {
45+
func ServeLink(w http.ResponseWriter, r *http.Request) {
4946
l, ok := links[strings.TrimPrefix(r.URL.Path, "/")]
5047
if !ok {
5148
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
@@ -55,7 +52,7 @@ func ServeLink(_ web.C, w http.ResponseWriter, r *http.Request) {
5552
}
5653

5754
// ListLinks returns a list of the current user's links
58-
func ListLinks(_ web.C, w http.ResponseWriter, r *http.Request) {
55+
func ListLinks(w http.ResponseWriter, r *http.Request) {
5956
account := r.Header.Get("X-Remote-User")
6057
for _, l := range links {
6158
if l.Owner == account {
@@ -140,11 +137,9 @@ func main() {
140137

141138
// register with the service provider
142139
spMetadataBuf, _ := xml.MarshalIndent(samlSP.ServiceProvider.Metadata(), "", " ")
143-
144140
spURL := *idpMetadataURL
145141
spURL.Path = "/services/sp"
146142
resp, err := http.Post(spURL.String(), "text/xml", bytes.NewReader(spMetadataBuf))
147-
148143
if err != nil {
149144
panic(err)
150145
}
@@ -153,20 +148,17 @@ func main() {
153148
panic(err)
154149
}
155150

156-
goji.Handle("/saml/*", samlSP)
157-
158-
authMux := web.New()
159-
authMux.Use(samlSP.RequireAccount)
160-
authMux.Get("/whoami", func(w http.ResponseWriter, r *http.Request) {
161-
if _, err := pretty.Fprintf(w, "%# v", r); err != nil {
162-
panic(err)
163-
}
164-
})
165-
authMux.Post("/", CreateLink)
166-
authMux.Get("/", ListLinks)
167-
168-
goji.Handle("/*", authMux)
169-
goji.Get("/:link", ServeLink)
170-
171-
goji.Serve()
151+
mux := http.NewServeMux()
152+
mux.Handle("GET /saml/", samlSP)
153+
mux.HandleFunc("GET /{link}", ServeLink)
154+
mux.Handle("GET /whoami", samlSP.RequireAccount(http.HandlerFunc(
155+
func(w http.ResponseWriter, r *http.Request) {
156+
if _, err := pretty.Fprintf(w, "%# v", r); err != nil {
157+
panic(err)
158+
}
159+
})))
160+
mux.Handle("POST /", samlSP.RequireAccount(http.HandlerFunc(CreateLink)))
161+
mux.Handle("GET /", samlSP.RequireAccount(http.HandlerFunc(ListLinks)))
162+
163+
http.ListenAndServe(":8080", mux)
172164
}

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ require (
1414
github.com/mattermost/xml-roundtrip-validator v0.1.0
1515
github.com/russellhaering/goxmldsig v1.4.0
1616
github.com/stretchr/testify v1.10.0
17-
github.com/zenazn/goji v1.0.1
1817
golang.org/x/crypto v0.18.0
1918
gotest.tools v2.2.0+incompatible
2019
)

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
4343
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
4444
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
4545
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
46-
github.com/zenazn/goji v1.0.1 h1:4lbD8Mx2h7IvloP7r2C0D6ltZP6Ufip8Hn0wmSK5LR8=
47-
github.com/zenazn/goji v1.0.1/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
4846
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
4947
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
5048
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

samlidp/samlidp.go

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ import (
88
"html/template"
99
"net/http"
1010
"net/url"
11-
"regexp"
1211
"strings"
1312
"sync"
1413

15-
"github.com/zenazn/goji/web"
16-
1714
"github.com/crewjam/saml"
1815
"github.com/crewjam/saml/logger"
1916
)
@@ -94,40 +91,39 @@ func New(opts Options) (*Server, error) {
9491
// is called automatically for you by New, but you may need to call it
9592
// yourself if you don't create the object using New.)
9693
func (s *Server) InitializeHTTP() {
97-
mux := web.New()
94+
mux := http.NewServeMux()
9895
s.Handler = mux
9996

100-
mux.Get("/metadata", func(w http.ResponseWriter, r *http.Request) {
97+
mux.HandleFunc("GET /metadata", func(w http.ResponseWriter, r *http.Request) {
10198
s.idpConfigMu.RLock()
10299
defer s.idpConfigMu.RUnlock()
103100
s.IDP.ServeMetadata(w, r)
104101
})
105-
mux.Handle("/sso", func(w http.ResponseWriter, r *http.Request) {
102+
mux.HandleFunc("/sso", func(w http.ResponseWriter, r *http.Request) {
106103
s.IDP.ServeSSO(w, r)
107104
})
108105

109-
mux.Handle("/login", s.HandleLogin)
110-
mux.Handle("/login/:shortcut", s.HandleIDPInitiated)
111-
mux.Handle("/login/:shortcut/*", s.HandleIDPInitiated)
112-
113-
mux.Get("/services/", s.HandleListServices)
114-
mux.Get("/services/:id", s.HandleGetService)
115-
mux.Put("/services/:id", s.HandlePutService)
116-
mux.Post("/services/:id", s.HandlePutService)
117-
mux.Delete("/services/:id", s.HandleDeleteService)
118-
119-
mux.Get("/users/", s.HandleListUsers)
120-
mux.Get("/users/:id", s.HandleGetUser)
121-
mux.Put("/users/:id", s.HandlePutUser)
122-
mux.Delete("/users/:id", s.HandleDeleteUser)
123-
124-
sessionPath := regexp.MustCompile("/sessions/(?P<id>.*)")
125-
mux.Get("/sessions/", s.HandleListSessions)
126-
mux.Get(sessionPath, s.HandleGetSession)
127-
mux.Delete(sessionPath, s.HandleDeleteSession)
128-
129-
mux.Get("/shortcuts/", s.HandleListShortcuts)
130-
mux.Get("/shortcuts/:id", s.HandleGetShortcut)
131-
mux.Put("/shortcuts/:id", s.HandlePutShortcut)
132-
mux.Delete("/shortcuts/:id", s.HandleDeleteShortcut)
106+
mux.HandleFunc("/login", s.HandleLogin)
107+
mux.HandleFunc("/login/{shortcut}", s.HandleIDPInitiated)
108+
mux.HandleFunc("/login/{shortcut}/{suffix}", s.HandleIDPInitiated)
109+
110+
mux.HandleFunc("GET /services/", s.HandleListServices)
111+
mux.HandleFunc("GET /services/{id}", s.HandleGetService)
112+
mux.HandleFunc("PUT /services/{id}", s.HandlePutService)
113+
mux.HandleFunc("POST /services/{id}", s.HandlePutService)
114+
mux.HandleFunc("DELETE /services/{id}", s.HandleDeleteService)
115+
116+
mux.HandleFunc("GET /users/", s.HandleListUsers)
117+
mux.HandleFunc("GET /users/{id}", s.HandleGetUser)
118+
mux.HandleFunc("PUT /users/{id}", s.HandlePutUser)
119+
mux.HandleFunc("DELETE /users/{id}", s.HandleDeleteUser)
120+
121+
mux.HandleFunc("GET /sessions/", s.HandleListSessions)
122+
mux.HandleFunc("GET /sessions/{id}", s.HandleGetSession)
123+
mux.HandleFunc("DELETE /sessions/{id}", s.HandleDeleteSession)
124+
125+
mux.HandleFunc("GET /shortcuts/", s.HandleListShortcuts)
126+
mux.HandleFunc("GET /shortcuts/{id}", s.HandleGetShortcut)
127+
mux.HandleFunc("PUT /shortcuts/{id}", s.HandlePutShortcut)
128+
mux.HandleFunc("DELETE /shortcuts/{id}", s.HandleDeleteShortcut)
133129
}

samlidp/service.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"net/http"
88
"os"
99

10-
"github.com/zenazn/goji/web"
11-
1210
"github.com/crewjam/saml"
1311
)
1412

@@ -37,7 +35,7 @@ func (s *Server) GetServiceProvider(_ *http.Request, serviceProviderID string) (
3735

3836
// HandleListServices handles the `GET /services/` request and responds with a JSON formatted list
3937
// of service names.
40-
func (s *Server) HandleListServices(_ web.C, w http.ResponseWriter, _ *http.Request) {
38+
func (s *Server) HandleListServices(w http.ResponseWriter, _ *http.Request) {
4139
services, err := s.Store.List("/services/")
4240
if err != nil {
4341
s.logger.Printf("ERROR: %s", err)
@@ -56,9 +54,9 @@ func (s *Server) HandleListServices(_ web.C, w http.ResponseWriter, _ *http.Requ
5654

5755
// HandleGetService handles the `GET /services/:id` request and responds with the service
5856
// metadata in XML format.
59-
func (s *Server) HandleGetService(c web.C, w http.ResponseWriter, _ *http.Request) {
57+
func (s *Server) HandleGetService(w http.ResponseWriter, r *http.Request) {
6058
service := Service{}
61-
err := s.Store.Get(fmt.Sprintf("/services/%s", c.URLParams["id"]), &service)
59+
err := s.Store.Get(fmt.Sprintf("/services/%s", r.PathValue("id")), &service)
6260
if err != nil {
6361
s.logger.Printf("ERROR: %s", err)
6462
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
@@ -73,7 +71,7 @@ func (s *Server) HandleGetService(c web.C, w http.ResponseWriter, _ *http.Reques
7371

7472
// HandlePutService handles the `PUT /shortcuts/:id` request. It accepts the XML-formatted
7573
// service metadata in the request body and stores it.
76-
func (s *Server) HandlePutService(c web.C, w http.ResponseWriter, r *http.Request) {
74+
func (s *Server) HandlePutService(w http.ResponseWriter, r *http.Request) {
7775
service := Service{}
7876

7977
metadata, err := getSPMetadata(r.Body)
@@ -85,7 +83,7 @@ func (s *Server) HandlePutService(c web.C, w http.ResponseWriter, r *http.Reques
8583

8684
service.Metadata = *metadata
8785

88-
err = s.Store.Put(fmt.Sprintf("/services/%s", c.URLParams["id"]), &service)
86+
err = s.Store.Put(fmt.Sprintf("/services/%s", r.PathValue("id")), &service)
8987
if err != nil {
9088
s.logger.Printf("ERROR: %s", err)
9189
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
@@ -100,16 +98,16 @@ func (s *Server) HandlePutService(c web.C, w http.ResponseWriter, r *http.Reques
10098
}
10199

102100
// HandleDeleteService handles the `DELETE /services/:id` request.
103-
func (s *Server) HandleDeleteService(c web.C, w http.ResponseWriter, _ *http.Request) {
101+
func (s *Server) HandleDeleteService(w http.ResponseWriter, r *http.Request) {
104102
service := Service{}
105-
err := s.Store.Get(fmt.Sprintf("/services/%s", c.URLParams["id"]), &service)
103+
err := s.Store.Get(fmt.Sprintf("/services/%s", r.PathValue("id")), &service)
106104
if err != nil {
107105
s.logger.Printf("ERROR: %s", err)
108106
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
109107
return
110108
}
111109

112-
if err := s.Store.Delete(fmt.Sprintf("/services/%s", c.URLParams["id"])); err != nil {
110+
if err := s.Store.Delete(fmt.Sprintf("/services/%s", r.PathValue("id"))); err != nil {
113111
s.logger.Printf("ERROR: %s", err)
114112
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
115113
return

samlidp/session.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111

1212
"golang.org/x/crypto/bcrypt"
1313

14-
"github.com/zenazn/goji/web"
15-
1614
"github.com/crewjam/saml"
1715
)
1816

@@ -145,7 +143,7 @@ func (s *Server) sendLoginForm(w http.ResponseWriter, req *saml.IdpAuthnRequest,
145143
// in the request body, then they are validated. For valid credentials, the response is a
146144
// 200 OK and the JSON session object. For invalid credentials, the HTML login prompt form
147145
// is sent.
148-
func (s *Server) HandleLogin(_ web.C, w http.ResponseWriter, r *http.Request) {
146+
func (s *Server) HandleLogin(w http.ResponseWriter, r *http.Request) {
149147
if err := r.ParseForm(); err != nil {
150148
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
151149
return
@@ -162,7 +160,7 @@ func (s *Server) HandleLogin(_ web.C, w http.ResponseWriter, r *http.Request) {
162160

163161
// HandleListSessions handles the `GET /sessions/` request and responds with a JSON formatted list
164162
// of session names.
165-
func (s *Server) HandleListSessions(_ web.C, w http.ResponseWriter, _ *http.Request) {
163+
func (s *Server) HandleListSessions(w http.ResponseWriter, _ *http.Request) {
166164
sessions, err := s.Store.List("/sessions/")
167165
if err != nil {
168166
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
@@ -180,9 +178,9 @@ func (s *Server) HandleListSessions(_ web.C, w http.ResponseWriter, _ *http.Requ
180178

181179
// HandleGetSession handles the `GET /sessions/:id` request and responds with the session
182180
// object in JSON format.
183-
func (s *Server) HandleGetSession(c web.C, w http.ResponseWriter, _ *http.Request) {
181+
func (s *Server) HandleGetSession(w http.ResponseWriter, r *http.Request) {
184182
session := saml.Session{}
185-
err := s.Store.Get(fmt.Sprintf("/sessions/%s", c.URLParams["id"]), &session)
183+
err := s.Store.Get(fmt.Sprintf("/sessions/%s", r.PathValue("id")), &session)
186184
if err != nil {
187185
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
188186
return
@@ -195,8 +193,8 @@ func (s *Server) HandleGetSession(c web.C, w http.ResponseWriter, _ *http.Reques
195193

196194
// HandleDeleteSession handles the `DELETE /sessions/:id` request. It invalidates the
197195
// specified session.
198-
func (s *Server) HandleDeleteSession(c web.C, w http.ResponseWriter, _ *http.Request) {
199-
err := s.Store.Delete(fmt.Sprintf("/sessions/%s", c.URLParams["id"]))
196+
func (s *Server) HandleDeleteSession(w http.ResponseWriter, r *http.Request) {
197+
err := s.Store.Delete(fmt.Sprintf("/sessions/%s", r.PathValue("id")))
200198
if err != nil {
201199
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
202200
return

0 commit comments

Comments
 (0)