https://github.com/fmorenovr/gojwt
goJweto (Golang JSON Web Token) is a Golang implementation for REST service security.
https://github.com/fmorenovr/gojwt
golang golang-package jwt jwt-auth jwt-authentication jwt-go jwt-middleware jwt-server jwt-token jwt-tokens
Last synced: 4 months ago
JSON representation
goJweto (Golang JSON Web Token) is a Golang implementation for REST service security.
- Host: GitHub
- URL: https://github.com/fmorenovr/gojwt
- Owner: fmorenovr
- License: mit
- Created: 2017-10-19T15:53:38.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-08-19T17:36:26.000Z (almost 7 years ago)
- Last Synced: 2026-01-12T05:37:13.656Z (5 months ago)
- Topics: golang, golang-package, jwt, jwt-auth, jwt-authentication, jwt-go, jwt-middleware, jwt-server, jwt-token, jwt-tokens
- Language: Go
- Homepage: https://godoc.org/github.com/fmorenovr/gojwt
- Size: 60.5 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
# golang + JWT = goJwt (GoJweto)
goJwt (Golang for JSON Web Token) is a Golang implementation for REST service security.
You can see an extended doc in [godocs](https://godoc.org/github.com/fmorenovr/gojwt).
## JWT
JWT (JSON Web Token) is a standard to make secure a connection in a compact URL-safe means of representing claims to be transferred between two parties.
See more info [here](https://jwt.io).
## goJwt
* First, You should create your RSA key pairs.
Create `/tls-ssl/jwtkeys/` directory in your root path of your project:
cd jwt/keys
openssl genrsa -out rsakey.pem 2048
openssl rsa -in rsakey.pem -pubout > rsakey.pem.pub
* Or You should create your ECDSA key pairs.
Create `/tls-ssl/jwtkeys/` directory in your root path of your project:
* First, select a curve list:
openssl ecparam -list_curves
* Then, select secp256r1 or secp384r1:
cd jwt/keys
openssl ecparam -genkey -name secp384r1 | sed -e '1,3d' > ecdsakey.pem
openssl ec -in ecdsakey.pem -pubout > ecdsakey.pem.pub
* Next, You should download my library:
go get github.com/fmorenovr/gojwt/
* Then, you should use for differents implements in Go.
* First, Create a HMAC_SHA gojwt object, specifying nameServer, headerAuth in request, secretKey, bytes, and expiration time (in hours).
var GojwtObject, _ = gojwt.NewGojwtHMAC_SHA("JnzadsServer", "jnzads-rest", "Jnzads-rest-JWT", "512", 24)
* Or a RSA/ECDA Object, specifying nameServer, headerAuth in request, privKeypath, pubKeyPath, bytes, and expiration time (in hours).
var GojwtObject, _ = gojwt.NewGojwtRSA("JnzadsServer", "Jnzads-rest-JWT", privKeyPath, pubKeyPath, "384", 24)
var GojwtObject, _ = gojwt.NewGojwtECDSA("JnzadsServer", "Jnzads-rest-JWT", privKeyPath, pubKeyPath, "256", 24)
* Then, generate the token string specifyind a nameserver and username:
tokenString, _ := GojwtObject.CreateToken(Username)
* Using in Go net/http package:
* Add `examples/goJwtHandler.go` in your controllers directory.
* Then, in your muxServe add:
```go
muxHttp.HandleFunc("/setToken", setTokenHandler)
muxHttp.HandleFunc("/login", LoginHandler)
muxHttp.HandleFunc("/profile", gojwt.MiddlewareGojwtHeaders(WithAuthHandler, NoAuthHandler))
```
* Using in BeeGo:
* Add `examples/goJwtBeeGoController.go` in your controllers directory.
* And, in other controllers, add your new controller instead beegoController.
```go
import (
"encoding/json";
"restfulapi-beego/models";
//"github.com/astaxie/beego";
)
type AlertController struct {
//beego.Controller
GoJwtController
}
```