golang_oauth

package module
v0.0.0-...-0ae9f9e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2020 License: MIT Imports: 15 Imported by: 4

README

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	DSN          string
	MaxLifetime  time.Duration
	MaxOpenConns int
	MaxIdleConns int
}

Config mysql configuration

func NewConfig

func NewConfig(dsn string) *Config

NewConfig create mysql configuration instance, dsn mysql database credential

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store mysql token store model

func NewDefaultStore

func NewDefaultStore(config *Config) *Store

NewDefaultStore create mysql store instance, config mysql configuration,

func NewStore

func NewStore(config *Config, gcInterval int) *Store

NewStore create mysql store instance, config mysql configuration, GC time interval (in seconds, default 600)

func NewStoreWithDB

func NewStoreWithDB(db *sql.DB, gcInterval int) *Store

NewStoreWithDB create mysql store instance, db sql.DB, GC time interval (in seconds, default 600)

func (*Store) ClearByAccessToken

func (s *Store) ClearByAccessToken(userId int64) error

ClearByAccessToken clears all token related to user, userId id of user whose access token needs to be cleared

func (*Store) Close

func (s *Store) Close()

Close close the store

func (*Store) Create

func (s *Store) Create(info model.TokenInfo) (model.TokenResponse, error)

Create create and store the new token information

func (*Store) CreateClient

func (s *Store) CreateClient(userId int64, name string) (model.Clients, error)

CreateClient creates new client, userId user's id who created the client

func (*Store) GetByAccess

func (s *Store) GetByAccess(access string) (*model.AccessTokens, error)

GetByAccess use the access token for token information data, access Access token string

func (*Store) GetByRefresh

func (s *Store) GetByRefresh(refresh string) (*model.AccessTokens, error)

GetByRefresh use the refresh token for token information data, refresh Refresh token string

func (*Store) RevokeByAccessTokens

func (s *Store) RevokeByAccessTokens(userId int64) error

RevokeByAccessTokens revokes token from accessToken

func (*Store) RevokeRefreshToken

func (s *Store) RevokeRefreshToken(accessTokenId string) error

RevokeRefreshToken revokes token from RefreshToken,

Directories

Path Synopsis
example module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL