-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauthorizer.go
More file actions
330 lines (264 loc) · 9.12 KB
/
authorizer.go
File metadata and controls
330 lines (264 loc) · 9.12 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package main
import (
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"flag"
"github.com/gocql/gocql"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
)
// Struct to represent configuration file params
type configFile struct {
Port string
Serverslist string
Keyspace string
}
// Simple structure to represent login and add user response
type newUserRequest struct {
Username string
Password string
}
// Function read configuration and set return configFile type
func readConfig(confFilePath string) (configFile, error) {
var config configFile
confFile, err := ioutil.ReadFile(confFilePath)
if err != nil {
return config, err
}
json.Unmarshal(confFile, &config)
return config, nil
}
// Function for creating datastructures if they're not exist
func createDatastructure(session *gocql.Session, keyspace string) error {
err := session.Query("CREATE KEYSPACE IF NOT EXISTS " + keyspace +
" WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }").Exec()
if err != nil {
return err
}
err = session.Query("CREATE TABLE IF NOT EXISTS " + keyspace + ".users (" +
"username varchar," +
"password varchar," +
"PRIMARY KEY(username))").Exec()
if err != nil {
return err
}
err = session.Query("CREATE TABLE IF NOT EXISTS " + keyspace + ".sessions (" +
"session_id varchar PRIMARY KEY," +
"username varchar)").Exec()
return err
}
// Generation random session ID and verifiyng that it is unique
func generateSessionId(session *gocql.Session) (string, error) {
var session_id string
count := 2
size := 32
rb := make([]byte, size)
// generating session_id while it will be uniq(actually in most cases it will be uniq in a first time)
for count != 0 {
rand.Read(rb)
session_id = base64.URLEncoding.EncodeToString(rb)
err := session.Query("SELECT COUNT(*) from sessions where session_id = '" + session_id + "'").Scan(&count)
if err != nil {
return session_id, err
}
}
return session_id, nil
}
// Router for /user/ functions. Routing based on request method, i.e. GET, POST, PUT, DELETE.
// Currently only for POST, but it made expandable :-)
func userHandler(w http.ResponseWriter, r *http.Request, session *gocql.Session) {
body, _ := ioutil.ReadAll(r.Body)
switch {
case r.Method == "POST":
error_code, err := createUser(&body, session)
if err != nil {
log.Println("Error on creating user: ", err, "\nClient: ", r.RemoteAddr, " Request: ", string(body))
}
http.Error(w, http.StatusText(error_code), error_code)
default:
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}
}
// Router for /session/ functions. Routing based on request method, i.e. GET, POST, PUT, DELETE.
func sessionHandler(w http.ResponseWriter, r *http.Request, session *gocql.Session) {
body, _ := ioutil.ReadAll(r.Body)
switch {
case r.Method == "POST":
session_id, error_code, err := createSession(&body, session)
if err != nil {
log.Println("Error on creating session: ", err, "\nClient: ", r.RemoteAddr, " Request: ", string(body))
}
// Set expire for a one year, same as in sessions table
if session_id != "" {
expire := time.Now().AddDate(1, 0, 0)
authCookie := &http.Cookie{
Name: "session_id",
Expires: expire,
Value: session_id,
}
http.SetCookie(w, authCookie)
}
http.Error(w, http.StatusText(error_code), error_code)
case r.Method == "GET":
session_id, _ := r.Cookie("session_id")
error_code, err := checkSession(session, session_id.Value)
if err != nil {
log.Println("Error on checking authorization: ", err)
}
http.Error(w, http.StatusText(error_code), error_code)
case r.Method == "DELETE":
session_id, _ := r.Cookie("session_id")
error_code, err := deleteSession(session, session_id.Value)
if err != nil {
log.Println("Error on checking authorization: ", err)
}
// Rewrite session_id cookie with empty sting and set expiration now
expire := time.Now()
authCookie := &http.Cookie{
Name: "session_id",
Expires: expire,
Value: "",
}
http.SetCookie(w, authCookie)
http.Error(w, http.StatusText(error_code), error_code)
default:
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}
}
// Function handle new use creating
func createUser(body *[]byte, session *gocql.Session) (int, error) {
var request newUserRequest
var count int
err := json.Unmarshal(*body, &request)
if err != nil {
return http.StatusBadRequest, err
}
// Here should be call of function to extended validation, but nothing was in requirements
if request.Password == "" || request.Username == "" {
return http.StatusBadRequest, errors.New("User or password is empty")
}
// Check if such user already existing
err = session.Query("SELECT COUNT(*) from users where username = '" + request.Username + "'").Scan(&count)
if err != nil {
return http.StatusInternalServerError, err
}
if count > 0 {
return http.StatusConflict, errors.New("Such user alredy existing")
}
// Prepare password hash to write it to DB
hash := sha256.New()
hash.Write([]byte(request.Password))
err = session.Query("INSERT INTO users (username,password) VALUES (?,?)", request.Username, hex.EncodeToString(hash.Sum(nil))).Exec()
if err != nil {
return http.StatusInternalServerError, err
}
return http.StatusCreated, nil
}
// Function handling creating new session
func createSession(body *[]byte, session *gocql.Session) (string, int, error) {
var request newUserRequest
var session_id string
var count int
err := json.Unmarshal(*body, &request)
if err != nil {
return session_id, http.StatusBadRequest, err
}
// Here should be call of function to extended validation, but nothing was in requirements
if request.Password == "" || request.Username == "" {
return session_id, http.StatusBadRequest, errors.New("User or password is empty")
}
// Prepare password hash to make request to DB
hash := sha256.New()
hash.Write([]byte(request.Password))
// Check if user and password is valid
err = session.Query("SELECT COUNT(*) from users where username = '" + request.Username + "' and password ='" + hex.EncodeToString(hash.Sum(nil)) + "'").Scan(&count)
if err != nil {
return session_id, http.StatusInternalServerError, err
}
if count == 0 {
return session_id, http.StatusUnauthorized, errors.New("User name or password is not correct")
}
// prepare session ID for a new session
session_id, err = generateSessionId(session)
if err != nil {
return session_id, http.StatusInternalServerError, err
}
// set TTL to a one year to expire in same time with cookie
err = session.Query("INSERT INTO sessions (session_id,username) VALUES (?,?) USING TTL 31536000", session_id, request.Username).Exec()
if err != nil {
return session_id, http.StatusInternalServerError, err
}
return session_id, http.StatusCreated, nil
}
// Function checking if provided cookie matching to active sessions
func checkSession(session *gocql.Session, session_id string) (int, error) {
var count int
// fast path to don't use DB when session_id cookie not indicated at all
if session_id == "" {
return http.StatusUnauthorized, nil
}
// Check if such session exist
err := session.Query("SELECT COUNT(*) from sessions where session_id = '" + session_id + "'").Scan(&count)
if err != nil {
return http.StatusInternalServerError, err
}
if count == 0 {
return http.StatusUnauthorized, nil
} else {
return http.StatusOK, nil
}
}
func deleteSession(session *gocql.Session, session_id string) (int, error) {
// fast path to don't use DB when session_id cookie not indicated at all
if session_id == "" {
return http.StatusUnauthorized, nil
}
// removing session for DB
err := session.Query("DELETE FROM sessions WHERE session_id = '" + session_id + "'").Exec()
if err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}
func main() {
confFilePath := flag.String("conf", "config.json", "path to application config")
flag.Parse()
config, err := readConfig(*confFilePath)
if err != nil {
log.Fatal("Couldn't read config file ", err)
}
// Initialize Cassandra cluster
cluster := gocql.NewCluster(strings.Split(config.Serverslist, ",")...)
// Establish connection to Cassandra
session, err := cluster.CreateSession()
if err != nil {
log.Fatal(err)
}
// Creating necessary datastructures
err = createDatastructure(session, config.Keyspace)
if err != nil {
log.Fatal("Get an error while creating datastructures: ", err)
}
// Close old session and open newly to created(or already existed) keyspace
// This is a limitation of gocql library https://github.com/gocql/gocql#important-default-keyspace-changes
session.Close()
cluster.Keyspace = config.Keyspace
session, _ = cluster.CreateSession()
defer session.Close()
// If someone ask root, reply 404
http.HandleFunc("/", http.NotFound)
http.HandleFunc("/user/", func(w http.ResponseWriter, r *http.Request) { userHandler(w, r, session) })
http.HandleFunc("/session/", func(w http.ResponseWriter, r *http.Request) { sessionHandler(w, r, session) })
err = http.ListenAndServe(":"+config.Port, nil)
if err != nil {
log.Fatal("Error on creating listener: ", err)
}
log.Println("Authorizer closed.")
}