dev: hotfix, cleaning + shortent code for auth
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package authentication
|
||||
|
||||
import (
|
||||
dg "github.com/karincake/apem/db-gorm-pg"
|
||||
|
||||
pl "simrs-vx/pkg/logger"
|
||||
|
||||
edp "simrs-vx/internal/domain/main-entities/division-position"
|
||||
eip "simrs-vx/internal/domain/main-entities/installation-position"
|
||||
esp "simrs-vx/internal/domain/main-entities/specialist-position"
|
||||
@@ -12,8 +16,6 @@ import (
|
||||
usp "simrs-vx/internal/use-case/main-use-case/specialist-position"
|
||||
ussp "simrs-vx/internal/use-case/main-use-case/subspecialist-position"
|
||||
uup "simrs-vx/internal/use-case/main-use-case/unit-position"
|
||||
|
||||
dg "github.com/karincake/apem/db-gorm-pg"
|
||||
)
|
||||
|
||||
// just return the error code
|
||||
@@ -28,43 +30,21 @@ func getAndCheck(input, condition any) (eCode string) {
|
||||
return ""
|
||||
}
|
||||
|
||||
func getDivisionPosition(employee_id uint) ([]string, error) {
|
||||
func getDivisionPosition(employee_id uint, event *pl.Event) ([]string, error) {
|
||||
var result []string
|
||||
|
||||
// var employee ee.Employee
|
||||
// if err := dg.I.Where("\"Employee_Id\" = ?", employee_id).First(&employee).Error; err != nil {
|
||||
// if err == gorm.ErrRecordNotFound {
|
||||
// return result, nil
|
||||
// }
|
||||
// return result, errors.New("no employee found")
|
||||
// }
|
||||
|
||||
//var divisionPositions []edp.DivisionPosition
|
||||
//err := dg.I.
|
||||
// Preload("Division").
|
||||
// Where("\"Employee_Id\" = ?", employee_id).
|
||||
// Find(&divisionPositions).Error
|
||||
//if err != nil {
|
||||
// if err == gorm.ErrRecordNotFound {
|
||||
// return result, nil
|
||||
// }
|
||||
// return result, err
|
||||
//}
|
||||
|
||||
// get data division_position based on employee_id
|
||||
dataDivisionPosition, err := udp.ReadList(edp.ReadListDto{
|
||||
data, _, err := udp.ReadListData(edp.ReadListDto{
|
||||
FilterDto: edp.FilterDto{Employee_Id: &employee_id},
|
||||
Includes: "division"})
|
||||
Includes: "Division"}, event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if list, ok := dataDivisionPosition.Data.([]edp.ResponseDto); ok {
|
||||
if len(list) > 0 {
|
||||
for _, dp := range list {
|
||||
if dp.Division != nil {
|
||||
result = append(result, "div-"+dp.Division.Code+"-"+dp.Code)
|
||||
}
|
||||
if len(data) > 0 {
|
||||
for _, dp := range data {
|
||||
if dp.Division != nil {
|
||||
result = append(result, "div-"+dp.Division.Code+"-"+dp.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -72,23 +52,21 @@ func getDivisionPosition(employee_id uint) ([]string, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func getInstallationPosition(employeeId uint) ([]string, error) {
|
||||
func getInstallationPosition(employeeId uint, event *pl.Event) ([]string, error) {
|
||||
var result []string
|
||||
|
||||
// get data unit_position based on employee_id
|
||||
dataInstallationPosition, err := uip.ReadList(eip.ReadListDto{
|
||||
data, _, err := uip.ReadListData(eip.ReadListDto{
|
||||
FilterDto: eip.FilterDto{Employee_Id: &employeeId},
|
||||
Includes: "installation"})
|
||||
Includes: "installation"}, event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if list, ok := dataInstallationPosition.Data.([]eip.ResponseDto); ok {
|
||||
if len(list) > 0 {
|
||||
for _, dp := range list {
|
||||
if dp.Installation != nil {
|
||||
result = append(result, "inst-"+dp.Installation.Code+"-"+dp.Code)
|
||||
}
|
||||
if len(data) > 0 {
|
||||
for _, dp := range data {
|
||||
if dp.Installation != nil {
|
||||
result = append(result, "inst-"+dp.Installation.Code+"-"+dp.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -96,23 +74,21 @@ func getInstallationPosition(employeeId uint) ([]string, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func getUnitPosition(employeeId uint) ([]string, error) {
|
||||
func getUnitPosition(employeeId uint, event *pl.Event) ([]string, error) {
|
||||
var result []string
|
||||
|
||||
// get data unit_position based on employee_id
|
||||
dataUnitPosition, err := uup.ReadList(eup.ReadListDto{
|
||||
data, _, err := uup.ReadListData(eup.ReadListDto{
|
||||
FilterDto: eup.FilterDto{Employee_Id: &employeeId},
|
||||
Includes: "unit"})
|
||||
Includes: "unit"}, event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if list, ok := dataUnitPosition.Data.([]eup.ResponseDto); ok {
|
||||
if len(list) > 0 {
|
||||
for _, dp := range list {
|
||||
if dp.Unit != nil {
|
||||
result = append(result, "unit-"+dp.Unit.Code+"-"+dp.Code)
|
||||
}
|
||||
if len(data) > 0 {
|
||||
for _, dp := range data {
|
||||
if dp.Unit != nil {
|
||||
result = append(result, "unit-"+dp.Unit.Code+"-"+dp.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -120,23 +96,21 @@ func getUnitPosition(employeeId uint) ([]string, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func getSpecialistPosition(employeeId uint) ([]string, error) {
|
||||
func getSpecialistPosition(employeeId uint, event *pl.Event) ([]string, error) {
|
||||
var result []string
|
||||
|
||||
// get data unit_position based on employee_id
|
||||
dataSpecialistPosition, err := usp.ReadList(esp.ReadListDto{
|
||||
data, _, err := usp.ReadListData(esp.ReadListDto{
|
||||
FilterDto: esp.FilterDto{Employee_Id: &employeeId},
|
||||
Includes: "specialist"})
|
||||
Includes: "specialist"}, event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if list, ok := dataSpecialistPosition.Data.([]esp.ResponseDto); ok {
|
||||
if len(list) > 0 {
|
||||
for _, dp := range list {
|
||||
if dp.Specialist != nil {
|
||||
result = append(result, "spec-"+dp.Specialist.Code+"-"+dp.Code)
|
||||
}
|
||||
if len(data) > 0 {
|
||||
for _, dp := range data {
|
||||
if dp.Specialist != nil {
|
||||
result = append(result, "spec-"+dp.Specialist.Code+"-"+dp.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -144,26 +118,47 @@ func getSpecialistPosition(employeeId uint) ([]string, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func getSubspecialistPosition(employeeId uint) ([]string, error) {
|
||||
func getSubspecialistPosition(employeeId uint, event *pl.Event) ([]string, error) {
|
||||
var result []string
|
||||
|
||||
// get data unit_position based on employee_id
|
||||
dataSubspecialistPosition, err := ussp.ReadList(essp.ReadListDto{
|
||||
data, _, err := ussp.ReadListData(essp.ReadListDto{
|
||||
FilterDto: essp.FilterDto{Employee_Id: &employeeId},
|
||||
Includes: "subspecialist"})
|
||||
Includes: "subspecialist"}, event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if list, ok := dataSubspecialistPosition.Data.([]essp.ResponseDto); ok {
|
||||
if len(list) > 0 {
|
||||
for _, dp := range list {
|
||||
if dp.Subspecialist != nil {
|
||||
result = append(result, "subspec-"+dp.Subspecialist.Code+"-"+dp.Code)
|
||||
}
|
||||
if len(data) > 0 {
|
||||
for _, dp := range data {
|
||||
if dp.Subspecialist != nil {
|
||||
result = append(result, "subspec-"+dp.Subspecialist.Code+"-"+dp.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
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 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