Skip to content

Commit 87d2b5b

Browse files
committed
Documentation and refactoring
1 parent cef2393 commit 87d2b5b

File tree

7 files changed

+231
-146
lines changed

7 files changed

+231
-146
lines changed

app/adminHandler.go renamed to app/adminHandlers.go

Lines changed: 5 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import (
1010
"net/http"
1111
)
1212

13+
/*
14+
** contains handler fuctions which require admin account
15+
** contains fuctions to interact with shell scripts that handle ssh keys
16+
*/
17+
1318
func IsAdmin(w http.ResponseWriter, r *http.Request) (isAdminFlag bool) {
1419

1520
googTok := ReadCookieHandler(w, r)
@@ -326,135 +331,3 @@ func RevokeAccessPrivilege(w http.ResponseWriter, r *http.Request, ps httprouter
326331
w.Write(json)
327332

328333
}
329-
330-
func AccessesHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
331-
332-
var response Response
333-
var accesses []Access
334-
335-
if IsAdmin(w, r) == true {
336-
DB.db.Find(&accesses)
337-
json, err := json.Marshal(accesses)
338-
if err != nil {
339-
http.Error(w, err.Error(), http.StatusInternalServerError)
340-
return
341-
}
342-
response = Response{
343-
true,
344-
string(json),
345-
}
346-
} else {
347-
response = Response{
348-
false,
349-
"User not admin",
350-
}
351-
}
352-
json, err := json.Marshal(response)
353-
if err != nil {
354-
http.Error(w, err.Error(), http.StatusInternalServerError)
355-
return
356-
}
357-
358-
w.Header().Set("Content-Type", "application/json")
359-
w.Write(json)
360-
361-
}
362-
363-
func AccessRequestsHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
364-
365-
var response Response
366-
var access_requests []AccessRequest
367-
368-
if IsAdmin(w, r) == true {
369-
DB.db.Find(&access_requests)
370-
json, err := json.Marshal(access_requests)
371-
if err != nil {
372-
http.Error(w, err.Error(), http.StatusInternalServerError)
373-
return
374-
}
375-
response = Response{
376-
true,
377-
string(json),
378-
}
379-
} else {
380-
response = Response{
381-
false,
382-
"User not admin",
383-
}
384-
}
385-
json, err := json.Marshal(response)
386-
if err != nil {
387-
http.Error(w, err.Error(), http.StatusInternalServerError)
388-
return
389-
}
390-
391-
w.Header().Set("Content-Type", "application/json")
392-
w.Write(json)
393-
394-
}
395-
396-
func AdminsHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
397-
398-
var response Response
399-
var admins []Admin
400-
401-
if IsAdmin(w, r) == true {
402-
DB.db.Find(&admins)
403-
json, err := json.Marshal(admins)
404-
if err != nil {
405-
http.Error(w, err.Error(), http.StatusInternalServerError)
406-
return
407-
}
408-
response = Response{
409-
true,
410-
string(json),
411-
}
412-
} else {
413-
response = Response{
414-
false,
415-
"User not admin",
416-
}
417-
}
418-
json, err := json.Marshal(response)
419-
if err != nil {
420-
http.Error(w, err.Error(), http.StatusInternalServerError)
421-
return
422-
}
423-
424-
w.Header().Set("Content-Type", "application/json")
425-
w.Write(json)
426-
427-
}
428-
429-
func AdminRequestsHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
430-
431-
var response Response
432-
var admin_requests []AdminRequest
433-
434-
if IsAdmin(w, r) == true {
435-
DB.db.Find(&admin_requests)
436-
json, err := json.Marshal(admin_requests)
437-
if err != nil {
438-
http.Error(w, err.Error(), http.StatusInternalServerError)
439-
return
440-
}
441-
response = Response{
442-
true,
443-
string(json),
444-
}
445-
} else {
446-
response = Response{
447-
false,
448-
"User not admin",
449-
}
450-
}
451-
json, err := json.Marshal(response)
452-
if err != nil {
453-
http.Error(w, err.Error(), http.StatusInternalServerError)
454-
return
455-
}
456-
457-
w.Header().Set("Content-Type", "application/json")
458-
w.Write(json)
459-
460-
}

