pgstore

package module
v0.0.0-...-958b9ea Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2023 License: MIT Imports: 9 Imported by: 1

README

go-oauth2-pg

GoDoc Go Report Card Maintainability Test Coverage

This package is a Postgres storage implementation for go-oauth2 using pgx.

The package is following semantic versioning and is not tied to the versioning of go-oauth2.

Installation

go get github.com/gabor-boros/go-oauth2-pg

Example usage

package main

import (
	"context"
	"os"

	arangoDriver "github.com/pg/go-driver"
	arangoHTTP "github.com/pg/go-driver/http"

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

	pgstore "github.com/gabor-boros/go-oauth2-pg"
)

func main() {
	conn, _ := arangoHTTP.NewConnection(arangoHTTP.ConnectionConfig{
		Endpoints: []string{os.Getenv("ARANGO_URL")},
	})

	client, _ := arangoDriver.NewClient(arangoDriver.ClientConfig{
		Connection:     conn,
		Authentication: arangoDriver.BasicAuthentication(os.Getenv("ARANGO_USER"), os.Getenv("ARANGO_PASSWORD")),
	})

	db, _ := client.Database(context.Background(), os.Getenv("ARANGO_DB"))
	
	clientStore, _ := pgstore.NewClientStore(
		pgstore.WithClientStoreDatabase(db),
		pgstore.WithClientStoreTable("oauth2_clients"),
	)

	tokenStore, _ := pgstore.NewTokenStore(
		pgstore.WithTokenStoreDatabase(db),
		pgstore.WithTokenStoreTable("oauth2_tokens"),
	)

	manager := manage.NewDefaultManager()
	manager.MapTokenStorage(tokenStore)
	manager.MapClientStorage(clientStore)
	
	// ...
}

Contributing

Contributions are welcome! Please open an issue or a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

View Source
const (
	LogLevelDebug = LogLevel("DEBUG") // debug level
	LogLevelInfo  = LogLevel("INFO")  // info level
	LogLevelWarn  = LogLevel("WARN")  // warn level
	LogLevelError = LogLevel("ERROR") // error level
)
View Source
const (
	// DefaultClientStoreTable is the default collection for storing clients.
	DefaultClientStoreTable = "oauth2_clients"
)
View Source
const (
	// DefaultTokenStoreTable is the default collection for storing tokens.
	DefaultTokenStoreTable = "oauth2_tokens" // nolint: gosec
)

Variables

View Source
var (
	// ErrNoTable is returned when no table was provided.
	ErrNoTable = fmt.Errorf("no table provided")
	// ErrNoConnPool is returned when no database was provided.
	ErrNoConnPool = fmt.Errorf("no connection pool provided")
	// ErrNoLogger is returned when no logger was provided.
	ErrNoLogger = fmt.Errorf("no logger provided")
)

Functions

This section is empty.

Types

type ClientStore

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

ClientStore is a data struct that stores oauth2 client information.

func NewClientStore

func NewClientStore(opts ...ClientStoreOption) (*ClientStore, error)

NewClientStore creates a new ClientStore.

func (*ClientStore) Create

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

Create creates a new client in the store.

func (*ClientStore) GetByID

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

GetByID returns the client information by key from the store.

func (*ClientStore) InitTable

func (s *ClientStore) InitTable(ctx context.Context) error

InitTable initializes the client store table if it does not exist and creates the indexes.

type ClientStoreItem

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

ClientStoreItem data item

type ClientStoreOption

type ClientStoreOption func(*ClientStore) error

ClientStoreOption is a function that configures the ClientStore.

func WithClientStoreConnPool

func WithClientStoreConnPool(pool *pgxpool.Pool) ClientStoreOption

WithClientStoreConnPool configures the connection pool.

func WithClientStoreLogger

func WithClientStoreLogger(logger Logger) ClientStoreOption

WithClientStoreLogger configures the logger.

func WithClientStoreTable

func WithClientStoreTable(table string) ClientStoreOption

WithClientStoreTable configures the auth token table.

type LogLevel

type LogLevel string

LogLevel is a log level.

type Logger

type Logger interface {
	// Log logs a message.
	Log(ctx context.Context, level LogLevel, msg string, args ...any)
}

Logger wraps a logger to log messages.

type NoopLogger

type NoopLogger struct{}

NoopLogger is a logger that does nothing.

func (*NoopLogger) Log

func (l *NoopLogger) Log(_ context.Context, level LogLevel, msg string, args ...any)

Log logs a message.

type TokenStore

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

TokenStore is a data struct that stores oauth2 token information.

func NewTokenStore

func NewTokenStore(opts ...TokenStoreOption) (*TokenStore, error)

NewTokenStore creates a new TokenStore.

func (*TokenStore) Close

func (s *TokenStore) Close(ctx context.Context)

Close closes the store and releases any resources.

func (*TokenStore) Create

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

Create creates a new token in the store.

func (*TokenStore) GetByAccess

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

GetByAccess returns the token by its access token.

func (*TokenStore) GetByCode

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

GetByCode returns the token by its authorization code.

func (*TokenStore) GetByRefresh

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

GetByRefresh returns the token by its refresh token.

func (*TokenStore) InitCleanup

func (s *TokenStore) InitCleanup(ctx context.Context)

InitCleanup initializes the cleanup process.

func (*TokenStore) InitTable

func (s *TokenStore) InitTable(ctx context.Context) error

InitTable initializes the token store table if it does not exist and creates the indexes.

func (*TokenStore) RemoveByAccess

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

func (*TokenStore) RemoveByCode

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

RemoveByCode deletes the token by its authorization code.

func (*TokenStore) RemoveByRefresh

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

type TokenStoreItem

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

TokenStoreItem data item

type TokenStoreOption

type TokenStoreOption func(*TokenStore) error

TokenStoreOption is a function that configures the TokenStore.

func WithTokenStoreCleanupInterval

func WithTokenStoreCleanupInterval(interval time.Duration) TokenStoreOption

WithTokenStoreCleanupInterval configures the cleanup interval.

func WithTokenStoreConnPool

func WithTokenStoreConnPool(pool *pgxpool.Pool) TokenStoreOption

WithTokenStoreConnPool configures the connection pool.

func WithTokenStoreLogger

func WithTokenStoreLogger(logger Logger) TokenStoreOption

WithTokenStoreLogger configures the logger.

func WithTokenStoreTable

func WithTokenStoreTable(table string) TokenStoreOption

WithTokenStoreTable configures the auth token table.

Jump to

Keyboard shortcuts

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