https://github.com/zekrotja/jwt
A very simplistic implementation of JWT taking advantage of Go 1.18 generic type parameters for parsing claim objects.
https://github.com/zekrotja/jwt
generic go go118 hacktoberfest jwt
Last synced: about 2 months ago
JSON representation
A very simplistic implementation of JWT taking advantage of Go 1.18 generic type parameters for parsing claim objects.
- Host: GitHub
- URL: https://github.com/zekrotja/jwt
- Owner: zekroTJA
- License: mit
- Created: 2022-04-24T14:17:42.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2023-10-09T11:41:47.000Z (over 1 year ago)
- Last Synced: 2025-04-02T17:53:31.237Z (about 2 months ago)
- Topics: generic, go, go118, hacktoberfest, jwt
- Language: Go
- Homepage:
- Size: 15.6 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jwt
This is a very simplistic implementation of JWT using hashing algorithms like `HS256` or `HS512` and taking advantage of Go 1.18 generic type parameters for parsing claim objects.
This package is very much inspired and influenced by [robbert229's JWT implementation](https://github.com/robbert229/jwt). [Here](https://github.com/robbert229/jwt/blob/master/LICENSE) you can find the projects License.
## Usage
```go
const signingSecret = "3U5o3Z#XqfLpr3pjGknwWa^u6)CCo&&G"algorithm := jwt.NewHmacSha512([]byte(signingSecret))
handler := jwt.NewHandler[Claims](algorithm)claims := new(Claims)
claims.UserID = "221905671296253953"
claims.Iss = "jwt example"
claims.SetIat()
claims.SetExpDuration(15 * time.Minute)
claims.SetNbfTime(time.Now())token, err := handler.EncodeAndSign(*claims)
if err != nil {
log.Fatalf("Token generation failed: %s", err.Error())
}log.Printf("Token generated: %s", token)
recoveredClaims, err := handler.DecodeAndValidate(token)
if err != nil {
log.Fatalf("Token validation failed: %s", err.Error())
}log.Printf("Recovered claims: %+v", recoveredClaims)
```Go to [example](example) to see the full example.