Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nilslice/jwt
simple JWT package to create, parse and verify tokens for client authentication
https://github.com/nilslice/jwt
go golang jwt
Last synced: 2 days ago
JSON representation
simple JWT package to create, parse and verify tokens for client authentication
- Host: GitHub
- URL: https://github.com/nilslice/jwt
- Owner: nilslice
- License: mit
- Created: 2015-12-28T04:18:36.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2018-11-05T16:27:44.000Z (almost 6 years ago)
- Last Synced: 2024-06-18T20:15:15.214Z (4 months ago)
- Topics: go, golang, jwt
- Language: Go
- Size: 9.77 KB
- Stars: 35
- Watchers: 3
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JWT
### Usage
$ go get github.com/nilslice/jwtpackage jwt provides methods to create and check JSON Web Tokens. It only implements HMAC 256 encryption and has a very small footprint, ideal for simple usage when authorizing clients
```go
package mainimport (
auth "github.com/nilslice/jwt"
"fmt"
"net/http"
"strings"
)func main() {
http.HandleFunc("/auth/new", func(res http.ResponseWriter, req *http.Request) {
claims := map[string]interface{}{"exp": time.Now().Add(time.Hour * 24).Unix()}
token, err := auth.New(claims)
if err != nil {
http.Error(res, "Error", 500)
return
}
res.Header().Add("Authorization", "Bearer "+token)res.WriteHeader(http.StatusOK)
})http.HandleFunc("/auth", func(res http.ResponseWriter, req *http.Request) {
userToken := strings.Split(req.Header.Get("Authorization"), " ")[1]if auth.Passes(userToken) {
fmt.Println("ok")
} else {
fmt.Println("no")
}
})http.ListenAndServe(":8080", nil)
}
```