Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/remove utc #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions handler/loginHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ func LoginHandler(context *gin.Context) {
// validate the loginObj for valid credential adn if these are valid then

var claims = &models.JwtClaims{}
claims.ComapnyId = "ComapnyId"
claims.CompanyId = "CompanyId"
claims.Username = loginObj.UserName
claims.Roles = []int{1,2,3}
claims.Roles = []int{1, 2, 3}
claims.Audience = context.Request.Header.Get("Referer") // get it from Referer header

var tokenCreationTime = time.Now().UTC()
var expirationTime = tokenCreationTime.Add(time.Duration(2) * time.Hour)
tokeString, err := token.GenrateToken(claims, expirationTime)
tokenString, err := token.GenrateToken(claims, expirationTime)

if err != nil {
badRequest(context, http.StatusBadRequest, "error in gerating token", []models.ErrorDetail{
Expand All @@ -41,5 +41,5 @@ func LoginHandler(context *gin.Context) {
})
}

ok(context, http.StatusOK, "token created", tokeString)
ok(context, http.StatusOK, "token created", tokenString)
}
5 changes: 2 additions & 3 deletions middleware/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func ReturnUnauthorized(context *gin.Context) {
})
}


func ValidateToken() gin.HandlerFunc {
return func(context *gin.Context) {
tokenString := context.Request.Header.Get("apikey")
Expand All @@ -34,7 +33,7 @@ func ValidateToken() gin.HandlerFunc {
if len(context.Keys) == 0 {
context.Keys = make(map[string]interface{})
}
context.Keys["ComapnyId"] = claims.ComapnyId
context.Keys["CompanyId"] = claims.CompanyId
context.Keys["Username"] = claims.Username
context.Keys["Roles"] = claims.Roles

Expand Down Expand Up @@ -66,4 +65,4 @@ func Authorization(validRoles []int) gin.HandlerFunc {
}
}
}
}
}
8 changes: 4 additions & 4 deletions models/ClaimsModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type JwtClaims struct {
ComapnyId string `json:"comapnyId,omitempty"`
CompanyId string `json:"comapnyId,omitempty"`
Username string `json:"username,omitempty"`
Roles []int `json:"roles,omitempty"`
jwt.StandardClaims
Expand All @@ -18,13 +18,13 @@ type JwtClaims struct {
const ip = "192.168.0.107"

func (claims JwtClaims) Valid() error {
var now = time.Now().UTC().Unix()
var now = time.Now().Unix()
if claims.VerifyExpiresAt(now, true) && claims.VerifyIssuer(ip, true) {
return nil
return nil
}
return fmt.Errorf("Token is invalid")
}

func (claims JwtClaims) VerifyAudience(origin string) bool {
return strings.Compare(claims.Audience, origin) == 0
}
}
5 changes: 2 additions & 3 deletions token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
func GenrateToken(claims *models.JwtClaims, expirationTime time.Time) (string, error) {

claims.ExpiresAt = expirationTime.Unix()
claims.IssuedAt = time.Now().UTC().Unix()
claims.IssuedAt = time.Now().Unix()
claims.Issuer = ip

token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
Expand All @@ -29,7 +29,6 @@ func GenrateToken(claims *models.JwtClaims, expirationTime time.Time) (string, e
return tokenString, nil
}


func VerifyToken(tokenString, origin string) (bool, *models.JwtClaims) {
claims := &models.JwtClaims{}
token, _ := getTokenFromString(tokenString, claims)
Expand Down Expand Up @@ -60,4 +59,4 @@ func getTokenFromString(tokenString string, claims *models.JwtClaims) (*jwt.Toke
// hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key")
return []byte(jWTPrivateToken), nil
})
}
}