DB code for CIDR handling

This commit is contained in:
Joona Hoikkala
2016-12-01 00:03:08 +02:00
parent 8ffed7a3d7
commit c3ac7a211c
10 changed files with 156 additions and 56 deletions

30
api.go
View File

@ -16,28 +16,36 @@ func (a authMiddleware) Serve(ctx *iris.Context) {
username, err := getValidUsername(usernameStr)
if err == nil && validKey(password) {
au, err := DB.GetByUsername(username)
if err == nil && correctPassword(password, au.Password) {
// Password ok
if err := ctx.ReadJSON(&postData); err == nil {
// Check that the subdomain belongs to the user
if au.Subdomain == postData.Subdomain {
ctx.Next()
if err != nil {
log.WithFields(log.Fields{"error": err.Error()}).Error("Error while trying to get user")
// To protect against timed side channel (never gonna give you up)
correctPassword(password, "$2a$10$8JEFVNYYhLoBysjAxe2yBuXrkDojBQBkVpXEQgyQyjn43SvJ4vL36")
} else {
if correctPassword(password, au.Password) {
// Password ok
if err := ctx.ReadJSON(&postData); err == nil {
// Check that the subdomain belongs to the user
if au.Subdomain == postData.Subdomain {
ctx.Next()
return
}
} else {
// JSON error
ctx.JSON(iris.StatusBadRequest, iris.Map{"error": "bad data"})
return
}
} else {
ctx.JSON(iris.StatusBadRequest, iris.Map{"error": "bad data"})
return
// Wrong password
log.WithFields(log.Fields{"username": username}).Warning("Failed password check")
}
}
// To protect against timed side channel (never gonna give you up)
correctPassword(password, "$2a$10$8JEFVNYYhLoBysjAxe2yBuXrkDojBQBkVpXEQgyQyjn43SvJ4vL36")
}
ctx.JSON(iris.StatusUnauthorized, iris.Map{"error": "unauthorized"})
}
func webRegisterPost(ctx *iris.Context) {
// Create new user
nu, err := DB.Register()
nu, err := DB.Register(cidrslice{})
var regJSON iris.Map
var regStatus int
if err != nil {