Code Monkey home page Code Monkey logo

golang-oauth's Introduction

Golang Oauth 2.0 with JWT custom server with example

Build Go Report Card GoDoc 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

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.

Example

For easy scaffold and full working REST API example made with framework gin-gonic/gin is included in example implementing this package. Postman Collection

Installation

$ go get -u -v github.com/gobeam/golang-oauth

Initialization

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()
}

Create Client

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)

Create Access Token

Visit oauthMiddleware.go to get full example on how to handle creating access token and refresh token.

Revoke Access/Refresh Token manually

  /*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)

Clear All Access Token Of User

  /* you can also clear all token related to
  user by passing TokenInfo from valid token */
  store.ClearByAccessToken(userId)

Running the tests

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

Contributing

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.

License

Released under the MIT License - see LICENSE.txt for details.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.