Skip to content

Commit 33d6507

Browse files
committed
Initial commit.
0 parents  commit 33d6507

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

authentication.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package token
2+
3+
import (
4+
"strings"
5+
6+
"github.com/golang-jwt/jwt/v4"
7+
)
8+
9+
const (
10+
bearerPrefix = "Bearer "
11+
bearerPrefixLower = "bearer "
12+
)
13+
14+
func GetClaims(authorization string, key string, claims jwt.Claims) error {
15+
bearer := getBearer(authorization)
16+
17+
return ParseClaims(bearer, key, claims)
18+
}
19+
20+
func getBearer(authorization string) string {
21+
return strings.TrimPrefix(strings.TrimPrefix(
22+
authorization,
23+
bearerPrefix),
24+
bearerPrefixLower)
25+
}
26+
27+
func ParseClaims(token string, key string, claims jwt.Claims) error {
28+
_, err := jwt.ParseWithClaims(token, claims, func(t *jwt.Token) (interface{}, error) {
29+
return []byte(key), nil
30+
})
31+
return err
32+
}
33+
34+
func CreateToken(key string, claims jwt.Claims) (string, error) {
35+
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
36+
return token.SignedString([]byte(key))
37+
}

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/cardboardrobots/token
2+
3+
go 1.23.0
4+
5+
require github.com/golang-jwt/jwt/v4 v4.5.0

go.sum

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

0 commit comments

Comments
 (0)