-
Notifications
You must be signed in to change notification settings - Fork 11
/
views.go
244 lines (214 loc) · 6.62 KB
/
views.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"database/sql"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/labstack/echo"
)
func LoginPOST(c echo.Context) error {
data := NewTemplateGlobal()
service := ValidateService(c)
if service == nil {
data.NotAuthorized = true
return c.Render(http.StatusUnauthorized, "login", data)
}
var isRegistered bool
username := strings.ToLower(strings.TrimSpace(c.FormValue("username")))
// fakeCAS does not check password
err := DatabaseConnection.QueryRow(`
SELECT is_registered
FROM osf_osfuser
WHERE username = $1
OR EXISTS(SELECT * FROM osf_email WHERE osf_email.user_id = osf_osfuser.id AND osf_email.address = $1)
`, username).Scan(&isRegistered)
if err != nil {
if err != sql.ErrNoRows {
panic(err)
}
fmt.Println("User", username, "not found.")
data.LoginForm = true
data.NotExist = true
data.CASLogin = GetCasLoginUrl(service.String())
return c.Render(http.StatusOK, "login", data)
}
if !isRegistered {
data.NotRegistered = true
return c.Render(http.StatusOK, "login", data)
}
query := service.Query()
query.Set("ticket", username)
service.RawQuery = query.Encode()
fmt.Println("Logging in and redirecting to", service)
return c.Redirect(http.StatusFound, service.String())
}
func LoginGET(c echo.Context) error {
data := NewTemplateGlobal()
service := ValidateService(c)
if service == nil {
data.NotAuthorized = true
return c.Render(http.StatusUnauthorized, "login", data)
}
username, err := url.Parse(c.QueryParam("username"))
if err != nil {
c.Error(err)
return nil
}
verificationKey, err := url.Parse(c.QueryParam("verification_key"))
if err != nil {
c.Error(err)
return nil
}
if username.String() == "" && verificationKey.String() == "" {
data.LoginForm = true
data.CASLogin = GetCasLoginUrl(service.String())
return c.Render(http.StatusOK, "login", data)
}
var verification string
uname := strings.ToLower(strings.TrimSpace(c.FormValue("username")))
err = DatabaseConnection.QueryRow(`
SELECT verification_key
FROM osf_osfuser
WHERE username = $1
OR EXISTS(SELECT * FROM osf_email WHERE osf_email.user_id = osf_osfuser.id AND osf_email.address = $1)
`, uname).Scan(&verification)
if err != nil {
if err != sql.ErrNoRows {
panic(err)
}
fmt.Println("User", uname, "not found.")
data.NotValid = true
return c.Render(http.StatusNotFound, "login", data)
}
// fakeCAS will check verification key
if verification != c.FormValue("verification_key") {
fmt.Println("Invalid Verification Key\nExpecting: ", verification, "\nActual: ", c.FormValue("verification_key"))
data.NotValid = true
return c.Render(http.StatusNotFound, "login", data)
}
query := service.Query()
query.Set("ticket", c.FormValue("username"))
service.RawQuery = query.Encode()
fmt.Println("Logging in and redirecting to", service)
return c.Redirect(http.StatusFound, service.String())
}
func Logout(c echo.Context) error {
fmt.Println("Logging out and redirecting to", c.FormValue("service"))
return c.Redirect(http.StatusFound, c.FormValue("service"))
}
func ServiceValidate(c echo.Context) error {
result := User{}
ticket := c.FormValue("ticket")
var queryString = `
SELECT DISTINCT
osf_guid._id,
osf_osfuser.username,
osf_osfuser.given_name,
osf_osfuser.family_name
FROM osf_osfuser
RIGHT JOIN osf_guid
ON osf_guid.object_id = osf_osfuser.id
WHERE osf_guid.content_type_id = (SELECT id FROM django_content_type WHERE model = 'osfuser' LIMIT 1)
AND (
EXISTS (SELECT * FROM osf_email WHERE osf_email.user_id = osf_osfuser.id AND osf_email.address = $1)
OR (osf_osfuser.username = $1)
);
`
err := DatabaseConnection.QueryRow(queryString, ticket).Scan(&result.Id, &result.Username, &result.GivenName, &result.FamilyName)
if err != nil {
if err != sql.ErrNoRows {
panic(err)
}
fmt.Println("User", ticket, "not found.")
return c.NoContent(http.StatusNotFound)
}
fmt.Println("User found: username =", result.Username, ", guid =", result.Id)
response := ServiceResponse{
Xmlns: "http://www.yale.edu/tp/cas",
User: result.Id,
NewLogin: true,
Date: "Eh",
GivenName: result.GivenName,
FamilyName: result.FamilyName,
AccessToken: result.Id,
UserName: result.Username,
}
return c.XML(http.StatusOK, response)
}
func OAuth(c echo.Context) error {
tokenId := strings.Replace(c.Request().Header.Get("Authorization"), "Bearer ", "", 1)
// Find the user that owns the token
var result User
queryString := `
SELECT DISTINCT
osf_guid._id,
osf_osfuser.username,
osf_osfuser.given_name,
osf_osfuser.family_name
FROM osf_guid
LEFT JOIN django_content_type
ON django_content_type.model = 'osfuser'
JOIN osf_osfuser
ON django_content_type.id = osf_guid.content_type_id AND object_id = osf_osfuser.id
JOIN osf_apioauth2personaltoken
ON osf_osfuser.id = osf_apioauth2personaltoken.owner_id
WHERE osf_apioauth2personaltoken.token_id = $1 AND osf_apioauth2personaltoken.is_active
`
err := DatabaseConnection.QueryRow(queryString, tokenId).Scan(&result.Id, &result.Username, &result.GivenName, &result.FamilyName)
if err != nil {
if err != sql.ErrNoRows {
panic(err)
}
fmt.Printf("Access token %s not found\n", tokenId)
return c.NoContent(http.StatusNotFound)
}
fmt.Printf("User found for token: username = %s , guid =%s\n", result.Username, result.Id)
// Find all the scopes associated with the token
fmt.Printf("Reading scopes ... ")
queryString = `
SELECT DISTINCT osf_apioauth2scope.name
FROM osf_apioauth2personaltoken_scopes
JOIN osf_apioauth2personaltoken
on osf_apioauth2personaltoken_scopes.apioauth2personaltoken_id = osf_apioauth2personaltoken.id
JOIN osf_apioauth2scope
on osf_apioauth2personaltoken_scopes.apioauth2scope_id = osf_apioauth2scope.id
WHERE osf_apioauth2personaltoken.token_id = $1
`
rows, err := DatabaseConnection.Query(queryString, tokenId)
if err != nil {
if err != sql.ErrNoRows {
panic(err)
}
fmt.Printf("No scope is found for access token %s\n", tokenId)
return c.NoContent(http.StatusNotFound)
}
defer rows.Close()
scopes := make([]string, 0)
var scope string
for rows.Next() {
err = rows.Scan(&scope)
if err != nil {
panic(err)
}
fmt.Printf("%s, ", scope)
scopes = append(scopes, scope)
}
err = rows.Err()
if err != nil {
panic(err)
}
fmt.Printf("... %d scopes in total.\n", len(scopes))
// Return 200 with user information and scopes
return c.JSON(200, OAuthResponse{
Id: result.Id,
Attributes: OAuthAttributes{
LastName: result.FamilyName,
FirstName: result.GivenName,
},
Scope: scopes,
})
}
func OAuthRevoke(c echo.Context) error {
return c.NoContent(http.StatusNoContent)
}