app/app.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import (
77
"net/http"
88
)
99

10+
/*
11+
** migrates and creates tables in data base
12+
** registers functions with routes
13+
** opens up port and serves the app
14+
*/
15+
1016
var err error
1117
var DB Database
1218

app/cookie.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package app
22

33
import (
4-
// "fmt"
54
"github.com/gorilla/securecookie"
65
"net/http"
76
"time"
87
)
98

9+
/*
10+
** contains all middleware fuctions to interact with cookies
11+
*/
12+
1013
var cookieHandler = securecookie.New(
1114
securecookie.GenerateRandomKey(64),
1215
securecookie.GenerateRandomKey(32))

app/handlers.go

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,40 @@ import (
1414
"io/ioutil"
1515
"log"
1616
"net/http"
17-
"os"
1817
)
1918

19+
/*
20+
** contains handler fuctions which do not require admin account
21+
** contains fuctions to interact with OAuth2
22+
*/
23+
2024
var confOAuth2 *oauth2.Config
2125

2226
var state string
2327

2428
func init() {
2529

30+
// doesen't needs to be called explicitly
31+
// configures OAuth2 and Mysql
32+
2633
var credOAuth2 CredentialOAuth2
2734
var credMysql CredentialMysql
2835

2936
fileOAuth2, err := ioutil.ReadFile("./credOAuth2.json")
3037
if err != nil {
31-
log.Printf("File error: %v\n", err)
32-
os.Exit(1)
38+
log.Fatalf("Error while reading oauth2 config file.\nError: %v\n", err.Error())
3339
}
3440
json.Unmarshal(fileOAuth2, &credOAuth2)
3541

3642
confOAuth2 = &oauth2.Config{
43+
44+
// presently set through account [email protected]
3745
ClientID: credOAuth2.Cid,
3846
ClientSecret: credOAuth2.Csecret,
39-
RedirectURL: "http://127.0.0.1:8080/options",
47+
48+
// change redirect on production server
49+
RedirectURL: "http://127.0.0.1:8080/options",
50+
4051
Scopes: []string{
4152
"https://www.googleapis.com/auth/userinfo.email",
4253
},
@@ -45,8 +56,7 @@ func init() {
4556

4657
fileMysql, err := ioutil.ReadFile("./credMysql.json")
4758
if err != nil {
48-
log.Printf("File error: %v\n", err)
49-
os.Exit(1)
59+
log.Fatalf("Error while reading oauth2 config file.\nError: %v\n", err.Error())
5060
}
5161
json.Unmarshal(fileMysql, &credMysql)
5262

@@ -57,14 +67,17 @@ func init() {
5767

5868
DB.db, err = gorm.Open("mysql", connectionString)
5969
if err != nil {
60-
log.Fatal("Could not open database : ", err)
70+
log.Fatal("Could not open database : ", err.Error())
6171
}
6272

6373
state = randState()
6474

6575
}
6676

6777
func randState() string {
78+
79+
// generates random string
80+
6881
b := make([]byte, 32)
6982
rand.Read(b)
7083
return base64.StdEncoding.EncodeToString(b)
@@ -79,33 +92,49 @@ func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
7992
}
8093

8194
func LoginHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
95+
96+
// calls Google OAuth2 api endpoint on the provided configuration
97+
8298
http.Redirect(w, r, confOAuth2.AuthCodeURL(state), 302)
8399
}
84100

85101
func Options(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
86102

103+
// callback after user is authenticated via OAuth2
104+
105+
// checks if the state appended to callback url is same as generated by app
106+
// thus ensures the callback is not from a man in the middle
87107
if r.FormValue("state") != state {
88-
http.Error(w, "possibly malacious/fake callback redirect", http.StatusBadRequest)
108+
http.Error(w, "Possibly malacious/fake callback redirect", http.StatusBadRequest)
109+
log.Printf("Possibly malacious/fake callback redirect. Random state does not match\nError: %v\n", http.StatusBadRequest)
89110
return
90111
}
91112

113+
// call Exchange on conf to get token from Google OAuth2 api
114+
// pass code recieved as callback url parameter
92115
tok, err := confOAuth2.Exchange(oauth2.NoContext, r.FormValue("code"))
93116
if err != nil {
94-
http.Error(w, err.Error(), http.StatusBadRequest)
117+
http.Error(w, "Could not exchange code for google token", http.StatusBadRequest)
118+
log.Printf("Could not exchange code for google token.\nError: %v\n", err.Error())
95119
return
96120
}
97121

122+
// authenticate the google token obtained by calling endpoint
123+
// provided by google for token authentication
98124
check, err := http.Get("https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=" + tok.AccessToken)
99125
if err != nil {
100-
http.Error(w, err.Error(), http.StatusBadRequest)
126+
http.Error(w, "Google token not authentic", http.StatusBadRequest)
127+
log.Printf("Google token not authentic.\nError: %v\n", err.Error())
101128
return
102129
}
103130
defer check.Body.Close()
104131

132+
// use token to get user info
105133
client := confOAuth2.Client(oauth2.NoContext, tok)
106134
info, err := client.Get("https://www.googleapis.com/oauth2/v3/userinfo")
107135
if err != nil {
108-
http.Error(w, err.Error(), http.StatusBadRequest)
136+
http.Error(w, "User info could not be obtained", http.StatusBadRequest)
137+
log.Printf("User info could not be obtained.\nError: %v\n", err.Error())
109138
return
110139
}
111140
defer info.Body.Close()
@@ -121,23 +150,30 @@ func Options(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
121150
return
122151
}
123152

153+
// set google token as cookie
124154
SetCookieHandler(&googTok, w)
125155

126-
log.Println("Email body: ", string(data))
156+
log.Printf("%v logged in using account %v", googTok.Name, googTok.Email)
127157

128158
http.Redirect(w, r, "/", 302)
129159

130160
}
131161

132162
func LogoutHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
133163

164+
// clears cookie logs out user
165+
134166
ClearCookieHandler(w)
135167
http.Redirect(w, r, "/", 302)
136168

137169
}
138170

139171
func MakeAccessRequest(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
140172

173+
// make request to get access to server user wants to work on
174+
175+
// request contains user's ssh key and message describing the project
176+
// they are working on
141177
type Receive struct {
142178
SshKey string `json:"ssh_key"`
143179
Message string `json:"message"`
@@ -149,7 +185,9 @@ func MakeAccessRequest(w http.ResponseWriter, r *http.Request, _ httprouter.Para
149185
var receive Receive
150186
err := decoder.Decode(&receive)
151187
if err != nil {
152-
panic(err)
188+
http.Error(w, "Error reading the received request", http.StatusBadRequest)
189+
log.Printf("Error reading the received request.\nError: %v\n", err.Error())
190+
return
153191
}
154192

155193
googTok := ReadCookieHandler(w, r)
@@ -163,6 +201,7 @@ func MakeAccessRequest(w http.ResponseWriter, r *http.Request, _ httprouter.Para
163201

164202
var accessRequest AccessRequest
165203

204+
// register new request if it does not already exists
166205
notFoundErr := DB.db.Debug().Where("email = ?", googTok.Email).First(&accessRequest).Error
167206
if notFoundErr != nil {
168207
accessRequest = AccessRequest{
@@ -200,6 +239,8 @@ func MakeAccessRequest(w http.ResponseWriter, r *http.Request, _ httprouter.Para
200239

201240
func MakeAdminRequest(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
202241

242+
// make request to get admin privileges for DAMN portal
243+
203244
var response Response
204245

205246
googTok := ReadCookieHandler(w, r)
@@ -213,6 +254,7 @@ func MakeAdminRequest(w http.ResponseWriter, r *http.Request, _ httprouter.Param
213254

214255
var adminRequest AdminRequest
215256

257+
// register new request if it does not already exists
216258
notFoundErr := DB.db.Debug().Where("email = ?", googTok.Email).First(&adminRequest).Error
217259
if notFoundErr != nil {
218260
adminRequest = AdminRequest{

app/models.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package app
22

3+
/*
4+
** contains all models of the data base
5+
*/
6+
37
type AccessRequest struct {
48
AccessRequestID uint `json:"access_request_id" sql:"AUTO_INCREMENT" gorm:"primary_key"`
59
Name string `json:"name" sql:"not null"`

0 commit comments

Comments
 (0)