Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjohnsonaz committed Sep 8, 2024
0 parents commit 33d6507
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
37 changes: 37 additions & 0 deletions authentication.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package token

import (
"strings"

"github.com/golang-jwt/jwt/v4"
)

const (
bearerPrefix = "Bearer "
bearerPrefixLower = "bearer "
)

func GetClaims(authorization string, key string, claims jwt.Claims) error {
bearer := getBearer(authorization)

return ParseClaims(bearer, key, claims)
}

func getBearer(authorization string) string {
return strings.TrimPrefix(strings.TrimPrefix(
authorization,
bearerPrefix),
bearerPrefixLower)
}

func ParseClaims(token string, key string, claims jwt.Claims) error {
_, err := jwt.ParseWithClaims(token, claims, func(t *jwt.Token) (interface{}, error) {
return []byte(key), nil
})
return err
}

func CreateToken(key string, claims jwt.Claims) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString([]byte(key))
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/cardboardrobots/token

go 1.23.0

require github.com/golang-jwt/jwt/v4 v4.5.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=

0 comments on commit 33d6507

Please sign in to comment.