forked from rancher/auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route_handlers.go
201 lines (166 loc) · 4.87 KB
/
route_handlers.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package tokens
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"time"
"github.com/rancher/auth/util"
"github.com/rancher/types/apis/management.cattle.io/v3"
)
//login is a handler for route /tokens?action=login and returns the jwt token after authenticating the user
func (server *tokenAPIServer) login(w http.ResponseWriter, r *http.Request) {
bytes, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Errorf("login failed with error: %v", err)
util.ReturnHTTPError(w, r, http.StatusBadRequest, fmt.Sprintf("Error reading input json data: %v", err))
return
}
jsonInput := v3.LoginInput{}
err = json.Unmarshal(bytes, &jsonInput)
if err != nil {
log.Errorf("unmarshal failed with error: %v", err)
util.ReturnHTTPError(w, r, http.StatusBadRequest, fmt.Sprintf("Error unmarshaling input json data: %v", err))
return
}
var token v3.Token
var status int
token, status, err = server.createLoginToken(jsonInput)
if err != nil {
log.Errorf("Login failed with error: %v", err)
if status == 0 {
status = http.StatusInternalServerError
}
util.ReturnHTTPError(w, r, status, fmt.Sprintf("%v", err))
return
}
isSecure := false
if r.URL.Scheme == "https" {
isSecure = true
}
if jsonInput.ResponseType == "cookie" {
tokenCookie := &http.Cookie{
Name: "rAuthnSessionToken",
Value: token.TokenID,
Secure: isSecure,
Path: "/",
HttpOnly: true,
}
http.SetCookie(w, tokenCookie)
} else {
enc := json.NewEncoder(w)
enc.SetEscapeHTML(false)
enc.Encode(token)
}
}
func (server *tokenAPIServer) deriveToken(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("rAuthnSessionToken")
if err != nil {
log.Info("Failed to get token cookie: %v", err)
util.ReturnHTTPError(w, r, http.StatusUnauthorized, "No valid token cookie")
return
}
log.Infof("token cookie: %v %v", cookie.Name, cookie.Value)
bytes, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Errorf("GetToken failed with error: %v", err)
}
jsonInput := v3.Token{}
err = json.Unmarshal(bytes, &jsonInput)
if err != nil {
log.Errorf("unmarshal failed with error: %v", err)
}
var token v3.Token
var status int
// create derived token
token, status, err = server.createDerivedToken(jsonInput, cookie.Value)
if err != nil {
log.Errorf("deriveToken failed with error: %v", err)
if status == 0 {
status = http.StatusInternalServerError
}
util.ReturnHTTPError(w, r, status, fmt.Sprintf("%v", err))
return
}
enc := json.NewEncoder(w)
enc.SetEscapeHTML(false)
enc.Encode(token)
}
func (server *tokenAPIServer) listTokens(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("rAuthnSessionToken")
if err != nil {
log.Info("Failed to get token cookie: %v", err)
util.ReturnHTTPError(w, r, http.StatusUnauthorized, "Invalid token cookie")
return
}
log.Infof("token cookie: %v %v", cookie.Name, cookie.Value)
//getToken
tokens, status, err := server.getTokens(cookie.Value)
if err != nil {
log.Errorf("GetToken failed with error: %v", err)
if status == 0 {
status = http.StatusInternalServerError
}
util.ReturnHTTPError(w, r, status, fmt.Sprintf("%v", err))
return
}
enc := json.NewEncoder(w)
enc.SetEscapeHTML(false)
enc.Encode(tokens)
}
func (server *tokenAPIServer) logout(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("rAuthnSessionToken")
if err != nil {
log.Info("Failed to get token cookie: %v", err)
util.ReturnHTTPError(w, r, http.StatusUnauthorized, "Invalid token cookie")
return
}
log.Infof("token cookie: %v %v", cookie.Name, cookie.Value)
isSecure := false
if r.URL.Scheme == "https" {
isSecure = true
}
tokenCookie := &http.Cookie{
Name: "rAuthnSessionToken",
Value: "",
Secure: isSecure,
Path: "/",
HttpOnly: true,
MaxAge: -1,
Expires: time.Date(1982, time.February, 10, 23, 0, 0, 0, time.UTC),
}
http.SetCookie(w, tokenCookie)
//getToken
status, err := server.deleteToken(cookie.Value)
if err != nil {
log.Errorf("DeleteToken failed with error: %v", err)
if status == 0 {
status = http.StatusInternalServerError
}
util.ReturnHTTPError(w, r, status, fmt.Sprintf("%v", err))
return
}
}
func (server *tokenAPIServer) listIdentities(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("rAuthnSessionToken")
if err != nil {
log.Info("Failed to get token cookie: %v", err)
util.ReturnHTTPError(w, r, http.StatusUnauthorized, "Invalid token cookie")
return
}
log.Infof("token cookie: %v %v", cookie.Name, cookie.Value)
//getToken
identities, status, err := server.getIdentities(cookie.Value)
if err != nil {
log.Errorf("DeleteToken failed with error: %v", err)
if status == 0 {
status = http.StatusInternalServerError
}
util.ReturnHTTPError(w, r, status, fmt.Sprintf("%v", err))
return
}
enc := json.NewEncoder(w)
enc.SetEscapeHTML(false)
enc.Encode(identities)
}