Compare commits
2 Commits
ad58d84396
...
1c419454a2
Author | SHA1 | Date | |
---|---|---|---|
1c419454a2 | |||
ab60f8796a |
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,16 +29,21 @@ 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)
|
||||||
result, err := db.NamedExec((`INSERT INTO users (RoleId, UserName, Password) VALUES (:RoleId, :UserName, :Password)`), u)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("SaveUser error executing sql record : '%s'\n", err)
|
log.Printf("SaveUser Username already exists : '%v'\n", err)
|
||||||
return &User{}, err
|
|
||||||
} else {
|
} else {
|
||||||
affected, _ := result.RowsAffected()
|
log.Printf("SaveUser confirmed no existing user, continuing with creation of user '%s'\n", u.UserName)
|
||||||
id, _ := result.LastInsertId()
|
result, err := db.NamedExec((`INSERT INTO users (RoleId, UserName, Password) VALUES (:RoleId, :UserName, :Password)`), u)
|
||||||
log.Printf("SaveUser insert returned result id '%d' affecting %d row(s).\n", id, affected)
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("SaveUser error executing sql record : '%s'\n", err)
|
||||||
|
return &User{}, err
|
||||||
|
} else {
|
||||||
|
affected, _ := result.RowsAffected()
|
||||||
|
id, _ := result.LastInsertId()
|
||||||
|
log.Printf("SaveUser insert returned result id '%d' affecting %d row(s).\n", id, affected)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return u, nil
|
return u, nil
|
||||||
|
@@ -24,6 +24,7 @@ func GenerateToken(user_id uint) (string, error) {
|
|||||||
claims["authorized"] = true
|
claims["authorized"] = true
|
||||||
claims["user_id"] = user_id
|
claims["user_id"] = user_id
|
||||||
claims["exp"] = time.Now().Add(time.Hour * time.Duration(token_lifespan)).Unix()
|
claims["exp"] = time.Now().Add(time.Hour * time.Duration(token_lifespan)).Unix()
|
||||||
|
// https://pkg.go.dev/github.com/golang-jwt/jwt/v5#New
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||||
|
|
||||||
return token.SignedString([]byte(os.Getenv("API_SECRET")))
|
return token.SignedString([]byte(os.Getenv("API_SECRET")))
|
||||||
@@ -36,8 +37,8 @@ func TokenValid(c *gin.Context) error {
|
|||||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||||
}
|
}
|
||||||
//return []byte(os.Getenv("API_SECRET")), nil
|
// This code says signature is invalid if we return an empty []byte but I don't know why
|
||||||
return []byte(""), nil
|
return []byte(os.Getenv("API_SECRET")), nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -64,9 +65,8 @@ func ExtractTokenID(c *gin.Context) (uint, error) {
|
|||||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||||
}
|
}
|
||||||
// Why return the secret??
|
// Why return the secret?? Code doesn't work if we don't return the secret
|
||||||
//return []byte(os.Getenv("API_SECRET")), nil
|
return []byte(os.Getenv("API_SECRET")), nil
|
||||||
return 0, nil
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
Reference in New Issue
Block a user