mq

package module
v4.0.0-...-a63b7a4 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2023 License: MIT Imports: 10 Imported by: 0

README

Mysql Storage for OAuth 2.0

GoDoc Coverage Status ReportCard License

Install

Storage major version matches OAuth 2.0 major version, so use corresponding version (go modules compliant)

For github.com/go-oauth2/oauth2/v4:

go get -u github.com/vgarvardt/go-oauth2-pg/v4

For gopkg.in/oauth2.v3 see v3 branch.

PostgreSQL drivers

The store accepts an adapter interface that interacts with the DB. Adapter and implementations extracted to separate package github.com/vgarvardt/go-pg-adapter for easier maintenance.

Usage example

package main

import (
  "context"
  "os"
  "time"

  "github.com/go-oauth2/oauth2/v4/manage"
  "github.com/jackc/pgx/v4"
  pg "github.com/vgarvardt/go-oauth2-pg/v4"
  "github.com/vgarvardt/go-pg-adapter/pgx4adapter"
)

func main() {
  pgxConn, _ := pgx.Connect(context.TODO(), os.Getenv("DB_URI"))

  manager := manage.NewDefaultManager()

  // use PostgreSQL token store with pgx.Connection adapter
  adapter := pgx4adapter.NewConn(pgxConn)
  tokenStore, _ := pg.NewTokenStore(adapter, pg.WithTokenStoreGCInterval(time.Minute))
  defer tokenStore.Close()

  clientStore, _ := pg.NewClientStore(adapter)

  manager.MapTokenStorage(tokenStore)
  manager.MapClientStorage(clientStore)
  // ...
}

Testing

Linter and tests are running for every Pul Request, but it is possible to run linter and tests locally using docker and make.

Run linter: make link. This command runs liner in docker container with the project source code mounted.

Run tests: make test. This command runs project dependencies in docker containers if they are not started yet and runs go tests with coverage.

MIT License

Copyright (c) 2020 Vladimir Garvardt

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 Mariadb client store

func NewClientStore

func NewClientStore(db *sqlx.DB, options ...ClientStoreOption) (*ClientStore, error)

NewClientStore creates Mariadb store instance

func (*ClientStore) Create

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

Create creates and stores the n ew client information

func (*ClientStore) GetByID

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

GetByID retrieves and returns client information by id

type ClientStoreItem

type ClientStoreItem struct {
	ID     string `db:"id"`
	Secret string `db:"secret"`
	Domain string `db:"domain"`
	Data   []byte `db:"data"`
}

ClientStoreItem data item

type ClientStoreOption

type ClientStoreOption func(s *ClientStore)

ClientStoreOption is the configuration options type for client store

func WithClientStoreInitTableDisabled

func WithClientStoreInitTableDisabled() ClientStoreOption

WithClientStoreInitTableDisabled returns option that disables table creation on client store instantiation

func WithClientStoreLogger

func WithClientStoreLogger(logger Logger) ClientStoreOption

WithClientStoreLogger returns option that sets client store logger implementation

func WithClientStoreTableName

func WithClientStoreTableName(tableName string) ClientStoreOption

WithClientStoreTableName returns option that sets client store table name

type Logger

type Logger interface {
	Printf(format string, v ...interface{})
}

Logger is the Mysql store logger interface

type TokenStore

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

TokenStore Mariadb token store

func NewTokenStore

func NewTokenStore(db *sqlx.DB, options ...TokenStoreOption) (*TokenStore, error)

NewTokenStore creates Mysql store instance

func (*TokenStore) Close

func (s *TokenStore) Close() error

Close closes the store

func (*TokenStore) Create

func (s *TokenStore) Create(ctx context.Context, info oauth2.TokenInfo) error

Create creates and stores the new token information

func (*TokenStore) GetByAccess

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

GetByAccess uses the access token for token information data

func (*TokenStore) GetByCode

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

GetByCode uses the authorization code for token information data

func (*TokenStore) GetByRefresh

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

GetByRefresh uses the refresh token for token information data

func (*TokenStore) RemoveByAccess

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

RemoveByAccess uses the access token to delete the token information

func (*TokenStore) RemoveByCode

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

RemoveByCode deletes the authorization code

func (*TokenStore) RemoveByRefresh

func (s *TokenStore) RemoveByRefresh(ctx context.Context, refresh string) error

RemoveByRefresh uses the refresh token to delete the token information

type TokenStoreItem

type TokenStoreItem struct {
	ID        int64     `db:"id"`
	CreatedAt time.Time `db:"created_at"`
	ExpiresAt time.Time `db:"expires_at"`
	Code      string    `db:"code"`
	Access    string    `db:"access"`
	Refresh   string    `db:"refresh"`
	Data      []byte    `db:"data"`
}

TokenStoreItem data item

type TokenStoreOption

type TokenStoreOption func(s *TokenStore)

TokenStoreOption is the configuration options type for token store

func WithTokenStoreGCDisabled

func WithTokenStoreGCDisabled() TokenStoreOption

WithTokenStoreGCDisabled returns option that disables token store garbage collection

func WithTokenStoreGCInterval

func WithTokenStoreGCInterval(gcInterval time.Duration) TokenStoreOption

WithTokenStoreGCInterval returns option that sets token store garbage collection interval

func WithTokenStoreInitTableDisabled

func WithTokenStoreInitTableDisabled() TokenStoreOption

WithTokenStoreInitTableDisabled returns option that disables table creation on token store instantiation

func WithTokenStoreLogger

func WithTokenStoreLogger(logger Logger) TokenStoreOption

WithTokenStoreLogger returns option that sets token store logger implementation

func WithTokenStoreTableName

func WithTokenStoreTableName(tableName string) TokenStoreOption

WithTokenStoreTableName returns option that sets token store table name

Jump to

Keyboard shortcuts

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