pg

package module
v3.3.2 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2020 License: MIT Imports: 9 Imported by: 0

README

PostgreSQL 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 gopkg.in/oauth2.v3:

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

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/jackc/pgx/v4"
	pg "github.com/vgarvardt/go-oauth2-pg/v3"
	"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)
	// ...
}

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 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