This repository has been archived by the owner on Apr 1, 2024. It is now read-only.
forked from dustin-decker/saml-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaml.go
74 lines (68 loc) · 2.18 KB
/
saml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"github.com/crewjam/saml/samlsp"
"github.com/labstack/echo"
log "github.com/sirupsen/logrus"
"net/url"
"strings"
)
func samlHeaders(config HostConfig) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
req := c.Request()
attributes := samlsp.Token(req.Context()).Attributes
for _, attr := range config.AddAttributesAsHeaders {
if val, ok := attributes[attr]; ok {
req.Header.Add("X-Saml-"+attr, strings.Join(val, " "))
} else {
log.WithFields(log.Fields{"attrs_available": attributes,
"attr": attr}).Warn("given attr not in attributes")
}
}
return next(c)
}
}
}
func (s *Server) NewSamlSP(hostConfig HostConfig) *samlsp.Middleware {
keyPair, err := tls.LoadX509KeyPair(s.config.CertPath, s.config.KeyPath)
if err != nil {
log.WithFields(log.Fields{
"cert_path": s.config.CertPath,
"key_path": s.config.KeyPath,
"error": err.Error()}).Fatal("could not load keypair")
}
keyPair.Leaf, err = x509.ParseCertificate(keyPair.Certificate[0])
if err != nil {
log.WithFields(log.Fields{
"cert_path": s.config.CertPath,
"error": err.Error()}).Fatal("could not parse certificate")
}
idpMetadataURL, err := url.Parse(hostConfig.IdpMetadataURL)
if err != nil {
log.WithFields(log.Fields{
"idp_metadata_url": hostConfig.IdpMetadataURL,
"error": err.Error()}).Fatal("could not parse metadata URL")
}
rootURL, err := url.Parse(hostConfig.ServiceRootURL)
if err != nil {
log.WithFields(log.Fields{
"service_root_url": hostConfig.ServiceRootURL,
"error": err.Error()}).Fatal("could not parse service root URL")
}
// initialize SAML middleware
samlSP, err := samlsp.New(samlsp.Options{
URL: *rootURL,
Key: keyPair.PrivateKey.(*rsa.PrivateKey),
Certificate: keyPair.Leaf,
IDPMetadataURL: idpMetadataURL,
AllowIDPInitiated: hostConfig.AllowIDPInitiated,
CookieMaxAge: s.config.CookieMaxAge,
})
if err != nil {
log.WithFields(log.Fields{"error": err.Error()}).Fatal("could not initialize SAML middleware")
}
return samlSP
}