-
Notifications
You must be signed in to change notification settings - Fork 0
/
saml.go
157 lines (127 loc) · 3.54 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
package intuit
import (
"bytes"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"github.com/MattNewberry/oauth"
"github.com/nu7hatch/gouuid"
"io/ioutil"
"net/http"
"net/url"
"strings"
"text/template"
"time"
)
type Assertion struct {
IssuerId string
UserId string
RefId string
TimeNow string
TimeBefore string
TimeAfter string
Signature string
}
type SignedInfo struct {
RefId string
Digest string
}
type Signature struct {
SignatureValue string
SignedInfo string
}
func MakeSamlAssertion() (*oauth.AccessToken, error) {
a := &Assertion{}
a.IssuerId = SessionConfiguration.SamlProviderId
a.UserId = SessionConfiguration.CustomerId
a.RefId = newUUId()
t := time.Now()
a.TimeNow = a.formatTimeFromDuration(t, 0)
a.TimeBefore = a.formatTimeFromDuration(t, -5*time.Minute)
a.TimeAfter = a.formatTimeFromDuration(t, 10*time.Minute)
si := signedInfoFromAssertion(a)
s := &Signature{}
s.SignatureValue = si.SignatureValue(SessionConfiguration.CertificatePath)
s.SignedInfo = si.String()
signature := s.String()
a.Signature = signature
payload := base64.URLEncoding.EncodeToString([]byte(a.String()))
values := make(url.Values)
values.Set("saml_assertion", payload)
values.Set("oauth_consumer_key", SessionConfiguration.OAuthConsumerKey)
resp, err := http.PostForm("https://oauth.intuit.com/oauth/v1/get_access_token_by_saml", values)
tokens := &oauth.AccessToken{}
if err != nil || resp.StatusCode != 200 {
db, _ := url.QueryUnescape(resp.Header.Get("Www-Authenticate"))
msg := fmt.Sprintf("%s %s", resp.Status, db)
err = errors.New(msg)
} else {
body, _ := ioutil.ReadAll(resp.Body)
bValues, _ := url.ParseQuery(string(body))
tokens.Token = bValues.Get("oauth_token")
tokens.Secret = bValues.Get("oauth_token_secret")
}
return tokens, err
}
func (a *Assertion) String() string {
return parseTemplate("saml_assertion", a)
}
func (s *Signature) String() string {
return parseTemplate("saml_signature", s)
}
func (s *SignedInfo) String() string {
return parseTemplate("saml_signed", s)
}
func parseTemplate(file string, data interface{}) string {
t, _ := template.ParseFiles("templates/" + file + ".xml")
var buf bytes.Buffer
t.Execute(&buf, data)
return buf.String()
}
func sha1Encode(a string) string {
h := sha1.New()
h.Write([]byte(a))
return string(h.Sum(nil))
}
func (a *Assertion) formatTimeFromDuration(t time.Time, d time.Duration) string {
const layout = "2006-01-02T15:04:05"
return fmt.Sprintf("%s.000Z", t.Add(d).UTC().Format(layout))
}
func newUUId() string {
uuid, _ := uuid.NewV4()
return fmt.Sprintf("_%s", strings.Replace(uuid.String(), "-", "", -1))
}
func signedInfoFromAssertion(a *Assertion) *SignedInfo {
s := &SignedInfo{}
s.RefId = a.RefId
sha := sha1Encode(a.String())
s.Digest = base64.StdEncoding.EncodeToString([]byte(sha))
return s
}
func (s *SignedInfo) SignatureValue(keyPath string) string {
pkey, err := ioutil.ReadFile(keyPath)
if err != nil {
panic(err)
}
block, _ := pem.Decode(pkey)
if block == nil {
panic(fmt.Sprintf("bad key data: %s", "not PEM-encoded"))
}
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
panic(fmt.Sprintf("bad private key: %s", err))
}
signedString := s.String()
digest := []byte(sha1Encode(signedString))
signature, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA1, digest)
if err != nil {
panic(fmt.Sprintf("rsa.SignPKCS1v15 error: %v\n", err))
}
return base64.StdEncoding.EncodeToString([]byte(signature))
}