-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from Firdavs9512/feat/http-server-user-auth-se…
…cure Feat/http server user auth secure
- Loading branch information
Showing
9 changed files
with
108 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package middleware | ||
|
||
import ( | ||
"github.com/Firdavs9512/qk-server/app/models" | ||
"github.com/Firdavs9512/qk-server/config" | ||
"github.com/kataras/iris/v12" | ||
) | ||
|
||
type RequestHeader struct { | ||
Authorization string `header:"Authorization,required"` | ||
} | ||
|
||
func UserAuthMiddleware() iris.Handler { | ||
return func(ctx iris.Context) { | ||
var requestHeader RequestHeader | ||
if err := ctx.ReadHeaders(&requestHeader); err != nil { | ||
ctx.StatusCode(iris.StatusBadRequest) | ||
ctx.JSON(iris.Map{"message": "Invalid request"}) | ||
return | ||
} | ||
|
||
if requestHeader.Authorization == "" { | ||
ctx.StatusCode(iris.StatusUnauthorized) | ||
ctx.JSON(iris.Map{"message": "Unauthorized"}) | ||
return | ||
} | ||
|
||
var token models.AuthToken | ||
if err := config.Database.DB.Where("token = ?", requestHeader.Authorization).First(&token).Error; err != nil { | ||
ctx.StatusCode(iris.StatusUnauthorized) | ||
ctx.JSON(iris.Map{"message": "Unauthorized"}) | ||
return | ||
} | ||
|
||
ctx.Next() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package models | ||
|
||
import "gorm.io/gorm" | ||
|
||
type AuthToken struct { | ||
gorm.Model | ||
Name string | ||
Token string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package core | ||
|
||
import ( | ||
"github.com/Firdavs9512/qk-server/app/models" | ||
"github.com/Firdavs9512/qk-server/config" | ||
) | ||
|
||
func Migrate() { | ||
config.Database.DB.AutoMigrate( | ||
&models.AuthToken{}, | ||
&models.Settings{}, | ||
&models.Files{}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package utils | ||
|
||
import "math/rand" | ||
|
||
func RandomString(length int) string { | ||
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" | ||
b := make([]byte, length) | ||
for i := range b { | ||
b[i] = charset[rand.Intn(len(charset))] | ||
} | ||
return string(b) | ||
} |