forked from go-cas/cas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_helpers.go
175 lines (147 loc) · 4.89 KB
/
http_helpers.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
170
171
172
173
174
175
package cas
import (
"context"
"net/http"
"time"
)
type key int
const ( // emulating enums is actually pretty ugly in go.
clientKey key = iota
authenticationResponseKey
firstAuthenticatedRequestKey
)
// setClient associates a Client with a http.Request.
func setClient(r *http.Request, c *Client) {
ctx := context.WithValue(r.Context(), clientKey, c)
r2 := r.WithContext(ctx)
*r = *r2
}
// getClient retrieves the Client associated with the http.Request.
func getClient(r *http.Request) *Client {
if c := r.Context().Value(clientKey); c != nil {
return c.(*Client)
} else {
return nil // explicitly pass along the nil to caller -- conforms to previous impl
}
}
// RedirectToLogin allows CAS protected handlers to redirect a request
// to the CAS login page.
func RedirectToLogin(w http.ResponseWriter, r *http.Request) {
c := getClient(r)
if c == nil {
err := "cas: redirect to cas failed as no client associated with request"
http.Error(w, err, http.StatusInternalServerError)
return
}
c.RedirectToLogin(w, r)
}
// RedirectToLogout allows CAS protected handlers to redirect a request
// to the CAS logout page.
func RedirectToLogout(w http.ResponseWriter, r *http.Request) {
c := getClient(r)
if c == nil {
err := "cas: redirect to cas failed as no client associated with request"
http.Error(w, err, http.StatusInternalServerError)
return
}
c.RedirectToLogout(w, r)
}
// setAuthenticationResponse associates an AuthenticationResponse with
// a http.Request.
func setAuthenticationResponse(r *http.Request, a *AuthenticationResponse) {
ctx := context.WithValue(r.Context(), authenticationResponseKey, a)
r2 := r.WithContext(ctx)
*r = *r2
}
// getAuthenticationResponse retrieves the AuthenticationResponse associated
// with a http.Request.
func getAuthenticationResponse(r *http.Request) *AuthenticationResponse {
if a := r.Context().Value(authenticationResponseKey); a != nil {
return a.(*AuthenticationResponse)
} else {
return nil // explicitly pass along the nil to caller -- conforms to previous impl
}
}
// IsAuthenticated indicates whether the request has been authenticated with CAS.
func IsAuthenticated(r *http.Request) bool {
if a := getAuthenticationResponse(r); a != nil {
return true
}
return false
}
// Username returns the authenticated users username
func Username(r *http.Request) string {
if a := getAuthenticationResponse(r); a != nil {
return a.User
}
return ""
}
// Attributes returns the authenticated users attributes.
func Attributes(r *http.Request) UserAttributes {
if a := getAuthenticationResponse(r); a != nil {
return a.Attributes
}
return nil
}
// AuthenticationDate returns the date and time that authentication was performed.
//
// This may return time.IsZero if Authentication Date information is not included
// in the CAS service validation response. This will be the case for CAS 2.0
// protocol servers.
func AuthenticationDate(r *http.Request) time.Time {
var t time.Time
if a := getAuthenticationResponse(r); a != nil {
t = a.AuthenticationDate
}
return t
}
// IsNewLogin indicates whether the CAS service ticket was granted following a
// new authentication.
//
// This may incorrectly return false if Is New Login information is not included
// in the CAS service validation response. This will be the case for CAS 2.0
// protocol servers.
func IsNewLogin(r *http.Request) bool {
if a := getAuthenticationResponse(r); a != nil {
return a.IsNewLogin
}
return false
}
// IsRememberedLogin indicates whether the CAS service ticket was granted by the
// presence of a long term authentication token.
//
// This may incorrectly return false if Remembered Login information is not included
// in the CAS service validation response. This will be the case for CAS 2.0
// protocol servers.
func IsRememberedLogin(r *http.Request) bool {
if a := getAuthenticationResponse(r); a != nil {
return a.IsRememberedLogin
}
return false
}
// MemberOf returns the list of groups which the user belongs to.
func MemberOf(r *http.Request) []string {
if a := getAuthenticationResponse(r); a != nil {
return a.MemberOf
}
return nil
}
// markAsFirstAuthenticatedRequest is used to mark the request as the first authenticated request within a session
func setFirstAuthenticatedRequest(r *http.Request, firstAuthenticatedRequest bool) {
ctx := context.WithValue(r.Context(), firstAuthenticatedRequestKey, firstAuthenticatedRequest)
r2 := r.WithContext(ctx)
*r = *r2
}
// getFirstAuthenticatedRequest returns true if the request was marked as first successful authenticated request
// in this session.
func getFirstAuthenticatedRequest(r *http.Request) bool {
if a := r.Context().Value(firstAuthenticatedRequestKey); a != nil {
return a.(bool)
} else {
return false
}
}
// IsFirstAuthenticatedRequest returns true, for the first successful authenticated request of a session
func IsFirstAuthenticatedRequest(r *http.Request) bool {
return getFirstAuthenticatedRequest(r)
}