-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add jwt handling functionality with tests
Signed-off-by: Arnav Gupta <[email protected]>
- Loading branch information
1 parent
0d8dd14
commit 4e85ad6
Showing
4 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package auth | ||
|
||
import ( | ||
"github.com/golang-jwt/jwt/v5" | ||
"github.com/samber/lo" | ||
"onepixel_backend/src/models" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
// TODO: pick JWT_KEY from config | ||
|
||
var JWT_KEY = "this is a sample key, should change in prod" | ||
var JWT_SIGNING_METHOD = jwt.SigningMethodHS256 | ||
|
||
func CreateJWTFromUser(u *models.User) string { | ||
token := jwt.NewWithClaims(JWT_SIGNING_METHOD, jwt.MapClaims{ | ||
"sub": strconv.Itoa(int(u.ID)), | ||
"iat": jwt.NewNumericDate(time.Now()), | ||
"exp": jwt.NewNumericDate(time.Now().Add(time.Hour * 24 * 7)), // 7 days | ||
}) | ||
|
||
return lo.Must(token.SignedString([]byte(JWT_KEY))) | ||
} | ||
|
||
func ValidateJWT(t string) (*uint, error) { | ||
claims := jwt.MapClaims{} | ||
_, err := jwt.ParseWithClaims(t, claims, func(token *jwt.Token) (interface{}, error) { | ||
return []byte(JWT_KEY), nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
sub := uint(lo.Must(strconv.Atoi(lo.Must(claims.GetSubject())))) | ||
exp := lo.Must(claims.GetExpirationTime()) | ||
if exp.Before(time.Now()) { | ||
return nil, jwt.ErrTokenExpired | ||
} | ||
return &sub, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package auth | ||
|
||
import ( | ||
"github.com/gofiber/fiber/v2/log" | ||
"github.com/stretchr/testify/assert" | ||
"onepixel_backend/src/auth" | ||
"onepixel_backend/src/models" | ||
"testing" | ||
) | ||
|
||
func TestJwt_CreateToken(t *testing.T) { | ||
testUser := &models.User{ | ||
ID: 12, | ||
} | ||
|
||
jwt := auth.CreateJWTFromUser(testUser) | ||
log.Info("jwt: ", jwt) | ||
assert.NotNil(t, jwt) | ||
} | ||
|
||
func TestJwt_ParseToken(t *testing.T) { | ||
testUser := &models.User{ | ||
ID: 12, | ||
} | ||
|
||
jwt := auth.CreateJWTFromUser(testUser) | ||
log.Info("jwt: ", jwt) | ||
assert.NotNil(t, jwt) | ||
|
||
userID, err := auth.ValidateJWT(jwt) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, userID) | ||
assert.Equal(t, testUser.ID, *userID) | ||
} |