-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 33d6507
Showing
3 changed files
with
44 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
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)) | ||
} |
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,5 @@ | ||
module github.com/cardboardrobots/token | ||
|
||
go 1.23.0 | ||
|
||
require github.com/golang-jwt/jwt/v4 v4.5.0 |
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,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= |