-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpages_handlers.go
251 lines (221 loc) · 6.71 KB
/
pages_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
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
245
246
247
248
249
250
251
package main
import (
"context"
"fmt"
"html/template"
"net/http"
"strconv"
"time"
"github.com/gorilla/mux"
resp "github.com/vano2903/ipaas/responser"
)
// homepage handler
func (h Handler) HomePageHandler(w http.ResponseWriter, r *http.Request) {
//return the home html page
http.ServeFile(w, r, "./pages/home.html")
}
func (h Handler) LoginPageHandler(w http.ResponseWriter, r *http.Request) {
//return the login html page
http.ServeFile(w, r, "./pages/login.html")
}
func (h Handler) MockRegisterUserPageHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./pages/mockRegistration.html")
}
func (h Handler) UserPageHandler(w http.ResponseWriter, r *http.Request) {
db, err := connectToDB()
if err != nil {
resp.Error(w, http.StatusInternalServerError, err.Error())
return
}
defer db.Client().Disconnect(context.TODO())
//get the access token from the cookie
cookie, err := r.Cookie("ipaas-access-token")
if err != nil {
if err == http.ErrNoCookie {
//new token pair
cookie, err := r.Cookie("ipaas-refresh-token")
if err != nil {
if err == http.ErrNoCookie {
//resp.Error(w, http.StatusBadRequest, "No refresh token")
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
resp.Error(w, http.StatusInternalServerError, err.Error())
return
}
refreshToken := cookie.Value
//check if there is a refresh token
if refreshToken == "" {
resp.Error(w, 498, "No refresh token, do login again")
return
}
//check if the refresh token is expired
isExpired, err := IsRefreshTokenExpired(refreshToken, db)
if err != nil {
resp.Errorf(w, http.StatusInternalServerError, "Error checking if refresh token is expired: %s", err.Error())
}
if isExpired {
//!should redirect to the oauth page
resp.Error(w, 498, "Refresh token is expired")
return
}
//generate a new token pair
accessToken, newRefreshToken, err := GenerateNewTokenPairFromRefreshToken(refreshToken, db)
if err != nil {
resp.Error(w, http.StatusInternalServerError, err.Error())
return
}
//delete the old tokens from the cookies
http.SetCookie(w, &http.Cookie{
Name: "ipaas-access-token",
Path: "/",
Value: "",
Expires: time.Unix(0, 0),
})
http.SetCookie(w, &http.Cookie{
Name: "ipaas-refresh-token",
Path: "/",
Value: "",
Expires: time.Unix(0, 0),
})
//set the new tokens
//!should set domain and path
http.SetCookie(w, &http.Cookie{
Name: "ipaas-access-token",
Path: "/",
Value: accessToken,
Expires: time.Now().Add(time.Hour),
})
http.SetCookie(w, &http.Cookie{
Name: "ipaas-refresh-token",
Path: "/",
Value: newRefreshToken,
Expires: time.Now().Add(time.Hour * 24 * 7),
})
//make the user refresh the page
http.Redirect(w, r, "/user/", http.StatusFound)
return
}
resp.Error(w, http.StatusInternalServerError, err.Error())
return
}
accessToken := cookie.Value
//get the student generic infos from the access token
student, err := GetUserFromAccessToken(accessToken, db)
fmt.Println("studente:", student)
if err != nil {
resp.Error(w, http.StatusInternalServerError, err.Error())
return
}
//load page with the student info
//template
t, err := template.ParseFiles("./pages/user.html")
if err != nil {
resp.Error(w, http.StatusInternalServerError, err.Error())
return
}
// var rows *sql.Rows
// rows, err = db.Query("SELECT containerID, type, name, description, isPublic FROM applications WHERE studentID = ?", student.ID)
// if err != nil {
// resp.Errorf(w, http.StatusInternalServerError, "error getting the applications: %v", err.Error())
// return
// }
// //parse the rows into a []Application and return it
// var applications []Application
// var databases []Application
// for rows.Next() {
// var app Application
// err = rows.Scan(&app.ContainerID, &app.Type, &app.Name, &app.Description, &app.IsPublic)
// if err != nil {
// resp.Errorf(w, http.StatusInternalServerError, "error getting the applications: %v", err.Error())
// return
// }
// if app.Type == "database" {
// databases = append(databases, app)
// } else {
// applications = append(applications, app)
// }
// }
// var appHTMLString string
// var dbHTMlstring string
// //parse the applications into html
// for _, app := range applications {
// var btn string
// if app.IsPublic {
// btn = `<button type="button" onclick="makePrivate('%s') class="btn btn-warning">Make private</button>`
// } else {
// btn = `<button type="button" onclick="makePublic('%s') class="btn btn-info">Make public</button>`
// }
// appHTMLString += fmt.Sprintf(`
// <div id="%s" class=\"doc\">
// <p>%s</p>
// %s
// <button type="button" onclick="deleteContainer('%s')" class="btn btn-danger">Delete</button>
// <hr>
// <h5>%s</h5>
// </div`, app.ContainerID, app.Name, btn, app.ContainerID, app.ContainerID, app.Description)
// }
// for _, database := range databases {
// dbHTMlstring += fmt.Sprintf(`
// <div id="%s" class=\"doc\">
// <p>%s</p>
// <button type="button" class="btn btn-info" disabled>Esporta</button>
// <button type="button" onclick="deleteContainer('%s')" class="btn btn-danger">Delete</button>
// </div`, database.ContainerID, database.Name, database.ContainerID)
// }
toParse := struct {
Name string
Pfp string
// Apps string
// DBs string
}{
Name: student.Name + " " + student.LastName,
Pfp: student.Pfp,
// Apps: appHTMLString,
// DBs: dbHTMlstring,
}
t.Execute(w, toParse)
}
func (h Handler) NewAppPageHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./pages/newApp.html")
}
func (h Handler) NewDatabasePageHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./pages/newDB.html")
}
func (h Handler) PublicStudentPageHandler(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["studentID"]
db, err := connectToDB()
if err != nil {
resp.Error(w, http.StatusInternalServerError, err.Error())
return
}
//get the student generic infos from the access token
idI, err := strconv.Atoi(id)
if err != nil {
resp.Error(w, http.StatusBadRequest, "invalid student id")
return
}
student, err := GetStudentFromID(idI, db)
if err != nil {
resp.Error(w, http.StatusInternalServerError, err.Error())
return
}
//load page with the student info
//template
t, err := template.ParseFiles("./pages/student.html")
if err != nil {
resp.Error(w, http.StatusInternalServerError, err.Error())
return
}
toParse := struct {
StudentID string
Name string
LastName string
}{
StudentID: id,
Name: student.Name,
LastName: student.LastName,
}
fmt.Println(toParse)
t.Execute(w, toParse)
}