Skip to content

Commit

Permalink
add jwt handling functionality with tests
Browse files Browse the repository at this point in the history
Signed-off-by: Arnav Gupta <[email protected]>
  • Loading branch information
championswimmer committed Nov 5, 2023
1 parent 0d8dd14 commit 4e85ad6
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
require (
github.com/andybalholm/brotli v1.0.6 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang-jwt/jwt/v5 v5.0.0 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gofiber/fiber/v2 v2.50.0 h1:ia0JaB+uw3GpNSCR5nvC5dsaxXjRU5OEu36aytx+zGw=
github.com/gofiber/fiber/v2 v2.50.0/go.mod h1:21eytvay9Is7S6z+OgPi7c7n4++tnClWmhpimVHMimw=
github.com/golang-jwt/jwt/v5 v5.0.0 h1:1n1XNM9hk7O9mnQoNBGolZvzebBQ7p93ULHRc28XJUE=
github.com/golang-jwt/jwt/v5 v5.0.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
Expand Down
40 changes: 40 additions & 0 deletions src/auth/jwt.go
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
}
34 changes: 34 additions & 0 deletions tests/auth/jwt_test.go
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)
}

0 comments on commit 4e85ad6

Please sign in to comment.