mongo

package module
v4.0.38 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2024 License: MIT Imports: 12 Imported by: 0

README

Mongo Storage for OAuth 2.0

Build Codecov ReportCard GoDoc License

Install

$ go get -u -v gopkg.in/go-oauth2/mongo.v3

Usage

import(
    "github.com/go-oauth2/oauth2/v4/manage"
    "github.com/go-oauth2/oauth2/v4/server"
    mongo "gopkg.in/go-oauth2/mongo.v3"    
)

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

    /*
	* only for a MongoDB replicaSet deployment
    * Using a replicaSet is recommended as it allows for MongoDB's native support for transactions
    **/
	// mongoConf := mongo.NewConfigReplicaSet(
	// 	"mongodb://localhost:27017,localhost:28017,localhost:29017/?replicaSet=myReplicaSet",
	// 	"oauth2",
	// )

	// set connectionTimeout(7s) and the requestsTimeout(5s) // is optional
	storeConfigs := mongo.NewStoreConfig(7, 5)

    /*
	* for a single mongoDB node
	* if the oauth2 service is deployed with more than one instance
	* each mongoConf should have unique serviceName
    **/
	mongoConf := mongo.NewConfigNonReplicaSet(
		"mongodb://127.0.0.1:27017",
		"oauth2",   // database name
		"admin",    // username to authenticate with db
		"password", // password to authenticate with db
		"serviceName",
	)

	// use mongodb token store
	manager.MapTokenStorage(
		mongo.NewTokenStore(mongoConf, storeConfigs), // with timeout
		// mongo.NewTokenStore(mongoConf), // no timeout
	)

	clientStore := mongo.NewClientStore(mongoConf, storeConfigs) // with timeout
	// clientStore := mongo.NewClientStore(mongoConf) // no timeout

	manager.MapClientStorage(clientStore)

	// register a service
	clientStore.Create(&models.Client{
		ID:     idvar,
		Secret: secretvar,
		Domain: domainvar,
		UserID: "frontend",
	})

	// register a second service
	clientStore.Create(&models.Client{
		ID:     idPreorder,
		Secret: secretPreorder,
		Domain: domainPreorder,
		UserID: "prePost",
	})

	srv := server.NewServer(server.NewConfig(), manager)

    // ...
}

MIT License

Copyright (c) 2016 Lyric

Documentation

Index

Constants

View Source
const (
	UserAgentKey = "User-Agent-Sn"
)

Variables

This section is empty.

Functions

func NewTransactionHandler

func NewTransactionHandler(client *mongo.Client, tcfg *TokenConfig) *transactionHandler

func NewTransactionWorker

func NewTransactionWorker(t *TokenConfig, cl *mongo.Client) *transactionWorker

Types

type ClientConfig

type ClientConfig struct {
	// store clients data collection name(The default is oauth2_clients)
	ClientsCName string
	// contains filtered or unexported fields
}

ClientConfig client configuration parameters

func NewDefaultClientConfig

func NewDefaultClientConfig(strCfgs *StoreConfig) *ClientConfig

NewDefaultClientConfig create a default client configuration

type ClientStore

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

ClientStore MongoDB storage for OAuth 2.0

func NewClientStore

func NewClientStore(cfg *Config, scfgs ...*StoreConfig) *ClientStore

NewClientStore create a client store instance based on mongodb

func NewClientStoreWithSession

func NewClientStoreWithSession(client *mongo.Client, cfg *Config, scfgs ...*StoreConfig) *ClientStore

NewClientStoreWithSession create a client store instance based on mongodb

func (*ClientStore) Close

func (cs *ClientStore) Close()

Close close the mongo session

func (*ClientStore) Create

func (cs *ClientStore) Create(info oauth2.ClientInfo) (err error)

Create create client information

func (*ClientStore) GetByID

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

GetByID according to the ID for the client information

func (*ClientStore) RemoveByID

func (cs *ClientStore) RemoveByID(id string) (err error)

RemoveByID use the client id to delete the client information

type Config

type Config struct {
	URL          string
	DB           string
	Username     string
	Password     string
	Service      string
	IsReplicaSet bool
}

Config mongodb configuration parameters

func NewConfigNonReplicaSet

func NewConfigNonReplicaSet(url, db, username, password, service string) *Config

NewConfigNonReplicaSet create mongodb configuration for a non-replicaSet

func NewConfigReplicaSet

func NewConfigReplicaSet(url, db string) *Config

NewConfigReplicaSet create mongodb configuration for a ReplicaSet

type OAuth2TokenUsageInfo

type OAuth2TokenUsageInfo struct {
	ID         string    `bson:"token_id"`
	UserID     string    `bson:"user_id"`
	ClientID   string    `bson:"ClientID,omitempty"`
	Device     string    `bson:"device_name,omitempty"`
	DeviceOS   string    `bson:"device_os,omitempty"`
	CreatedAt  time.Time `bson:"created_at,omitempty"`
	LastUsedAt time.Time `bson:"last_used_at,omitempty"`
	ExpiredAt  time.Time `bson:"expired_at,omitempty"`
}

