An authentication and RBAC authorization library using JWT for Go 1.7+
- Simple and well-tested API
- Exported flexible contracts
- Developer friendly
- Persistence free
- Password-based authentication
- OAuth2
go get github.com/hiendv/gate
Quick example to get a taste of Gate
var auth gate.Auth
var user gate.User
var err error
// some construction codes go here
// Login using password-based authentication
user, err = auth.Login(map[string]string{"email": "email", "password": "password"})
if err != nil {
log.Fatal("oops")
}
// Login using OAuth
// Redirect users to the authentication code URL
url, err := auth.LoginURL("state")
// Receive the code and exchange it
user, err = auth.Login(map[string]string{"code": "received-code"})
if err != nil {
log.Fatal("oops")
}
// Issue the JWT for the user
jwt, err := auth.IssueJWT(user)
if err != nil {
log.Fatal("oops")
}
// Send the JWT to the user and let them use it to authenticate
// Authenticate a user using a given JWT
user, err = auth.Authenticate("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImVtYWlsQGxvY2FsIiwicm9sZXMiOlsicm9sZSJdLCJleHAiOjE2MDUwNTI4MDAsImp0aSI6ImNsYWltcy1pZCIsImlhdCI6MTYwNTA0OTIwMCwic3ViIjoiaWQifQ.wRouDwptboRBSK-bXHugYeorWGy7pfUHstH_jEHKl_4")
if err != nil {
log.Fatal("oops")
}
err = auth.Authorize(user, "action", "object")
You may want to check these examples and tests:
- Password-based authentication examples, unit tests & integration tests
- OAuth2 authentication examples, unit tests & integration tests
Please check the Contributing Guidelines.
Issues and PRs are welcome !
The Gate bouncer logo is licensed under the Creative Commons 4.0 Attributions license.
The original gopher.svg was created by Takuya Ueda, licensed under the Creative Commons 3.0 Attributions license.
The Go Gopher was designed by Renee French, licensed under the Creative Commons 3.0 Attributions license.
Big thanks to:
- dgrijalva/jwt-go for the enormous help dealing with JWT works
- rs/xid for the claims ID generator