-
Notifications
You must be signed in to change notification settings - Fork 0
/
logout_response.go
145 lines (128 loc) · 2.8 KB
/
logout_response.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
package provider
import (
"bufio"
"bytes"
"encoding/base64"
"encoding/xml"
"html/template"
"net/http"
"time"
"github.com/tzrd/saml/pkg/provider/xml/saml2"
"github.com/tzrd/saml/pkg/provider/xml/saml2p"
)
type LogoutResponse struct {
LogoutTemplate *template.Template
RelayState string
SAMLResponse string
LogoutURL string
RequestID string
Issuer string
ErrorFunc func(err error)
}
type LogoutResponseForm struct {
RelayState string
SAMLResponse string
LogoutURL string
}
func (r *LogoutResponse) sendBackLogoutResponse(w http.ResponseWriter, resp *saml2p.LogoutResponseType) {
var xmlbuff bytes.Buffer
memWriter := bufio.NewWriter(&xmlbuff)
_, err := memWriter.Write([]byte(xml.Header))
if err != nil {
r.ErrorFunc(err)
return
}
encoder := xml.NewEncoder(memWriter)
err = encoder.Encode(resp)
if err != nil {
r.ErrorFunc(err)
return
}
err = memWriter.Flush()
if err != nil {
r.ErrorFunc(err)
return
}
samlMessage := base64.StdEncoding.EncodeToString(xmlbuff.Bytes())
data := LogoutResponseForm{
RelayState: r.RelayState,
SAMLResponse: samlMessage,
LogoutURL: r.LogoutURL,
}
if err := r.LogoutTemplate.Execute(w, data); err != nil {
r.ErrorFunc(err)
return
}
}
func (r *LogoutResponse) makeSuccessfulLogoutResponse(timeFormat string) *saml2p.LogoutResponseType {
return makeLogoutResponse(
r.RequestID,
r.LogoutURL,
time.Now().UTC().Format(timeFormat),
StatusCodeSuccess,
"",
getIssuer(r.Issuer),
)
}
func (r *LogoutResponse) makeUnsupportedlLogoutResponse(
message string,
timeFormat string,
) *saml2p.LogoutResponseType {
return makeLogoutResponse(
r.RequestID,
r.LogoutURL,
time.Now().UTC().Format(timeFormat),
StatusCodeRequestUnsupported,
message,
getIssuer(r.Issuer),
)
}
func (r *LogoutResponse) makePartialLogoutResponse(
message string,
timeFormat string,
) *saml2p.LogoutResponseType {
return makeLogoutResponse(
r.RequestID,
r.LogoutURL,
time.Now().UTC().Format(timeFormat),
StatusCodePartialLogout,
message,
getIssuer(r.Issuer),
)
}
func (r *LogoutResponse) makeDeniedLogoutResponse(
message string,
timeFormat string,
) *saml2p.LogoutResponseType {
return makeLogoutResponse(
r.RequestID,
r.LogoutURL,
time.Now().UTC().Format(timeFormat),
StatusCodeRequestDenied,
message,
getIssuer(r.Issuer),
)
}
func makeLogoutResponse(
requestID string,
logoutURL string,
issueInstant string,
status string,
message string,
issuer *saml2.NameIDType,
) *saml2p.LogoutResponseType {
return &saml2p.LogoutResponseType{
Id: NewID(),
InResponseTo: requestID,
Version: "2.0",
IssueInstant: issueInstant,
Destination: logoutURL,
Issuer: issuer,
Status: saml2p.StatusType{
StatusCode: saml2p.StatusCodeType{
Value: status,
},
StatusMessage: message,
},
}
}