type PersistentIDContextKey added in v4.0.12

type PersistentIDContextKey struct{}

PersistentIDContextKey Persistent ID key to get/set from context

type StoreConfig

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

StoreConfig hold configs common to all Configs(ClientConfig, TokenConfig)

func NewDefaultStoreConfig

func NewDefaultStoreConfig(db, service string, isReplicasSet bool) *StoreConfig

func NewStoreConfig

func NewStoreConfig(ctout, rtout int) *StoreConfig

type TokenConfig

type TokenConfig struct {
	// store txn collection name(The default is oauth2)
	TxnCName string
	// store token based data collection name(The default is oauth2_basic)
	BasicCName string
	// store access token data collection name(The default is oauth2_access)
	AccessCName string
	// store refresh token data collection name(The default is oauth2_refresh)
	RefreshCName string
	// contains filtered or unexported fields
}

TokenConfig token configuration parameters

func NewDefaultTokenConfig

func NewDefaultTokenConfig(strConfig *StoreConfig) *TokenConfig

NewDefaultTokenConfig create a default token configuration

type TokenStore

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

TokenStore MongoDB storage for OAuth 2.0

func NewTokenStore

func NewTokenStore(cfg *Config, scfgs ...*StoreConfig) (store *TokenStore)

NewTokenStore create a token store instance based on mongodb

func NewTokenStoreWithSession

func NewTokenStoreWithSession(client *mongo.Client, cfg *Config, scfgs ...*StoreConfig) (store *TokenStore)

NewTokenStoreWithSession create a token store instance based on mongodb

func (*TokenStore) Close

func (ts *TokenStore) Close()

Close close the mongo session

func (*TokenStore) Create

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

Create create and store the new token information

func (*TokenStore) GetByAccess

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

GetByAccess use the access token for token information data

func (*TokenStore) GetByCode

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

GetByCode use the authorization code for token information data

func (*TokenStore) GetByRefresh

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

GetByRefresh use the refresh token for token information data

func (*TokenStore) GetEntryIDOfToken added in v4.0.21

func (ts *TokenStore) GetEntryIDOfToken(ctx context.Context, tokenID string) (entryID string, err error)

GetEntryIDOfToken returns ID of the token entry in oauth2_basic collection

func (*TokenStore) GetTokenByAccess added in v4.0.21

func (ts *TokenStore) GetTokenByAccess(ctx context.Context, access string) (token *OAuth2TokenUsageInfo, err error)

GetTokenByAccess use the access token to return token Information

func (*TokenStore) GetTokenByRefresh added in v4.0.21

func (ts *TokenStore) GetTokenByRefresh(ctx context.Context, refresh string) (token *OAuth2TokenUsageInfo, err error)

GetTokenByRefresh use the refresh token return token Information

func (*TokenStore) GetTokenByTokenID added in v4.0.21

func (ts *TokenStore) GetTokenByTokenID(ctx context.Context, tokenID string) (token *OAuth2TokenUsageInfo, err error)

GetTokensByUserID use the token ID to return token Information

func (*TokenStore) GetTokensByUserID added in v4.0.21

func (ts *TokenStore) GetTokensByUserID(ctx context.Context, userID string) (tokens []*OAuth2TokenUsageInfo, err error)

GetTokensByUserID returns all tokens of the specified user

func (*TokenStore) RemoveByAccess

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

RemoveByAccess use the access token to delete the token information

func (*TokenStore) RemoveByCode

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

RemoveByCode use the authorization code to delete the token information

func (*TokenStore) RemoveByRefresh

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

RemoveByRefresh use the refresh token to delete the token information

func (*TokenStore) RemoveTokenByAccess added in v4.0.21

func (ts *TokenStore) RemoveTokenByAccess(ctx context.Context, access string) (err error)

RemoveTokenByAccess use the access token to delete the whole token

func (*TokenStore) RemoveTokenByRefresh added in v4.0.21

func (ts *TokenStore) RemoveTokenByRefresh(ctx context.Context, refresh string) (err error)

RemoveTokenByRefresh use the refresh token to delete the whole token

func (*TokenStore) RemoveTokenByTokenID added in v4.0.21

func (ts *TokenStore) RemoveTokenByTokenID(ctx context.Context, tokenID string) (err error)

RemoveTokenByTokenID use the token ID to delete the whole token

func (*TokenStore) RemoveTokensByUserID added in v4.0.21

func (ts *TokenStore) RemoveTokensByUserID(ctx context.Context, userID string) (err error)

RemoveTokensByUserID removes all tokens of the specified user

type TransactionWorker

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

type UIData

type UIData struct {
	Device     string    `json:"device_name" bson:"device,omitempty"`
	DeviceOS   string    `json:"device_os" bson:"os_type,omitempty"`
	CreatedAt  time.Time `bson:"created_at,omitempty"`
	LastUsedAt time.Time `bson:"last_used_at,omitempty"`
}

type UIDataContextKey added in v4.0.5

type UIDataContextKey struct{}

UIDataContextKey UI data key to get/set from context

Jump to

Keyboard shortcuts

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