/
saml.go
191 lines (172 loc) · 4.72 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package saml
import (
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"net/http"
"net/url"
"path"
"github.com/crewjam/saml"
"github.com/crewjam/saml/samlsp"
"github.com/n-creativesystem/saml-proxy/infra/redis"
"github.com/n-creativesystem/saml-proxy/internal/options"
"github.com/n-creativesystem/saml-proxy/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
const defaultLogoutURL = "/logout"
type viperConfig struct {
MetadataURL string `json:"metadataUrl"`
X509Cert string `json:"x509Cert"`
X509Key string `json:"x509Key"`
Redirect options.Redirect `json:"redirect"`
// IdpLogout string `json:"idpLogout"`
CookieName string `json:"cookieName"`
Upstream options.Upstream `json:"upstream"`
NamedIdFormat string `json:"namedIdFormat"`
LogoutURL string `json:"logoutUrl"`
}
func newViperConfig() (*viperConfig, error) {
baseConfig := viperConfig{}
if err := utils.LoadYAML(viper.GetString("saml-config"), &baseConfig); err != nil {
return nil, err
}
return &baseConfig, nil
}
type redirect struct {
root string
logout string
}
type Config struct {
metadataURL string
x509Cert string
x509Key string
redirect redirect
// idpLogout string
cookieName string
nameIdFormat string
logoutURL string
upstream upstream
}
func (c *Config) GetProxyURL() *url.URL {
return c.upstream.url
}
func (c *Config) GetResources() Matching {
return c.upstream.resources
}
func (c *Config) GetMapping() options.Mapping {
return c.upstream.mapping
}
func (c *Config) InSecureSkipVerify() bool {
return !c.upstream.inSecureSkipTlsVerify
}
func (c *Config) GetInjectRequestHeaders() []options.Header {
return c.upstream.injectRequestHeaders
}
func NewConfig() *Config {
baseConfig, err := newViperConfig()
if err != nil {
logrus.Fatalln(err)
}
redirectLogout := "/"
if baseConfig.Redirect.LogoutRedirect != "" {
u, err := url.Parse(baseConfig.Redirect.LogoutRedirect)
if err != nil {
logrus.Fatalln(err)
}
redirectLogout = u.RequestURI()
}
if baseConfig.LogoutURL == "" {
baseConfig.LogoutURL = defaultLogoutURL
}
logoutURL := path.Join("/saml", baseConfig.LogoutURL)
config := &Config{
metadataURL: baseConfig.MetadataURL,
x509Cert: baseConfig.X509Cert,
x509Key: baseConfig.X509Key,
redirect: redirect{
root: baseConfig.Redirect.RootURL,
logout: redirectLogout,
},
cookieName: baseConfig.CookieName,
nameIdFormat: baseConfig.NamedIdFormat,
logoutURL: logoutURL,
upstream: newUpstream(baseConfig.Upstream),
}
return config
}
type Middleware struct {
*samlsp.Middleware
config *Config
}
var _ http.Handler = (*Middleware)(nil)
func (m *Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == m.config.logoutURL {
if err := m.Middleware.Session.DeleteSession(w, r); err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
} else {
http.Redirect(w, r, m.config.redirect.logout, http.StatusFound)
}
} else {
m.Middleware.ServeHTTP(w, r)
}
}
func New(config *Config, con redis.Redis) *Middleware {
sp := ServiceProvider(config, con)
return &Middleware{
Middleware: sp,
config: config,
}
}
func ServiceProvider(config *Config, con redis.Redis) *samlsp.Middleware {
type certificate struct {
privateKey *rsa.PrivateKey
certificate *x509.Certificate
}
var cert certificate
if config.x509Cert != "" && config.x509Key != "" {
keyPair, err := tls.LoadX509KeyPair(config.x509Cert, config.x509Key)
if err != nil {
panic(err)
}
keyPair.Leaf, err = x509.ParseCertificate(keyPair.Certificate[0])
if err != nil {
panic(err)
}
cert.privateKey = keyPair.PrivateKey.(*rsa.PrivateKey)
cert.certificate = keyPair.Leaf
}
idpMetadataURL, err := url.Parse(config.metadataURL)
if err != nil {
panic(err)
}
loginRedirect, err := url.Parse(config.redirect.root)
if err != nil {
panic(err)
}
opts := samlsp.Options{
URL: *loginRedirect,
Key: cert.privateKey,
Certificate: cert.certificate,
IDPMetadataURL: idpMetadataURL,
}
samlSP, err := samlsp.New(opts)
samlSP.Session = NewSessionProvider(config, opts, con)
if err != nil {
panic(err)
}
switch config.nameIdFormat {
case "unspecified":
samlSP.ServiceProvider.AuthnNameIDFormat = saml.UnspecifiedNameIDFormat
case "transient":
samlSP.ServiceProvider.AuthnNameIDFormat = saml.TransientNameIDFormat
case "email":
samlSP.ServiceProvider.AuthnNameIDFormat = saml.EmailAddressNameIDFormat
case "persistent":
samlSP.ServiceProvider.AuthnNameIDFormat = saml.PersistentNameIDFormat
default:
samlSP.ServiceProvider.AuthnNameIDFormat = saml.NameIDFormat(config.nameIdFormat)
}
return samlSP
}