mirror of
https://github.com/nicksherron/bashhub-server.git
synced 2025-10-11 05:15:45 +08:00
internal/server: store user_id in jwt token
This commit is contained in:
parent
7fbf6026a4
commit
1bcb4147fa
2 changed files with 47 additions and 24 deletions
|
@ -158,16 +158,6 @@ func (user User) userExists() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func userGetId(username string) uint {
|
|
||||||
var id uint
|
|
||||||
err := db.QueryRow("SELECT id FROM users WHERE username = $1",
|
|
||||||
username).Scan(&id)
|
|
||||||
if err != nil && err != sql.ErrNoRows {
|
|
||||||
log.Fatalf("error checking if row exists %v", err)
|
|
||||||
}
|
|
||||||
return id
|
|
||||||
}
|
|
||||||
|
|
||||||
func (user User) userGetSystemName() string {
|
func (user User) userGetSystemName() string {
|
||||||
var systemName string
|
var systemName string
|
||||||
err := db.QueryRow(`SELECT name
|
err := db.QueryRow(`SELECT name
|
||||||
|
|
|
@ -165,6 +165,7 @@ func Run() {
|
||||||
return jwt.MapClaims{
|
return jwt.MapClaims{
|
||||||
"username": v.Username,
|
"username": v.Username,
|
||||||
"systemName": v.SystemName,
|
"systemName": v.SystemName,
|
||||||
|
"user_id": v.ID,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return jwt.MapClaims{}
|
return jwt.MapClaims{}
|
||||||
|
@ -249,8 +250,13 @@ func Run() {
|
||||||
var command Command
|
var command Command
|
||||||
var user User
|
var user User
|
||||||
claims := jwt.ExtractClaims(c)
|
claims := jwt.ExtractClaims(c)
|
||||||
username := claims["username"].(string)
|
switch claims["user_id"].(type) {
|
||||||
command.User.ID = userGetId(username)
|
case float64:
|
||||||
|
command.User.ID = uint(claims["user_id"].(float64))
|
||||||
|
|
||||||
|
default:
|
||||||
|
command.User.ID = claims["user_id"].(uint)
|
||||||
|
}
|
||||||
|
|
||||||
if c.Param("path") == "search" {
|
if c.Param("path") == "search" {
|
||||||
command.Limit = 100
|
command.Limit = 100
|
||||||
|
@ -295,8 +301,14 @@ func Run() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
claims := jwt.ExtractClaims(c)
|
claims := jwt.ExtractClaims(c)
|
||||||
username := claims["username"].(string)
|
switch claims["user_id"].(type) {
|
||||||
command.User.ID = userGetId(username)
|
case float64:
|
||||||
|
command.User.ID = uint(claims["user_id"].(float64))
|
||||||
|
|
||||||
|
default:
|
||||||
|
command.User.ID = claims["user_id"].(uint)
|
||||||
|
}
|
||||||
|
|
||||||
command.SystemName = claims["systemName"].(string)
|
command.SystemName = claims["systemName"].(string)
|
||||||
command.commandInsert()
|
command.commandInsert()
|
||||||
})
|
})
|
||||||
|
@ -304,8 +316,13 @@ func Run() {
|
||||||
r.DELETE("/api/v1/command/:uuid", func(c *gin.Context) {
|
r.DELETE("/api/v1/command/:uuid", func(c *gin.Context) {
|
||||||
var command Command
|
var command Command
|
||||||
claims := jwt.ExtractClaims(c)
|
claims := jwt.ExtractClaims(c)
|
||||||
username := claims["username"].(string)
|
switch claims["user_id"].(type) {
|
||||||
command.User.ID = userGetId(username)
|
case float64:
|
||||||
|
command.User.ID = uint(claims["user_id"].(float64))
|
||||||
|
|
||||||
|
default:
|
||||||
|
command.User.ID = claims["user_id"].(uint)
|
||||||
|
}
|
||||||
command.Uuid = c.Param("uuid")
|
command.Uuid = c.Param("uuid")
|
||||||
command.commandDelete()
|
command.commandDelete()
|
||||||
})
|
})
|
||||||
|
@ -317,8 +334,13 @@ func Run() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
claims := jwt.ExtractClaims(c)
|
claims := jwt.ExtractClaims(c)
|
||||||
username := claims["username"].(string)
|
switch claims["user_id"].(type) {
|
||||||
system.User.ID = userGetId(username)
|
case float64:
|
||||||
|
system.User.ID = uint(claims["user_id"].(float64))
|
||||||
|
|
||||||
|
default:
|
||||||
|
system.User.ID = claims["user_id"].(uint)
|
||||||
|
}
|
||||||
|
|
||||||
system.systemInsert()
|
system.systemInsert()
|
||||||
c.AbortWithStatus(201)
|
c.AbortWithStatus(201)
|
||||||
|
@ -332,10 +354,16 @@ func Run() {
|
||||||
c.AbortWithStatus(http.StatusBadRequest)
|
c.AbortWithStatus(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
username := claims["username"].(string)
|
switch claims["user_id"].(type) {
|
||||||
system.User.ID = userGetId(username)
|
case float64:
|
||||||
|
system.User.ID = uint(claims["user_id"].(float64))
|
||||||
|
|
||||||
|
default:
|
||||||
|
system.User.ID = claims["user_id"].(uint)
|
||||||
|
}
|
||||||
|
|
||||||
system.Mac = mac
|
system.Mac = mac
|
||||||
result, err := system.systemGet()
|
result, err := system.systemGet()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
|
@ -347,9 +375,14 @@ func Run() {
|
||||||
r.GET("/api/v1/client-view/status", func(c *gin.Context) {
|
r.GET("/api/v1/client-view/status", func(c *gin.Context) {
|
||||||
var status Status
|
var status Status
|
||||||
claims := jwt.ExtractClaims(c)
|
claims := jwt.ExtractClaims(c)
|
||||||
username := claims["username"].(string)
|
switch claims["user_id"].(type) {
|
||||||
status.Username = username
|
case float64:
|
||||||
status.User.ID = userGetId(username)
|
status.User.ID = uint(claims["user_id"].(float64))
|
||||||
|
|
||||||
|
default:
|
||||||
|
status.User.ID = claims["user_id"].(uint)
|
||||||
|
}
|
||||||
|
|
||||||
status.SessionName = c.Query("processId")
|
status.SessionName = c.Query("processId")
|
||||||
t, err := strconv.Atoi(c.Query("startTime"))
|
t, err := strconv.Atoi(c.Query("startTime"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Add table
Reference in a new issue