Build your own Golang custom Oauth 2.0 server. This package helps you to develop your own custom oauth2 server. With lots of scaffolding done for you you can easily implement your own logic without any hassle.
Official docs: Here
- Why?
- Example
- Installation
- Initialization
- Create Client
- Create Access Token
- Revoke Access/Refresh Token manually
- Clear All Access Token Of User
- Running the tests
- Contributing
- License
I was trying to make my own modified version of OAUTH2 alongside with JWT server and didn't find any good package so, I made one. This project is modified version of go-oauth2/oauth2. since this project didn't meet my requirement .
This package uses EncryptOAEP which encrypts the given data with RSA-OAEP to encrypt token data. Two separate file private.pem and public.pem file will be created on your root folder which includes respective private and public RSA keys which is used for encryption.
For easy scaffold and full working REST API example made with framework gin-gonic/gin is included in example implementing this package. Postman Collection
$ go get -u -v github.com/gobeam/golang-oauth
Easy to initialize just by:
package main
import (
_ "github.com/go-sql-driver/mysql"
oauth "github.com/roshanr83/go-oauth2"
)
func main() {
//register store
store := oauth.NewDefaultStore(
oauth.NewConfig("root:root@tcp(127.0.0.1:8889)/goauth?charset=utf8&parseTime=True&loc=Local"),
)
defer store.Close()
}
To create client where 1 is user ID Which will return Oauth Clients struct which include client id and secret which is later used to validate client credentials
var userId = 1 // to know who created can be 0
var clientName = "my app" // app name can be empty string
store.CreateClient(userId, clientName)
Visit oauthMiddleware.go to get full example on how to handle creating access token and refresh token.
/*You can manually revoke access token by passing
userId which you can get from valid token info */
store.RevokeByAccessTokens(userId)
/*You can manually revoke refresh token by passing
accessTokenId which you can get from valid token info */
store.RevokeRefreshToken(accessTokenId)
/* you can also clear all token related to
user by passing TokenInfo from valid token */
store.ClearByAccessToken(userId)
Database config is used as "root:root@tcp(127.0.0.1:3306)/goauth?charset=utf8&parseTime=True&loc=Local" in const.go file, You may have to change that configuration according to your system config for successful test.
$ go test
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate.
Released under the MIT License - see LICENSE.txt
for details.