pg

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2020 License: MIT Imports: 9 Imported by: 1

README

PostgreSQL Storage for OAuth 2.0

Build Codecov ReportCard GoDoc License

Install

$ go get -u -v github.com/vgarvardt/go-oauth2-pg

PostgreSQL drivers

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

Usage example

package main

import (
	"context"
	"os"
	"time"

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

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)
	// ...
}

How to run tests

You will need running PostgreSQL instance. E.g. the one running in docker and exposing a port to a host system

docker run --rm -p 5432:5432 -it -e POSTGRES_PASSWORD=oauth2 -e POSTGRES_USER=oauth2 -e POSTGRES_DB=oauth2 postgres:10

Now you can run tests using the running PostgreSQL instance using PG_URI environment variable

PG_URI=postgres://oauth2:oauth2@localhost:5432/oauth2?sslmode=disable go test -cover ./...

MIT License

Copyright (c) 2019 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 PostgreSQL client store

func NewClientStore

func NewClientStore(adapter pgadapter.Adapter, options ...ClientStoreOption) (*ClientStore, error)

NewClientStore creates PostgreSQL store instance

func (*ClientStore) Create

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

Create 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

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 PostgreSQL store logger interface

type TokenStore

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

TokenStore PostgreSQL token store

func NewTokenStore

func NewTokenStore(adapter pgadapter.Adapter, options ...TokenStoreOption) (*TokenStore, error)

NewTokenStore creates PostgreSQL store instance

func (*TokenStore) Close

func (s *TokenStore) Close() error

Close close the store

func (*TokenStore) Create

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

Create creates and stores the new token information

func (*TokenStore) GetByAccess

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

GetByAccess uses the access token for token information data

func (*TokenStore) GetByCode

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

GetByCode uses the authorization code for token information data

func (*TokenStore) GetByRefresh

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

GetByRefresh uses the refresh token for token information data

func (*TokenStore) RemoveByAccess

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

RemoveByAccess uses the access token to delete the token information

func (*TokenStore) RemoveByCode

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

RemoveByCode deletes the authorization code

func (*TokenStore) RemoveByRefresh

func (s *TokenStore) RemoveByRefresh(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