dbstore

package module
v4.0.5 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2022 License: MIT Imports: 10 Imported by: 0

README

Database storage for OAuth 2.0

Inherited from MySQL storage for OAuth 2.0

License

Install

$ go get -v github.com/iodeal/go-oauth2-database-store/v4

Usage

package main

import (
	"context"
	"fmt"
	"time"

	dbstore "github.com/iodeal/go-oauth2-database-store/v4"

	"github.com/go-oauth2/oauth2/v4/models"

	"github.com/go-oauth2/oauth2/v4/manage"

	_ "github.com/lib/pq"
)

func main() {
	manager := manage.NewDefaultManager()

	// use mysql token store
	store := dbstore.NewDefaultStore(
		dbstore.NewConfig("postgres", "host=localhost port=5432 user=postgres password=123456 dbname=postgres sslmode=disable"),
	)

	defer store.Close()

	manager.MapTokenStorage(store)
	info := &models.Token{
		ClientID:      "123",
		UserID:        "adb",
		RedirectURI:   "http://localhost/",
		Scope:         "all",
		Code:          "12_34_56",
		CodeCreateAt:  time.Now(),
		CodeExpiresIn: time.Second * 5,
	}
	err := store.Create(context.TODO(), info)
	if err != nil {
		log.Fatal("create store err:", err)
	} else {
		fmt.Println("create ok!")
	}
}

MIT License

Copyright (c) 2018 Lyric
Copyright (c) 2022 iodeal

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

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

Config database configuration

func NewConfig

func NewConfig(driverName, dsn string) *Config

NewConfig create database configuration instance

type Option

type Option interface {
	// contains filtered or unexported methods
}

func WithGCTimeInterval

func WithGCTimeInterval(interval int) Option

WithGCTimeInterval sets the time interval for garbage collection.

func WithTableName

func WithTableName(tableName string) Option

WithTableName sets the table name for the store.

type Store

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

Store database token store

func NewDefaultStore

func NewDefaultStore(config *Config) *Store

NewDefaultStore create database store instance

func NewStore

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

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

func NewStoreWithDB

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

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

func NewStoreWithOpts

func NewStoreWithOpts(db *sqlx.DB, opts ...Option) *Store

NewStoreWithOpts create database store instance with apply custom input, 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(ctx context.Context, info oauth2.TokenInfo) error

Create create and store the new token information

func (*Store) GetByAccess

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

GetByAccess use the access token for token information data

func (*Store) GetByCode

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

GetByCode use the authorization code for token information data

func (*Store) GetByRefresh

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

GetByRefresh use the refresh token for token information data

func (*Store) RemoveByAccess

func (s *Store) RemoveByAccess(ctx context.Context, access string) error

RemoveByAccess use the access token to delete the token information

func (*Store) RemoveByCode

func (s *Store) RemoveByCode(ctx context.Context, code string) error

RemoveByCode delete the authorization code

func (*Store) RemoveByRefresh

func (s *Store) RemoveByRefresh(ctx context.Context, 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"`
	ExpiredAt int64  `db:"expired_at"`
	Code      string `db:"code"`
	Access    string `db:"access"`
	Refresh   string `db:"refresh"`
	Data      string `db:"data"`
}

StoreItem data item

Jump to

Keyboard shortcuts

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