-
Notifications
You must be signed in to change notification settings - Fork 96
/
cas_test.go
169 lines (146 loc) · 4.18 KB
/
cas_test.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
158
159
160
161
162
163
164
165
166
167
168
169
package cas
import (
"encoding/xml"
"fmt"
"net/http"
"time"
)
type AttributesStyle int
const (
CasThreeZeroNamedAttributesStyle AttributesStyle = iota
CasThreeZeroAnyAttributesStyle
RubyCasAttributesStyle
)
func (as AttributesStyle) String() string {
switch as {
case CasThreeZeroNamedAttributesStyle:
return "CasThreeZeroNamedAttributesStyle"
case CasThreeZeroAnyAttributesStyle:
return "CasThreeZeroAnyAttributesStyle"
case RubyCasAttributesStyle:
return "RubyCasAttributesStyle"
default:
return ""
}
}
type TestServer struct {
serviceTickets map[string]*TestTicket
}
type TestTicket struct {
Name string
Service string
Username string
ProxyGrantingTicket string
Attributes UserAttributes
AttributesStyle AttributesStyle
}
func (ts *TestServer) NewTicket(ticket string) *TestTicket {
t := &TestTicket{
Name: ticket,
Attributes: make(UserAttributes),
AttributesStyle: CasThreeZeroNamedAttributesStyle,
}
return t
}
func (ts *TestServer) AddTicket(ticket *TestTicket) {
if ts.serviceTickets == nil {
ts.serviceTickets = make(map[string]*TestTicket)
}
ts.serviceTickets[ticket.Name] = ticket
}
func (ts *TestServer) Close() {
ts.serviceTickets = nil
}
func (ts *TestServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
switch r.URL.Path {
case "/validate":
ticket := query.Get("ticket")
service := query.Get("service")
if t, ok := ts.serviceTickets[ticket]; ok {
if t.Service == service {
fmt.Fprintf(w, "yes\n%v\n", t.Username)
} else {
fmt.Fprintf(w, "no\n\n")
}
} else {
fmt.Fprintf(w, "no\n\n")
}
return
case "/serviceValidate":
ticket := query.Get("ticket")
service := query.Get("service")
var serviceResponse *xmlServiceResponse
if t, ok := ts.serviceTickets[ticket]; ok {
if t.Service == service {
serviceResponse = successServiceResponse(t.Username, t.ProxyGrantingTicket)
switch t.AttributesStyle {
case CasThreeZeroNamedAttributesStyle:
var userAttributes []*xmlNamedAttribute
for key, values := range t.Attributes {
for _, value := range values {
userAttributes = append(userAttributes, &xmlNamedAttribute{
Name: key,
Value: value,
})
}
}
serviceResponse.Success.Attributes = &xmlAttributes{
AuthenticationDate: time.Now().UTC(),
LongTermAuthenticationRequestTokenUsed: false,
IsFromNewLogin: true,
}
if len(userAttributes) > 1 {
serviceResponse.Success.Attributes.UserAttributes = &xmlUserAttributes{
Attributes: userAttributes,
}
}
case CasThreeZeroAnyAttributesStyle:
var userAttributes []*xmlAnyAttribute
for key, values := range t.Attributes {
for _, value := range values {
userAttributes = append(userAttributes, &xmlAnyAttribute{
XMLName: xml.Name{Local: key},
Value: value,
})
}
}
serviceResponse.Success.Attributes = &xmlAttributes{
AuthenticationDate: time.Now().UTC(),
LongTermAuthenticationRequestTokenUsed: false,
IsFromNewLogin: true,
}
if len(userAttributes) > 1 {
serviceResponse.Success.Attributes.UserAttributes = &xmlUserAttributes{
AnyAttributes: userAttributes,
}
}
case RubyCasAttributesStyle:
var userAttributes []*xmlAnyAttribute
for key, values := range t.Attributes {
for _, value := range values {
userAttributes = append(userAttributes, &xmlAnyAttribute{
XMLName: xml.Name{Local: key},
Value: value,
})
}
}
if len(userAttributes) > 1 {
serviceResponse.Success.ExtraAttributes = userAttributes
}
}
} else {
serviceResponse = failureServiceResponse("INVALID_SERVICE", fmt.Sprintf("Ticket %s is not recognized for service %s", ticket, service))
}
} else {
serviceResponse = failureServiceResponse("INVALID_TICKET", fmt.Sprintf("Ticket %s not recognized", ticket))
}
e := xml.NewEncoder(w)
e.Indent("", " ")
e.Encode(serviceResponse)
return
default:
http.NotFound(w, r)
return
}
}