-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
256 lines (212 loc) · 6.04 KB
/
provider.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package provider
import (
"context"
"crypto/rsa"
"fmt"
"net/http"
"github.com/google/uuid"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/tzrd/saml/pkg/provider/signature"
"github.com/tzrd/saml/pkg/provider/xml/md"
)
const (
DefaultTimeFormat = "2006-01-02T15:04:05.999999Z"
PostBinding = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
RedirectBinding = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"
SOAPBinding = "urn:oasis:names:tc:SAML:2.0:bindings:SOAP"
DefaultMetadataEndpoint = "/metadata"
)
type Storage interface {
EntityStorage
AuthStorage
IdentityProviderStorage
UserStorage
Health(context.Context) error
}
type Config struct {
MetadataConfig *MetadataConfig
IDPConfig *IdentityProviderConfig
Metadata *Endpoint `yaml:"Metadata"`
Organisation *Organisation
ContactPerson *ContactPerson
}
type MetadataConfig struct {
Path string
SignatureAlgorithm string
}
type Certificate struct {
Path string
PrivateKeyPath string
CaPath string
}
type Organisation struct {
Name string
DisplayName string
URL string
}
type ContactPerson struct {
ContactType md.ContactTypeType
Company string
GivenName string
SurName string
EmailAddress string
TelephoneNumber string
}
const (
healthEndpoint = "/healthz"
readinessEndpoint = "/ready"
)
type Provider struct {
storage Storage
httpHandler http.Handler
interceptors []HttpInterceptor
insecure bool
metadataEndpoint *Endpoint
conf *Config
issuerFromRequest IssuerFromRequest
identityProvider *IdentityProvider
timeFormat string
}
func NewProvider(
storage Storage,
path string,
conf *Config,
providerOpts ...Option,
) (*Provider, error) {
metadataEndpoint := NewEndpoint(DefaultMetadataEndpoint)
if conf.Metadata != nil {
metadataEndpoint = *conf.Metadata
}
idp, err := NewIdentityProvider(
metadataEndpoint,
conf.IDPConfig,
storage,
)
if err != nil {
return nil, err
}
prov := &Provider{
metadataEndpoint: &metadataEndpoint,
storage: storage,
conf: conf,
identityProvider: idp,
}
for _, optFunc := range providerOpts {
if err := optFunc(prov); err != nil {
return nil, err
}
}
issuerFromRequest, err := IssuerFromHost(path)(prov.insecure)
if err != nil {
return nil, err
}
prov.issuerFromRequest = issuerFromRequest
prov.httpHandler = CreateRouter(prov, prov.interceptors...)
return prov, nil
}
func NewID() string {
return fmt.Sprintf("_%s", uuid.New())
}
func (p *Provider) IssuerFromRequest(r *http.Request) string {
return p.issuerFromRequest(r)
}
type Option func(o *Provider) error
func WithHttpInterceptors(interceptors ...HttpInterceptor) Option {
return func(p *Provider) error {
p.interceptors = append(p.interceptors, interceptors...)
return nil
}
}
func (p *Provider) HttpHandler() http.Handler {
return p.httpHandler
}
func (p *Provider) Health(ctx context.Context) error {
return p.storage.Health(ctx)
}
func (p *Provider) Probes() []ProbesFn {
return []ProbesFn{
ReadyStorage(p.storage),
}
}
func (p *Provider) GetMetadata(ctx context.Context) (*md.EntityDescriptorType, error) {
metadata, err := p.conf.getMetadata(ctx, p.identityProvider)
if err != nil {
return nil, err
}
cert, key, err := getMetadataCert(ctx, p.storage)
if p.conf.MetadataConfig != nil && p.conf.MetadataConfig.SignatureAlgorithm != "" {
signer, err := signature.GetSigner(cert, key, p.conf.MetadataConfig.SignatureAlgorithm)
if err != nil {
return nil, err
}
idpSig, err := signature.Create(signer, metadata)
if err != nil {
return nil, err
}
metadata.Signature = idpSig
}
return metadata, nil
}
func getMetadataCert(ctx context.Context, storage EntityStorage) ([]byte, *rsa.PrivateKey, error) {
certAndKey, err := storage.GetMetadataSigningKey(ctx)
if err != nil {
return nil, nil, err
}
if certAndKey.Key == nil || certAndKey.Certificate == nil {
return nil, nil, fmt.Errorf("signer has no key")
}
return certAndKey.Certificate, certAndKey.Key, nil
}
type HttpInterceptor func(http.Handler) http.Handler
func CreateRouter(p *Provider, interceptors ...HttpInterceptor) *mux.Router {
router := mux.NewRouter()
router.Use(intercept(p.issuerFromRequest, interceptors...))
router.HandleFunc(healthEndpoint, healthHandler)
router.HandleFunc(readinessEndpoint, readyHandler(p.Probes()))
router.HandleFunc(p.metadataEndpoint.Relative(), p.metadataHandle)
if p.identityProvider != nil {
for _, route := range p.identityProvider.GetRoutes() {
router.Handle(route.Endpoint, route.HandleFunc)
}
}
return router
}
var allowAllOrigins = func(_ string) bool {
return true
}
// AuthCallbackURL builds the url for the redirect (with the requestID) after a successful login
func AuthCallbackURL(p *Provider) func(context.Context, string) string {
return func(ctx context.Context, requestID string) string {
return p.identityProvider.endpoints.callbackEndpoint.Absolute(IssuerFromContext(ctx)) + "?id=" + requestID
}
}
func intercept(i IssuerFromRequest, interceptors ...HttpInterceptor) func(handler http.Handler) http.Handler {
cors := handlers.CORS(
handlers.AllowCredentials(),
handlers.AllowedHeaders([]string{"authorization", "content-type"}),
handlers.AllowedOriginValidator(allowAllOrigins),
)
issuerInterceptor := NewIssuerInterceptor(i)
return func(handler http.Handler) http.Handler {
for i := len(interceptors) - 1; i >= 0; i-- {
handler = interceptors[i](handler)
}
return cors(issuerInterceptor.Handler(handler))
}
}
// WithAllowInsecure allows the use of http (instead of https) for issuers
// this is not recommended for production use and violates the SAML specification
func WithAllowInsecure() Option {
return func(p *Provider) error {
p.insecure = true
return nil
}
}
// WithCustomTimeFormat allows the use of a custom timeformat instead of the default
func WithCustomTimeFormat(timeFormat string) Option {
return func(p *Provider) error {
p.identityProvider.timeFormat = timeFormat
return nil
}
}