mysql

package module
v0.0.0-...-af40f4d Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2020 License: MIT Imports: 9 Imported by: 0

README

go-oauth2-mysql

MySQL storage for OAuth 2.0 Provides both client and token store.

Build Codecov ReportCard GoDoc License

Install

$ go get -u -v github.com/vnzernov/go-oauth2-mysql

Usage

package main

import (
	"github.com/vnzernov/go-oauth2-mysql"
	"gopkg.in/oauth2.v3/manage"

	_ "github.com/go-sql-driver/mysql"
)

func main() {
	manager := manage.NewDefaultManager()
	dsn := "root:123456@tcp(127.0.0.1:3306)/myapp_test?charset=utf8"
	// use mysql token store
	store := mysql.NewDefaultStore(
		mysql.NewConfig(dsn),
	)

	defer store.Close()
	// use mysql client store
	clientStore := mysql.NewClientDefaultStore(
		mysql.NewConfig(dsn),
	)

	defer clientStore.Close()

	clientStore.Set(&models.Client{
		ID:     "000000",
		Secret: "999999",
		Domain: "http://localhost",
	})

	manager.MapTokenStorage(store)
	manager.MapClientStorage(clientStore)

// ...
}

Credits

Based on https://github.com/go-oauth2/mysql/

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClientStore

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

ClientStore mysql client store

func NewClientDefaultStore

func NewClientDefaultStore(config *Config) *ClientStore

NewClientDefaultStore create mysql store instance

func NewClientStore

func NewClientStore(config *Config, tableName string, gcInterval int) *ClientStore

NewClientStore create mysql store instance, config mysql configuration, tableName table name (default oauth2_client), GC time interval (in seconds, default 600)

func NewClientStoreWithDB

func NewClientStoreWithDB(db *sql.DB, tableName string, gcInterval int) *ClientStore

NewClientStoreWithDB create mysql store instance, db sql.DB, tableName table name (default oauth2_token), GC time interval (in seconds, default 600)

func (*ClientStore) Close

func (s *ClientStore) Close() error

Close close the store

func (*ClientStore) Create

func (s *ClientStore) Create(info oauth2.ClientInfo) error

Create creates and stores the new client information

func (*ClientStore) Delete

func (s *ClientStore) Delete(id string) error

Delete creates and stores the new client information

func (*ClientStore) GetByID

func (s *ClientStore) GetByID(id string) (oauth2.ClientInfo, error)

GetByID retrieves and returns client information by id

func (*ClientStore) SetStdout

func (s *ClientStore) SetStdout(stdout io.Writer) *ClientStore

SetStdout set error output

type ClientStoreItem

type ClientStoreItem struct {
	ID     string `db:"id,primarykey"`
	Secret string `db:"secret"`
	Domain string `db:"domain"`
	Data   string `db:"data"`
}

ClientStoreItem data item

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

type Store

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

Store mysql token store

func NewDefaultStore

func NewDefaultStore(config *Config) *Store

NewDefaultStore create mysql store instance

func NewStore

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

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

func NewStoreWithDB

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

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

func (*Store) Close

func (s *Store) Close()

Close close the store

func (*Store) Create

func (s *Store) Create(info oauth2.TokenInfo) error

Create create and store the new token information

func (*Store) GetByAccess

func (s *Store) GetByAccess(access string) (oauth2.TokenInfo, error)

GetByAccess use the access token for token information data

func (*Store) GetByCode

func (s *Store) GetByCode(code string) (oauth2.TokenInfo, error)

GetByCode use the authorization code for token information data

func (*Store) GetByRefresh

func (s *Store) GetByRefresh(refresh string) (oauth2.TokenInfo, error)

GetByRefresh use the refresh token for token information data

func (*Store) RemoveByAccess

func (s *Store) RemoveByAccess(access string) error

RemoveByAccess use the access token to delete the token information

func (*Store) RemoveByCode

func (s *Store) RemoveByCode(code string) error

RemoveByCode delete the authorization code

func (*Store) RemoveByRefresh

func (s *Store) RemoveByRefresh(refresh string) error

RemoveByRefresh use the refresh token to delete the token information

func (*Store) SetStdout

func (s *Store) SetStdout(stdout io.Writer) *Store

SetStdout set error output

type StoreItem

type StoreItem struct {
	ID        int64  `db:"id,primarykey,autoincrement"`
	ExpiredAt int64  `db:"expired_at"`
	Code      string `db:"code,size:255"`
	Access    string `db:"access,size:255"`
	Refresh   string `db:"refresh,size:255"`
	Data      string `db:"data,size:2048"`
}

StoreItem data item

Jump to

Keyboard shortcuts

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