fix get user by token method
This commit is contained in:
1 parent
8f2a6a58f2
commit
0d956b6bf6
4 files changed
+69
-7
No files matched your search
+16
-4
@@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
"github.com/integration-system/isp-lib/logger"
|
||||
libStr "github.com/integration-system/isp-lib/structure"
|
||||
"msp-admin-service/token"
|
||||
"msp-admin-service/service"
|
||||
//"github.com/integration-system/isp-lib/token-gen"
|
||||
"github.com/integration-system/isp-lib/utils"
|
||||
libUtils "github.com/integration-system/isp-lib/utils"
|
||||
@@ -36,11 +36,23 @@ func GetProfile(metadata metadata.MD) (*structure.AdminUserShort, error) {
|
||||
st := status.New(codes.InvalidArgument, utils.ServiceError)
|
||||
return nil, st.Err()
|
||||
}
|
||||
user, err := model.GetUserByToken(token[0])
|
||||
|
||||
userId, err := service.GetUserId(token[0])
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Unauthenticated, "Invalid auth token")
|
||||
}
|
||||
|
||||
user, err := model.GetUserById(userId)
|
||||
if err != nil {
|
||||
return nil, validate.CreateUnknownError(err)
|
||||
}
|
||||
return user, nil
|
||||
return &structure.AdminUserShort{
|
||||
Image: user.Image,
|
||||
FirstName: user.FirstName,
|
||||
LastName: user.LastName,
|
||||
Email: user.Email,
|
||||
Phone: user.Phone,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func Login(authRequest structure.AuthRequest) (*structure.Auth, error) {
|
||||
@@ -56,7 +68,7 @@ func Login(authRequest structure.AuthRequest) (*structure.Auth, error) {
|
||||
return nil, status.New(codes.Unauthenticated, "Email or password is incorrect").Err()
|
||||
}
|
||||
|
||||
tokenString, expired, err := token.GetToken(string(user.Id))
|
||||
tokenString, expired, err := service.GenerateToken(user.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
+2
-2
@@ -10,7 +10,7 @@ import (
|
||||
|
||||
const DELETE_USERS = "DELETE FROM " + utils.DB_SCHEME + ".users WHERE id IN (?)"
|
||||
|
||||
func GetUserByToken(token string) (*structure.AdminUserShort, error) {
|
||||
/*func GetUserByToken(token string) (*structure.AdminUserShort, error) {
|
||||
var user structure.AdminUserShort
|
||||
_, err := database.GetDBManager().Db.Model(&user).
|
||||
Query(&user, `SELECT
|
||||
@@ -28,7 +28,7 @@ func GetUserByToken(token string) (*structure.AdminUserShort, error) {
|
||||
return nil, nil
|
||||
}
|
||||
return &user, err
|
||||
}
|
||||
}*/
|
||||
|
||||
func GetUserByEmail(email string) (*libStr.AdminUser, error) {
|
||||
var user libStr.AdminUser
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"github.com/integration-system/isp-lib/config"
|
||||
"github.com/pkg/errors"
|
||||
"msp-admin-service/conf"
|
||||
"time"
|
||||
)
|
||||
|
||||
type customClaims struct {
|
||||
Id int64
|
||||
jwt.StandardClaims
|
||||
}
|
||||
|
||||
func GenerateToken(id int64) (string, string, error) {
|
||||
expired := ""
|
||||
claims := customClaims{Id: id}
|
||||
cfg := config.GetRemote().(*conf.RemoteConfig)
|
||||
if cfg.ExpireSec != 0 {
|
||||
exp := time.Now().Add(time.Second * time.Duration(cfg.ExpireSec))
|
||||
expired = exp.String()
|
||||
claims.ExpiresAt = exp.Unix()
|
||||
}
|
||||
tokenString, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString([]byte(cfg.SecretKey))
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
return tokenString, expired, nil
|
||||
}
|
||||
|
||||
func GetUserId(token string) (int64, error) {
|
||||
cfg := config.GetRemote().(*conf.RemoteConfig)
|
||||
parsed, err := jwt.ParseWithClaims(token, &customClaims{}, func(token *jwt.Token) (i interface{}, e error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
return []byte(cfg.SecretKey), nil
|
||||
})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if claims, ok := parsed.Claims.(*customClaims); ok && parsed.Valid {
|
||||
return claims.Id, nil
|
||||
} else {
|
||||
return 0, errors.New("token is invalid")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user