dev: hotfix, cleaning + shortent code for auth
This commit is contained in:
No files matched your search
@@ -9,28 +9,28 @@ import (
|
||||
|
||||
"github.com/golang-jwt/jwt"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"simrs-vx/internal/domain/main-entities/intern"
|
||||
eu "simrs-vx/internal/domain/main-entities/user"
|
||||
|
||||
pa "simrs-vx/internal/lib/auth"
|
||||
el "simrs-vx/pkg/logger"
|
||||
p "simrs-vx/pkg/password"
|
||||
|
||||
ed "simrs-vx/internal/domain/main-entities/doctor"
|
||||
ee "simrs-vx/internal/domain/main-entities/employee"
|
||||
em "simrs-vx/internal/domain/main-entities/midwife"
|
||||
en "simrs-vx/internal/domain/main-entities/nurse"
|
||||
erc "simrs-vx/internal/domain/references/common"
|
||||
erg "simrs-vx/internal/domain/references/organization"
|
||||
|
||||
a "github.com/karincake/apem"
|
||||
dg "github.com/karincake/apem/db-gorm-pg"
|
||||
ms "github.com/karincake/apem/ms-redis"
|
||||
d "github.com/karincake/dodol"
|
||||
l "github.com/karincake/lepet"
|
||||
|
||||
pa "simrs-vx/internal/lib/auth"
|
||||
pl "simrs-vx/pkg/logger"
|
||||
p "simrs-vx/pkg/password"
|
||||
|
||||
ed "simrs-vx/internal/domain/main-entities/doctor"
|
||||
ee "simrs-vx/internal/domain/main-entities/employee"
|
||||
"simrs-vx/internal/domain/main-entities/intern"
|
||||
em "simrs-vx/internal/domain/main-entities/midwife"
|
||||
en "simrs-vx/internal/domain/main-entities/nurse"
|
||||
eu "simrs-vx/internal/domain/main-entities/user"
|
||||
erc "simrs-vx/internal/domain/references/common"
|
||||
erg "simrs-vx/internal/domain/references/organization"
|
||||
)
|
||||
|
||||
const source = "authentication"
|
||||
|
||||
var authCfg AuthCfg
|
||||
|
||||
func init() {
|
||||
@@ -40,13 +40,18 @@ func init() {
|
||||
// Generates token and store in redis at one place
|
||||
// just return the error code
|
||||
func GenToken(input eu.LoginDto) (*d.Data, error) {
|
||||
event := pl.Event{
|
||||
Feature: "Create",
|
||||
Source: source,
|
||||
}
|
||||
|
||||
// Get User
|
||||
user := &eu.User{Name: input.Name}
|
||||
// if input.Position_Code != "" {
|
||||
// user.Position_Code = input.Position_Code
|
||||
// }
|
||||
if errCode := getAndCheck(user, user); errCode != "" {
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: errCode, Message: el.GenMessage(errCode)}}
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: errCode, Message: pl.GenMessage(errCode)}}
|
||||
}
|
||||
|
||||
if user.LoginAttemptCount > 5 {
|
||||
@@ -54,7 +59,7 @@ func GenToken(input eu.LoginDto) (*d.Data, error) {
|
||||
now := time.Now()
|
||||
lastAllowdLogin := user.LastAllowdLogin
|
||||
if lastAllowdLogin.After(now.Add(-time.Hour * 1)) {
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-login-tooMany", Message: el.GenMessage("auth-login-tooMany")}}
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-login-tooMany", Message: pl.GenMessage("auth-login-tooMany")}}
|
||||
} else {
|
||||
tn := time.Now()
|
||||
user.LastAllowdLogin = &tn
|
||||
@@ -65,18 +70,18 @@ func GenToken(input eu.LoginDto) (*d.Data, error) {
|
||||
tn := time.Now()
|
||||
user.LastAllowdLogin = &tn
|
||||
dg.I.Save(&user)
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-login-tooMany", Message: el.GenMessage("auth-login-tooMany")}}
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-login-tooMany", Message: pl.GenMessage("auth-login-tooMany")}}
|
||||
}
|
||||
}
|
||||
|
||||
if !p.Check(input.Password, user.Password) {
|
||||
user.LoginAttemptCount++
|
||||
dg.I.Save(&user)
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-login-incorrect", Message: el.GenMessage("auth-login-incorrect")}}
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-login-incorrect", Message: pl.GenMessage("auth-login-incorrect")}}
|
||||
} else if user.Status_Code == erc.USCBlocked {
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-login-blocked", Message: el.GenMessage("auth-login-blocked")}}
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-login-blocked", Message: pl.GenMessage("auth-login-blocked")}}
|
||||
} else if user.Status_Code == erc.USCNew {
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-login-unverified", Message: el.GenMessage("auth-login-unverified")}}
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-login-unverified", Message: pl.GenMessage("auth-login-unverified")}}
|
||||
}
|
||||
|
||||
// Access token prep
|
||||
@@ -115,7 +120,7 @@ func GenToken(input eu.LoginDto) (*d.Data, error) {
|
||||
employee := ee.Employee{}
|
||||
dg.I.Where("\"User_Id\" = ?", user.Id).First(&employee)
|
||||
if employee.Id == 0 {
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noEmployee", Message: el.GenMessage("auth-noEmployee")}}
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noEmployee", Message: pl.GenMessage("auth-noEmployee")}}
|
||||
}
|
||||
atClaims["employee_id"] = employee.Id
|
||||
outputData["employee_id"] = employee.Id
|
||||
@@ -134,7 +139,7 @@ func GenToken(input eu.LoginDto) (*d.Data, error) {
|
||||
doctor := ed.Doctor{}
|
||||
dg.I.Where("\"Employee_Id\" = ?", employee.Id).First(&doctor)
|
||||
if doctor.Id == 0 {
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noDoctor", Message: el.GenMessage("auth-noDoctor")}}
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noDoctor", Message: pl.GenMessage("auth-noDoctor")}}
|
||||
}
|
||||
atClaims["doctor_code"] = doctor.Code
|
||||
outputData["doctor_code"] = doctor.Code
|
||||
@@ -152,7 +157,7 @@ func GenToken(input eu.LoginDto) (*d.Data, error) {
|
||||
empData := en.Nurse{}
|
||||
dg.I.Where("\"Employee_Id\" = ?", employee.Id).First(&empData)
|
||||
if empData.Id == 0 {
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noNurse", Message: el.GenMessage("auth-noNurse")}}
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noNurse", Message: pl.GenMessage("auth-noNurse")}}
|
||||
}
|
||||
atClaims["nurse_code"] = empData.Code
|
||||
outputData["nurse_code"] = empData.Code
|
||||
@@ -160,40 +165,40 @@ func GenToken(input eu.LoginDto) (*d.Data, error) {
|
||||
empData := em.Midwife{}
|
||||
dg.I.Where("\"Employee_Id\" = ?", employee.Id).First(&empData)
|
||||
if empData.Id == 0 {
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noMidwife", Message: el.GenMessage("auth-noMidwife")}}
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noMidwife", Message: pl.GenMessage("auth-noMidwife")}}
|
||||
}
|
||||
atClaims["midwife_code"] = empData.Code
|
||||
outputData["midwife_code"] = empData.Code
|
||||
}
|
||||
|
||||
errorGetPosition := d.FieldErrors{"authentication": d.FieldError{Code: "auth-getData-failed", Message: el.GenMessage("auth-getData-failed")}}
|
||||
errorGetPosition := d.FieldErrors{"authentication": d.FieldError{Code: "auth-getData-failed", Message: pl.GenMessage("auth-getData-failed")}}
|
||||
|
||||
// division position
|
||||
divisionPositions, err := getDivisionPosition(employee.Id)
|
||||
divisionPositions, err := getDivisionPosition(employee.Id, &event)
|
||||
if err != nil {
|
||||
return nil, errorGetPosition
|
||||
}
|
||||
|
||||
// installation position
|
||||
installationPositions, err := getInstallationPosition(employee.Id)
|
||||
installationPositions, err := getInstallationPosition(employee.Id, &event)
|
||||
if err != nil {
|
||||
return nil, errorGetPosition
|
||||
}
|
||||
|
||||
// unit position
|
||||
unitPositions, err := getUnitPosition(employee.Id)
|
||||
unitPositions, err := getUnitPosition(employee.Id, &event)
|
||||
if err != nil {
|
||||
return nil, errorGetPosition
|
||||
}
|
||||
|
||||
// specialist position
|
||||
specialistPositions, err := getSpecialistPosition(employee.Id)
|
||||
specialistPositions, err := getSpecialistPosition(employee.Id, &event)
|
||||
if err != nil {
|
||||
return nil, errorGetPosition
|
||||
}
|
||||
|
||||
// subspecialist position
|
||||
subspecialistPositions, err := getSubspecialistPosition(employee.Id)
|
||||
subspecialistPositions, err := getSubspecialistPosition(employee.Id, &event)
|
||||
if err != nil {
|
||||
return nil, errorGetPosition
|
||||
}
|
||||
@@ -220,7 +225,7 @@ func GenToken(input eu.LoginDto) (*d.Data, error) {
|
||||
at := jwt.NewWithClaims(jwt.SigningMethodHS256, atClaims)
|
||||
ats, err := at.SignedString([]byte(atSecretKey))
|
||||
if err != nil {
|
||||
return nil, d.FieldErrors{"user": d.FieldError{Code: "token-sign-err", Message: el.GenMessage("token-sign-err")}}
|
||||
return nil, d.FieldErrors{"user": d.FieldError{Code: "token-sign-err", Message: pl.GenMessage("token-sign-err")}}
|
||||
}
|
||||
outputData["accessToken"] = ats
|
||||
|
||||
@@ -283,21 +288,21 @@ func VerifyToken(r *http.Request, tokenType TokenType) (data *jwt.Token, errCode
|
||||
func ExtractToken(r *http.Request, tokenType TokenType) (data *pa.AuthInfo, err error) {
|
||||
token, errCode, errDetail := VerifyToken(r, tokenType)
|
||||
if errCode != "" {
|
||||
return nil, d.FieldError{Code: errCode, Message: el.GenMessage(errCode, errDetail)}
|
||||
return nil, d.FieldError{Code: errCode, Message: pl.GenMessage(errCode, errDetail)}
|
||||
}
|
||||
claims, ok := token.Claims.(jwt.MapClaims)
|
||||
if ok && token.Valid {
|
||||
accessUuid, ok := claims["uuid"].(string)
|
||||
if !ok {
|
||||
return nil, d.FieldError{Code: "token-invalid", Message: el.GenMessage("token-invalid", "uuid not available")}
|
||||
return nil, d.FieldError{Code: "token-invalid", Message: pl.GenMessage("token-invalid", "uuid not available")}
|
||||
}
|
||||
user_id, myErr := strconv.ParseInt(fmt.Sprintf("%.f", claims["user_id"]), 10, 64)
|
||||
if myErr != nil {
|
||||
return nil, d.FieldError{Code: "token-invalid", Message: el.GenMessage("token-invalid", "uuid is not available")}
|
||||
return nil, d.FieldError{Code: "token-invalid", Message: pl.GenMessage("token-invalid", "uuid is not available")}
|
||||
}
|
||||
accessUuidRedis := ms.I.Get(accessUuid)
|
||||
if accessUuidRedis.String() == "" {
|
||||
return nil, d.FieldError{Code: "token-unidentified", Message: el.GenMessage("token-unidentified")}
|
||||
return nil, d.FieldError{Code: "token-unidentified", Message: pl.GenMessage("token-unidentified")}
|
||||
}
|
||||
|
||||
data = &pa.AuthInfo{
|
||||
@@ -324,41 +329,3 @@ func ExtractToken(r *http.Request, tokenType TokenType) (data *pa.AuthInfo, err
|
||||
func GetConfig() {
|
||||
a.ParseCfg(&authCfg)
|
||||
}
|
||||
|
||||
func checkStrClaims(claim map[string]interface{}, key string) string {
|
||||
if v, exist := claim[key]; exist && v != nil {
|
||||
return v.(string)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func checkStrPtrClaims(claim map[string]interface{}, key string) *string {
|
||||
if v, exist := claim[key]; exist && v != nil {
|
||||
val := v.(string)
|
||||
return &val
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkIntClaims(claim map[string]interface{}, key string) int {
|
||||
if v, exist := claim[key]; exist && v != nil {
|
||||
return v.(int)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func checkIntPtrClaims(claim map[string]interface{}, key string) *int {
|
||||
if v, exist := claim[key]; exist && v != nil {
|
||||
val := int(v.(float64))
|
||||
return &val
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkUntPtrClaims(claim map[string]interface{}, key string) *uint {
|
||||
if v, exist := claim[key]; exist && v != nil {
|
||||
val := uint(v.(float64))
|
||||
return &val
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user