-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
111 lines (93 loc) · 3.14 KB
/
main.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
package main
import (
"context"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"github.com/crewjam/saml"
"github.com/crewjam/saml/samlsp"
"net/http"
"net/url"
)
func init() {
config := &SAMLConfig{
IDPMetadataURL: "https://login.microsoftonline.com/2a3490ef-c3df-4323-ae94-a75f83817991/federationmetadata/2007-06/federationmetadata.xml?appid=336fcc57-ddf4-4748-ab81-69dadbaf2648", //os.Getenv("TYK_SAML_METADATA_URL"),
CertFile: "myservice.cert",
KeyFile: "myservice.key",
BaseURL: "https://8e1c71502ab8.ngrok.io", //os.Getenv("TYK_SAML_BASE_URL"),
SPMetadataURL: "/websso/saml/metadata",
SPAcsURL: "/websso/saml/acs",
SPSloURL: "/websso/saml/slo",
ForceAuthentication: true,
}
logger.Debug("Initialising middleware SAML")
//needs to match the signing cert if IDP
keyPair, err := tls.LoadX509KeyPair(config.CertFile, config.KeyFile)
if err != nil {
logger.Errorf("Error loading keypair: %v", err)
}
keyPair.Leaf, err = x509.ParseCertificate(keyPair.Certificate[0])
if err != nil {
logger.Errorf("Error parsing certificate: %v", err)
}
idpMetadataURL, err := url.Parse(config.IDPMetadataURL)
if err != nil {
logger.Errorf("Error parsing IDP metadata URL: %v", err)
}
logger.Debugf("IDPmetadataURL is: %v", idpMetadataURL.String())
rootURL, err := url.Parse(config.BaseURL)
if err != nil {
logger.Errorf("Error parsing SAMLBaseURL: %v", err)
}
httpClient := http.DefaultClient
metadata, err := samlsp.FetchMetadata(context.TODO(), httpClient, *idpMetadataURL)
if err != nil {
logger.Errorf("Error retrieving IDP Metadata: %v", err)
}
logger.Debugf("Root URL: %v", rootURL.String())
opts := samlsp.Options{
URL: *rootURL,
Key: keyPair.PrivateKey.(*rsa.PrivateKey),
}
metadataURL := rootURL.ResolveReference(&url.URL{Path: config.SPMetadataURL})
acsURL := rootURL.ResolveReference(&url.URL{Path: config.SPAcsURL})
sloURL := rootURL.ResolveReference(&url.URL{Path: config.SPSloURL})
logger.Infof("SP metadata URL: %v", metadataURL.String())
logger.Infof("SP acs URL: %v", acsURL.String())
var forceAuthn = config.ForceAuthentication
sp := saml.ServiceProvider{
EntityID: metadataURL.String(),
Key: keyPair.PrivateKey.(*rsa.PrivateKey),
Certificate: keyPair.Leaf,
MetadataURL: *metadataURL,
AcsURL: *acsURL,
SloURL: *sloURL,
IDPMetadata: metadata,
ForceAuthn: &forceAuthn,
AllowIDPInitiated: false,
}
Middleware = &samlsp.Middleware{
ServiceProvider: sp,
OnError: samlsp.DefaultOnError,
Session: samlsp.DefaultSessionProvider(opts),
//Session: samlsp.CookieSessionProvider{
// Name: "token",
// Domain: rootURL.Host,
// MaxAge: time.Second * 3600,
// HTTPOnly: true,
// Secure: rootURL.Scheme == "https",
// Codec: samlsp.JWTSessionCodec{
// SigningMethod: jwt.SigningMethodRS256,
// Audience: config.SessionJWTAud,
// Issuer: config.SessionJWTIss,
// MaxAge: time.Second * 3600,
// Key: keyPair.PrivateKey.(*rsa.PrivateKey),
// },
//},
}
Middleware.RequestTracker = samlsp.DefaultRequestTracker(opts,&sp)
logger.Info("SAML Middleware initialised")
}
func main() {
//not run for a Go plugin
}