@@ -14,29 +14,40 @@ import (
14
14
"io/ioutil"
15
15
"log"
16
16
"net/http"
17
- "os"
18
17
)
19
18
19
+ /*
20
+ ** contains handler fuctions which do not require admin account
21
+ ** contains fuctions to interact with OAuth2
22
+ */
23
+
20
24
var confOAuth2 * oauth2.Config
21
25
22
26
var state string
23
27
24
28
func init () {
25
29
30
+ // doesen't needs to be called explicitly
31
+ // configures OAuth2 and Mysql
32
+
26
33
var credOAuth2 CredentialOAuth2
27
34
var credMysql CredentialMysql
28
35
29
36
fileOAuth2 , err := ioutil .ReadFile ("./credOAuth2.json" )
30
37
if err != nil {
31
- log .Printf ("File error: %v\n " , err )
32
- os .Exit (1 )
38
+ log .Fatalf ("Error while reading oauth2 config file.\n Error: %v\n " , err .Error ())
33
39
}
34
40
json .Unmarshal (fileOAuth2 , & credOAuth2 )
35
41
36
42
confOAuth2 = & oauth2.Config {
43
+
44
+ // presently set through account [email protected]
37
45
ClientID : credOAuth2 .Cid ,
38
46
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
+
40
51
Scopes : []string {
41
52
"https://www.googleapis.com/auth/userinfo.email" ,
42
53
},
@@ -45,8 +56,7 @@ func init() {
45
56
46
57
fileMysql , err := ioutil .ReadFile ("./credMysql.json" )
47
58
if err != nil {
48
- log .Printf ("File error: %v\n " , err )
49
- os .Exit (1 )
59
+ log .Fatalf ("Error while reading oauth2 config file.\n Error: %v\n " , err .Error ())
50
60
}
51
61
json .Unmarshal (fileMysql , & credMysql )
52
62
@@ -57,14 +67,17 @@ func init() {
57
67
58
68
DB .db , err = gorm .Open ("mysql" , connectionString )
59
69
if err != nil {
60
- log .Fatal ("Could not open database : " , err )
70
+ log .Fatal ("Could not open database : " , err . Error () )
61
71
}
62
72
63
73
state = randState ()
64
74
65
75
}
66
76
67
77
func randState () string {
78
+
79
+ // generates random string
80
+
68
81
b := make ([]byte , 32 )
69
82
rand .Read (b )
70
83
return base64 .StdEncoding .EncodeToString (b )
@@ -79,33 +92,49 @@ func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
79
92
}
80
93
81
94
func LoginHandler (w http.ResponseWriter , r * http.Request , _ httprouter.Params ) {
95
+
96
+ // calls Google OAuth2 api endpoint on the provided configuration
97
+
82
98
http .Redirect (w , r , confOAuth2 .AuthCodeURL (state ), 302 )
83
99
}
84
100
85
101
func Options (w http.ResponseWriter , r * http.Request , _ httprouter.Params ) {
86
102
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
87
107
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\n Error: %v\n " , http .StatusBadRequest )
89
110
return
90
111
}
91
112
113
+ // call Exchange on conf to get token from Google OAuth2 api
114
+ // pass code recieved as callback url parameter
92
115
tok , err := confOAuth2 .Exchange (oauth2 .NoContext , r .FormValue ("code" ))
93
116
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.\n Error: %v\n " , err .Error ())
95
119
return
96
120
}
97
121
122
+ // authenticate the google token obtained by calling endpoint
123
+ // provided by google for token authentication
98
124
check , err := http .Get ("https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=" + tok .AccessToken )
99
125
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.\n Error: %v\n " , err .Error ())
101
128
return
102
129
}
103
130
defer check .Body .Close ()
104
131
132
+ // use token to get user info
105
133
client := confOAuth2 .Client (oauth2 .NoContext , tok )
106
134
info , err := client .Get ("https://www.googleapis.com/oauth2/v3/userinfo" )
107
135
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.\n Error: %v\n " , err .Error ())
109
138
return
110
139
}
111
140
defer info .Body .Close ()
@@ -121,23 +150,30 @@ func Options(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
121
150
return
122
151
}
123
152
153
+ // set google token as cookie
124
154
SetCookieHandler (& googTok , w )
125
155
126
- log .Println ( "Email body: " , string ( data ) )
156
+ log .Printf ( "%v logged in using account %v " , googTok . Name , googTok . Email )
127
157
128
158
http .Redirect (w , r , "/" , 302 )
129
159
130
160
}
131
161
132
162
func LogoutHandler (w http.ResponseWriter , r * http.Request , _ httprouter.Params ) {
133
163
164
+ // clears cookie logs out user
165
+
134
166
ClearCookieHandler (w )
135
167
http .Redirect (w , r , "/" , 302 )
136
168
137
169
}
138
170
139
171
func MakeAccessRequest (w http.ResponseWriter , r * http.Request , _ httprouter.Params ) {
140
172
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
141
177
type Receive struct {
142
178
SshKey string `json:"ssh_key"`
143
179
Message string `json:"message"`
@@ -149,7 +185,9 @@ func MakeAccessRequest(w http.ResponseWriter, r *http.Request, _ httprouter.Para
149
185
var receive Receive
150
186
err := decoder .Decode (& receive )
151
187
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.\n Error: %v\n " , err .Error ())
190
+ return
153
191
}
154
192
155
193
googTok := ReadCookieHandler (w , r )
@@ -163,6 +201,7 @@ func MakeAccessRequest(w http.ResponseWriter, r *http.Request, _ httprouter.Para
163
201
164
202
var accessRequest AccessRequest
165
203
204
+ // register new request if it does not already exists
166
205
notFoundErr := DB .db .Debug ().Where ("email = ?" , googTok .Email ).First (& accessRequest ).Error
167
206
if notFoundErr != nil {
168
207
accessRequest = AccessRequest {
@@ -200,6 +239,8 @@ func MakeAccessRequest(w http.ResponseWriter, r *http.Request, _ httprouter.Para
200
239
201
240
func MakeAdminRequest (w http.ResponseWriter , r * http.Request , _ httprouter.Params ) {
202
241
242
+ // make request to get admin privileges for DAMN portal
243
+
203
244
var response Response
204
245
205
246
googTok := ReadCookieHandler (w , r )
@@ -213,6 +254,7 @@ func MakeAdminRequest(w http.ResponseWriter, r *http.Request, _ httprouter.Param
213
254
214
255
var adminRequest AdminRequest
215
256
257
+ // register new request if it does not already exists
216
258
notFoundErr := DB .db .Debug ().Where ("email = ?" , googTok .Email ).First (& adminRequest ).Error
217
259
if notFoundErr != nil {
218
260
adminRequest = AdminRequest {
0 commit comments