This repository has been archived by the owner on Oct 3, 2020. It is now read-only.
forked from RobotsAndPencils/go-saml
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsendgrid.go
115 lines (102 loc) · 3.54 KB
/
sendgrid.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
package saml
import (
"fmt"
"io/ioutil"
"os"
)
func GenerateAuthnRequest(publicCertificate, privateKey, idpSSOUrl, idpSSODescriptorUrl,
idpPublicCertificate, assertionConsumerServiceUrl string) (string, error) {
//Create a temporary file for the public certificate
publicCertificateFile, err := ioutil.TempFile(os.TempDir(), "publicCertificate")
if err != nil {
return "", err
}
publicCertificatePath := publicCertificateFile.Name()
defer deleteTempFile(publicCertificatePath)
publicCertificateFile.WriteString(publicCertificate)
publicCertificateFile.Close()
//Create a temporary file for the private key
privateKeyFile, err := ioutil.TempFile(os.TempDir(), "privateKey")
if err != nil {
return "", err
}
privateKeyPath := privateKeyFile.Name()
defer deleteTempFile(privateKeyPath)
privateKeyFile.WriteString(privateKey)
privateKeyFile.Close()
//Create a temporary file for the idp public certificate
idpPublicCertificateFile, err := ioutil.TempFile(os.TempDir(), "privateKey")
if err != nil {
return "", err
}
idpPublicCertificatePath := idpPublicCertificateFile.Name()
defer deleteTempFile(idpPublicCertificatePath)
idpPublicCertificateFile.WriteString(idpPublicCertificate)
idpPublicCertificateFile.Close()
sp := ServiceProviderSettings{
PublicCertPath: publicCertificatePath,
PrivateKeyPath: privateKeyPath,
IDPSSOURL: idpSSOUrl,
IDPSSODescriptorURL: idpSSODescriptorUrl,
IDPPublicCertPath: idpPublicCertificatePath,
AssertionConsumerServiceURL: assertionConsumerServiceUrl,
}
err = sp.Init()
if err != nil {
return "", err
}
// generate the AuthnRequest and then get a base64 encoded string of the XML
authnRequest := sp.GetAuthnRequest()
b64XML, err := authnRequest.EncodedSignedString(sp.PrivateKeyPath)
if err != nil {
return "", err
}
return b64XML, nil
}
func ValidateSamlResponse(response *Response, publicCertificate, privateKey, idpSSOUrl,
idpSSODescriptorUrl, idpPublicCertificate, assertionConsumerServiceUrl string) (error) {
//Create a temporary file for the public certificate
publicCertificateFile, err := ioutil.TempFile(os.TempDir(), "publicCertificate")
if err != nil {
return err
}
publicCertificatePath := publicCertificateFile.Name()
defer deleteTempFile(publicCertificatePath)
publicCertificateFile.WriteString(publicCertificate)
publicCertificateFile.Close()
//Create a temporary file for the private key
privateKeyFile, err := ioutil.TempFile(os.TempDir(), "privateKey")
if err != nil {
return err
}
privateKeyPath := privateKeyFile.Name()
defer deleteTempFile(privateKeyPath)
privateKeyFile.WriteString(privateKey)
privateKeyFile.Close()
//Create a temporary file for the idp public certificate
idpPublicCertificateFile, err := ioutil.TempFile(os.TempDir(), "idpPublicCertificate")
if err != nil {
return err
}
idpPublicCertificatePath := idpPublicCertificateFile.Name()
defer deleteTempFile(idpPublicCertificatePath)
idpPublicCertificateFile.WriteString(idpPublicCertificate)
idpPublicCertificateFile.Close()
sp := ServiceProviderSettings{
PublicCertPath: publicCertificatePath,
PrivateKeyPath: privateKeyPath,
IDPSSOURL: idpSSOUrl,
IDPSSODescriptorURL: idpSSODescriptorUrl,
IDPPublicCertPath: idpPublicCertificatePath,
AssertionConsumerServiceURL: assertionConsumerServiceUrl,
}
sp.Init()
if err != nil {
return err
}
err = response.Validate(&sp)
if err != nil {
return fmt.Errorf("SAMLResponse validation: "+err.Error())
}
return nil
}