|
|
|
|
@@ -52,39 +52,18 @@ func (aH *AccessHandler) Login(c *gin.Context) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fetch user record from DB
|
|
|
|
|
var dbRecord []models.User
|
|
|
|
|
err := aH.dbHandler.GetByKey(&dbRecord, "user_name", user.Name, false)
|
|
|
|
|
if err != nil {
|
|
|
|
|
aH.logger.Error("Login", err)
|
|
|
|
|
c.JSON(http.StatusBadRequest, models.NewJsonErrorResponse(err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(dbRecord) == 0 {
|
|
|
|
|
aH.logger.Error("Login", "no user "+user.Name+" found")
|
|
|
|
|
c.JSON(http.StatusUnauthorized, models.NewJsonMessageResponse("invalid credentials"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(dbRecord) > 1 {
|
|
|
|
|
aH.logger.Error("Login", "more than one record found")
|
|
|
|
|
c.JSON(http.StatusInternalServerError, models.NewJsonMessageResponse("internal error"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !dbRecord[0].ExpirationIsValid() {
|
|
|
|
|
aH.logger.Error("Login", fmt.Sprintf("user %s is expired", user.Name))
|
|
|
|
|
c.JSON(http.StatusUnauthorized, models.NewJsonMessageResponse("user "+user.Name+" is expired"))
|
|
|
|
|
dbUser, hasError := aH.getUserFromDB(c, user.Name)
|
|
|
|
|
if hasError {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check password
|
|
|
|
|
if !utils.CheckPassword(user.Password, dbRecord[0].Password) {
|
|
|
|
|
if !utils.CheckPassword(user.Password, dbUser.Password) {
|
|
|
|
|
aH.logger.Error("Login", "invalid password")
|
|
|
|
|
c.JSON(http.StatusUnauthorized, models.NewJsonMessageResponse("invalid credentials"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
user = dbRecord[0]
|
|
|
|
|
user = dbUser
|
|
|
|
|
|
|
|
|
|
// -----------------------------
|
|
|
|
|
// 🔑 TOKEN CREATION
|
|
|
|
|
@@ -96,22 +75,20 @@ func (aH *AccessHandler) Login(c *gin.Context) {
|
|
|
|
|
|
|
|
|
|
// Create access token
|
|
|
|
|
accessToken := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
|
|
|
|
"id": user.Id,
|
|
|
|
|
"username": user.Name,
|
|
|
|
|
"role": user.Role,
|
|
|
|
|
"type": "access",
|
|
|
|
|
"exp": accessTokenExp.Unix(),
|
|
|
|
|
"userExpiration": user.Expiration,
|
|
|
|
|
"id": user.Id,
|
|
|
|
|
"username": user.Name,
|
|
|
|
|
"role": user.Role,
|
|
|
|
|
"type": "access",
|
|
|
|
|
"exp": accessTokenExp.Unix(),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Create refresh token
|
|
|
|
|
refreshToken := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
|
|
|
|
"id": user.Id,
|
|
|
|
|
"username": user.Name,
|
|
|
|
|
"role": user.Role,
|
|
|
|
|
"type": "refresh",
|
|
|
|
|
"exp": refreshTokenExp.Unix(),
|
|
|
|
|
"userExpiration": user.GetExpiration(),
|
|
|
|
|
"id": user.Id,
|
|
|
|
|
"username": user.Name,
|
|
|
|
|
"role": user.Role,
|
|
|
|
|
"type": "refresh",
|
|
|
|
|
"exp": refreshTokenExp.Unix(),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Sign tokens
|
|
|
|
|
@@ -189,10 +166,8 @@ func (aH *AccessHandler) Refresh(c *gin.Context) {
|
|
|
|
|
id := int(claims["id"].(float64))
|
|
|
|
|
role := claims["role"].(string)
|
|
|
|
|
|
|
|
|
|
if !expirationDateValid(claims["userExpiration"].(string)) {
|
|
|
|
|
aH.Logout(c)
|
|
|
|
|
aH.logger.Error("Refresh", fmt.Sprintf("user %s is expired", username))
|
|
|
|
|
c.JSON(http.StatusUnauthorized, models.NewJsonMessageResponse("user "+username+" is expired"))
|
|
|
|
|
_, hasError := aH.getUserFromDB(c, username)
|
|
|
|
|
if hasError {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -262,15 +237,33 @@ func (aH *AccessHandler) Logout(c *gin.Context) {
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "logged out"})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func expirationDateValid(expiration string) bool {
|
|
|
|
|
if expiration == "" {
|
|
|
|
|
return true // No expiration = always valid
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t, err := time.Parse(time.RFC3339, expiration)
|
|
|
|
|
func (aH *AccessHandler) getUserFromDB(c *gin.Context, userName string) (user models.User, hasError bool) {
|
|
|
|
|
hasError = true
|
|
|
|
|
// Fetch user record from DB
|
|
|
|
|
var dbRecord []models.User
|
|
|
|
|
err := aH.dbHandler.GetByKey(&dbRecord, "user_name", userName, false)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false // Invalid date format
|
|
|
|
|
aH.logger.Error("Login", err)
|
|
|
|
|
c.JSON(http.StatusBadRequest, models.NewJsonErrorResponse(err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return time.Now().Before(t)
|
|
|
|
|
if len(dbRecord) == 0 {
|
|
|
|
|
aH.logger.Error("Login", "no user "+userName+" found")
|
|
|
|
|
c.JSON(http.StatusUnauthorized, models.NewJsonMessageResponse("invalid credentials"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(dbRecord) > 1 {
|
|
|
|
|
aH.logger.Error("Login", "more than one record found")
|
|
|
|
|
c.JSON(http.StatusInternalServerError, models.NewJsonMessageResponse("internal error"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !dbRecord[0].ExpirationIsValid() {
|
|
|
|
|
aH.logger.Error("Login", fmt.Sprintf("user %s is expired", userName))
|
|
|
|
|
c.JSON(http.StatusUnauthorized, models.NewJsonMessageResponse("user "+userName+" is expired"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return dbRecord[0], false
|
|
|
|
|
}
|
|
|
|
|
|