|
|
|
|
@@ -52,27 +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) > 1 {
|
|
|
|
|
aH.logger.Error("Login", "more than one record found")
|
|
|
|
|
c.JSON(http.StatusInternalServerError, models.NewJsonMessageResponse("internal error"))
|
|
|
|
|
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
|
|
|
|
|
@@ -173,7 +164,11 @@ func (aH *AccessHandler) Refresh(c *gin.Context) {
|
|
|
|
|
|
|
|
|
|
username := claims["username"].(string)
|
|
|
|
|
id := int(claims["id"].(float64))
|
|
|
|
|
role := claims["role"].(string)
|
|
|
|
|
|
|
|
|
|
user, hasError := aH.getUserFromDB(c, username)
|
|
|
|
|
if hasError {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create new access token
|
|
|
|
|
aH.logger.Debug("Refresh", "create new access token")
|
|
|
|
|
@@ -181,7 +176,7 @@ func (aH *AccessHandler) Refresh(c *gin.Context) {
|
|
|
|
|
newAccess := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
|
|
|
|
"id": id,
|
|
|
|
|
"username": username,
|
|
|
|
|
"role": role,
|
|
|
|
|
"role": user.Role,
|
|
|
|
|
"exp": accessExp.Unix(),
|
|
|
|
|
})
|
|
|
|
|
accessString, _ := newAccess.SignedString(ACCESS_SECRET)
|
|
|
|
|
@@ -240,3 +235,34 @@ func (aH *AccessHandler) Logout(c *gin.Context) {
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "logged out"})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
aH.logger.Error("Login", err)
|
|
|
|
|
c.JSON(http.StatusBadRequest, models.NewJsonErrorResponse(err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|