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 staticcheck issues #6

Open
wants to merge 1 commit 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
4 changes: 2 additions & 2 deletions domain/jwt_custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
type JwtCustomClaims struct {
Name string `json:"name"`
ID string `json:"id"`
jwt.StandardClaims
jwt.RegisteredClaims
}

type JwtCustomRefreshClaims struct {
ID string `json:"id"`
jwt.StandardClaims
jwt.RegisteredClaims
}
21 changes: 13 additions & 8 deletions internal/tokenutil/tokenutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
)

func CreateAccessToken(user *domain.User, secret string, expiry int) (accessToken string, err error) {
exp := time.Now().Add(time.Hour * time.Duration(expiry)).Unix()
nowTime := time.Now()
expireTime := nowTime.Add(time.Duration(expiry) * time.Hour)

claims := &domain.JwtCustomClaims{
Name: user.Name,
ID: user.ID.Hex(),
StandardClaims: jwt.StandardClaims{
ExpiresAt: exp,
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(expireTime),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
Expand All @@ -26,10 +28,13 @@ func CreateAccessToken(user *domain.User, secret string, expiry int) (accessToke
}

func CreateRefreshToken(user *domain.User, secret string, expiry int) (refreshToken string, err error) {
nowTime := time.Now()
expireTime := nowTime.Add(time.Duration(expiry) * time.Hour)

claimsRefresh := &domain.JwtCustomRefreshClaims{
ID: user.ID.Hex(),
StandardClaims: jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Hour * time.Duration(expiry)).Unix(),
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(expireTime),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claimsRefresh)
Expand All @@ -43,7 +48,7 @@ func CreateRefreshToken(user *domain.User, secret string, expiry int) (refreshTo
func IsAuthorized(requestToken string, secret string) (bool, error) {
_, err := jwt.Parse(requestToken, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
return []byte(secret), nil
})
Expand All @@ -56,7 +61,7 @@ func IsAuthorized(requestToken string, secret string) (bool, error) {
func ExtractIDFromToken(requestToken string, secret string) (string, error) {
token, err := jwt.Parse(requestToken, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
return []byte(secret), nil
})
Expand All @@ -68,7 +73,7 @@ func ExtractIDFromToken(requestToken string, secret string) (string, error) {
claims, ok := token.Claims.(jwt.MapClaims)

if !ok && !token.Valid {
return "", fmt.Errorf("Invalid Token")
return "", fmt.Errorf("invalid Token")
}

return claims["id"].(string), nil
Expand Down
26 changes: 0 additions & 26 deletions mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ package mongo

import (
"context"
"errors"
"reflect"
"time"

"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/bson/bsonrw"
"go.mongodb.org/mongo-driver/bson/bsontype"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
Expand Down Expand Up @@ -73,27 +68,6 @@ type mongoSession struct {
mongo.Session
}

type nullawareDecoder struct {
defDecoder bsoncodec.ValueDecoder
zeroValue reflect.Value
}

func (d *nullawareDecoder) DecodeValue(dctx bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
if vr.Type() != bsontype.Null {
return d.defDecoder.DecodeValue(dctx, vr, val)
}

if !val.CanSet() {
return errors.New("value not settable")
}
if err := vr.ReadNull(); err != nil {
return err
}
// Set the zero value of val's type:
val.Set(d.zeroValue)
return nil
}

func NewClient(connection string) (Client, error) {

time.Local = time.UTC
Expand Down