This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,6 +1,8 @@
|
|||||||
api\ tests.txt
|
api\ tests.txt
|
||||||
ccsecrets
|
ccsecrets
|
||||||
ccsecrets.*
|
ccsecrets.*
|
||||||
|
smt
|
||||||
|
smt.*
|
||||||
.env
|
.env
|
||||||
*.pem
|
*.pem
|
||||||
.DS_Store
|
.DS_Store
|
@@ -14,6 +14,7 @@ func JwtAuthMiddleware() gin.HandlerFunc {
|
|||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
err := token.TokenValid(c)
|
err := token.TokenValid(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf("JwtAuthMiddleware token is not valid : '%s'\n", err)
|
||||||
c.String(http.StatusUnauthorized, "Unauthorized")
|
c.String(http.StatusUnauthorized, "Unauthorized")
|
||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
@@ -25,10 +26,9 @@ func JwtAuthMiddleware() gin.HandlerFunc {
|
|||||||
func JwtAuthAdminMiddleware() gin.HandlerFunc {
|
func JwtAuthAdminMiddleware() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
|
|
||||||
// TODO - also verify user role of admin
|
|
||||||
|
|
||||||
err := token.TokenValid(c)
|
err := token.TokenValid(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf("JwtAuthAdminMiddleware token is not valid : '%s'\n", err)
|
||||||
c.String(http.StatusUnauthorized, "Unauthorized")
|
c.String(http.StatusUnauthorized, "Unauthorized")
|
||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
@@ -38,6 +38,7 @@ func JwtAuthAdminMiddleware() gin.HandlerFunc {
|
|||||||
user_id, err := token.ExtractTokenID(c)
|
user_id, err := token.ExtractTokenID(c)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf("JwtAuthAdminMiddleware could not extract user ID from context : '%s'\n", err)
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
@@ -52,6 +53,7 @@ func JwtAuthAdminMiddleware() gin.HandlerFunc {
|
|||||||
}
|
}
|
||||||
log.Printf("JwtAuthAdminMiddleware retrieved UserRole object '%v'\n", ur)
|
log.Printf("JwtAuthAdminMiddleware retrieved UserRole object '%v'\n", ur)
|
||||||
|
|
||||||
|
// Verify that the user has a role with the admin flag set
|
||||||
if !ur.Admin {
|
if !ur.Admin {
|
||||||
c.String(http.StatusUnauthorized, "User role is Non-Admin")
|
c.String(http.StatusUnauthorized, "User role is Non-Admin")
|
||||||
c.Abort()
|
c.Abort()
|
||||||
|
@@ -29,7 +29,11 @@ func (u *User) SaveUser() (*User, error) {
|
|||||||
var err error
|
var err error
|
||||||
|
|
||||||
// TODO - validate username not already in use
|
// TODO - validate username not already in use
|
||||||
|
_, err = GetUserByName(u.UserName)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("SaveUser Username already exists : '%v'\n", err)
|
||||||
|
} else {
|
||||||
|
log.Printf("SaveUser confirmed no existing user, continuing with creation of user '%s'\n", u.UserName)
|
||||||
result, err := db.NamedExec((`INSERT INTO users (RoleId, UserName, Password) VALUES (:RoleId, :UserName, :Password)`), u)
|
result, err := db.NamedExec((`INSERT INTO users (RoleId, UserName, Password) VALUES (:RoleId, :UserName, :Password)`), u)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -40,6 +44,7 @@ func (u *User) SaveUser() (*User, error) {
|
|||||||
id, _ := result.LastInsertId()
|
id, _ := result.LastInsertId()
|
||||||
log.Printf("SaveUser insert returned result id '%d' affecting %d row(s).\n", id, affected)
|
log.Printf("SaveUser insert returned result id '%d' affecting %d row(s).\n", id, affected)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return u, nil
|
return u, nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user