forked from go-cas/cas
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logout_request.go
77 lines (63 loc) · 1.74 KB
/
logout_request.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
package cas
import (
"crypto/rand"
"encoding/xml"
"strings"
"time"
)
// Represents the XML CAS Single Log Out Request data
type logoutRequest struct {
XMLName xml.Name `xml:"urn:oasis:names:tc:SAML:2.0:protocol LogoutRequest"`
Version string `xml:"Version,attr"`
IssueInstant time.Time `xml:"-"`
RawIssueInstant string `xml:"IssueInstant,attr"`
ID string `xml:"ID,attr"`
NameID string `xml:"urn:oasis:names:tc:SAML:2.0:assertion NameID"`
SessionIndex string `xml:"SessionIndex"`
}
func parseLogoutRequest(data []byte) (*logoutRequest, error) {
l := &logoutRequest{}
if err := xml.Unmarshal(data, &l); err != nil {
return nil, err
}
t, err := parseDate(l.RawIssueInstant)
if err != nil {
return nil, err
}
l.IssueInstant = t
l.NameID = strings.TrimSpace(l.NameID)
l.SessionIndex = strings.TrimSpace(l.SessionIndex)
return l, nil
}
func parseDate(raw string) (time.Time, error) {
t, err := time.Parse(time.RFC1123Z, raw)
if err != nil {
// if RFC1123Z does not match, we will try iso8601
t, err = time.Parse("2006-01-02T15:04:05Z0700", raw)
if err != nil {
return t, err
}
}
return t, nil
}
func newLogoutRequestID() string {
const alphabet = "abcdef0123456789"
// generate 64 character string
bytes := make([]byte, 64)
rand.Read(bytes)
for k, v := range bytes {
bytes[k] = alphabet[v%byte(len(alphabet))]
}
return string(bytes)
}
func xmlLogoutRequest(ticket string) ([]byte, error) {
l := &logoutRequest{
Version: "2.0",
IssueInstant: time.Now().UTC(),
ID: newLogoutRequestID(),
NameID: "@NOT_USED@",
SessionIndex: ticket,
}
l.RawIssueInstant = l.IssueInstant.Format(time.RFC1123Z)
return xml.MarshalIndent(l, "", " ")
}