/
saml.go
186 lines (163 loc) · 4.41 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
package ginkit
import (
"context"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"net/http"
"net/url"
"os"
"github.com/crewjam/saml"
"github.com/crewjam/saml/samlsp"
"github.com/gin-gonic/gin"
adapter "github.com/gwatts/gin-adapter"
)
// metadataFile metadata.xml
// certFile myservice.cert
// keyFile myservice.key
// entityID asdfasdf
// rootURL https://devlocal.site:8000
// ACS URL: /saml/acs
// TODO: Implement SAMLGroupConfig
type (
SAMLGroupConfig struct {
MetaDataFile string
MetaDataURL string
CertFile string
KeyFile string
EntityID string
RootURL string
ParamMap map[string]string
AllowSkip bool
}
)
func (e *Engine) SAMLGroup(path string, config *SAMLGroupConfig) *gin.RouterGroup {
samlSP := e.SAMLInit(config)
restricted := e.Router().Group(path)
restricted.Use(SAMLMiddleware(samlSP))
restricted.Use(SAMLtoParamsMapMiddleware(config.ParamMap))
return restricted
}
func (e *Engine) SAMLSetup(config *SAMLGroupConfig) *samlsp.Middleware {
// TODO: Move to builtin Cert Store
keyPair, err := tls.LoadX509KeyPair(config.CertFile, config.KeyFile)
if err != nil {
panic(err) // TODO handle error
}
keyPair.Leaf, err = x509.ParseCertificate(keyPair.Certificate[0])
if err != nil {
panic(err) // TODO handle error
}
var idpMetadata *saml.EntityDescriptor
// TODO - Support URL
if config.MetaDataURL != "" {
idpMetadataURL, err := url.Parse(config.MetaDataURL)
if err != nil {
panic(err) // TODO handle error
}
idpMetadata, err = samlsp.FetchMetadata(context.Background(), http.DefaultClient, *idpMetadataURL)
if err != nil {
panic(err) // TODO handle error
}
} else {
xmlFile, err := os.ReadFile(config.MetaDataFile)
if err != nil {
panic(err) // TODO handle error
}
idpMetadata, err = samlsp.ParseMetadata(xmlFile)
if err != nil {
panic(err) // TODO handle error
}
}
rootURLParsed, err := url.Parse(config.RootURL)
if err != nil {
panic(err) // TODO handle error
}
samlSP, err := samlsp.New(samlsp.Options{
EntityID: config.EntityID,
DefaultRedirectURI: "/",
URL: *rootURLParsed,
AllowIDPInitiated: true,
Key: keyPair.PrivateKey.(*rsa.PrivateKey),
Certificate: keyPair.Leaf,
IDPMetadata: idpMetadata,
})
if err != nil {
panic(err) // TODO handle error
}
return samlSP
}
func (e *Engine) SAMLInit(config *SAMLGroupConfig) *samlsp.Middleware {
samlSP := e.SAMLSetup(config)
// TODO Does this need to be any?
// TODO make /saml path configurable
// e.Router().Any("/saml/*action", gin.WrapH(samlSP))
e.Router().GET("/saml/*action", gin.WrapH(samlSP))
e.Router().POST("/saml/*action", gin.WrapH(samlSP))
return samlSP
}
func SAMLMiddleware(samlSP *samlsp.Middleware) func(c *gin.Context) {
return func(c *gin.Context) {
Authenticated := c.GetBool("authenticated")
if Authenticated {
return
}
adapter.Wrap(samlSP.RequireAccount)(c)
}
}
func SAMLtoParamsMiddleware(params ...string) func(c *gin.Context) {
return func(c *gin.Context) {
// Range over all params which we want to add from the saml context
for _, param := range params {
// Get the saml attribute from the saml context
value := samlsp.AttributeFromContext(c.Request.Context(), param)
// Determine if there was already a param
_, exists := c.Params.Get(param)
// If the param already exists overwrite else append param
if exists {
for i, entry := range c.Params {
if entry.Key == param {
c.Params[i] = gin.Param{
Key: param,
Value: value,
}
return
}
}
} else {
c.Params = append(c.Params, gin.Param{
Key: param,
Value: value,
})
}
}
}
}
func SAMLtoParamsMapMiddleware(params map[string]string) func(c *gin.Context) {
return func(c *gin.Context) {
// Range over all params which we want to add from the saml context
for param, key := range params {
// Get the saml attribute from the saml context
value := samlsp.AttributeFromContext(c.Request.Context(), key)
// Determine if there was already a param
_, exists := c.Params.Get(param)
// If the param already exists overwrite else append param
if exists {
for i, entry := range c.Params {
if entry.Key == param {
c.Params[i] = gin.Param{
Key: param,
Value: value,
}
return
}
}
} else {
c.Params = append(c.Params, gin.Param{
Key: param,
Value: value,
})
}
}
}
}