userclient

package module
v0.0.0-...-ae08e5a Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2023 License: MIT Imports: 12 Imported by: 6

README

user-service-go-client

=========== User service go client

Installation

go get github.com/best-expendables-v2/user-service-client

Documentation

Index

Constants

View Source
const (
	RoleAdmin                  = "Admin"
	RoleShippingProvider       = "ShippingProvider"
	RolePlatformTransportation = "PlatformTransportation"
	RolePlatformWarehouse      = "PlatformWarehouse"
	RolePlatformOverview       = "PlatformOverview"
	RoleLocationTree           = "LocationTree"
	RoleAccessAdmin            = "AccessAdmin"
	RoleReturn                 = "Return"
	RmsAdmin                   = "RmsAdmin"
	RmsReturnOperator          = "RmsReturnOperator"
	RmsCbOperator              = "RmsCbOperator"
	RmsApi                     = "RmsApi"
	RoleRmsApplicationAdmin    = "RmsApplicationAdmin"
	RmsCs                      = "RmsCs"
	RmsAdminID                 = "ID_RMS_ADMIN"
	RmsAdminVN                 = "VN_RMS_ADMIN"
	RmsAdminTH                 = "TH_RMS_ADMIN"
	RmsAdminSG                 = "SG_RMS_ADMIN"
	RmsAdminPH                 = "PH_RMS_ADMIN"
	RmsAdminPK                 = "PK_RMS_ADMIN"
	RmsAdminBD                 = "BD_RMS_ADMIN"
	RmsAdminLK                 = "LK_RMS_ADMIN"
	RmsAdminNP                 = "NP_RMS_ADMIN"
	RmsAdminMM                 = "MM_RMS_ADMIN"
	RmsAdminMY                 = "MY_RMS_ADMIN"
	RmsReturnOperatorID        = "ID_RMS_RETURN-OPERATOR"
	RmsReturnOperatorVN        = "VN_RMS_RETURN-OPERATOR"
	RmsReturnOperatorTH        = "TH_RMS_RETURN-OPERATOR"
	RmsReturnOperatorSG        = "SG_RMS_RETURN-OPERATOR"
	RmsReturnOperatorPH        = "PH_RMS_RETURN-OPERATOR"
	RmsReturnOperatorMY        = "MY_RMS_RETURN-OPERATOR"
	RmsReturnOperatorPK        = "PK_RMS_RETURN-OPERATOR"
	RmsReturnOperatorBD        = "BD_RMS_RETURN-OPERATOR"
	RmsReturnOperatorLK        = "LK_RMS_RETURN-OPERATOR"
	RmsReturnOperatorNP        = "NP_RMS_RETURN-OPERATOR"
	RmsReturnOperatorMM        = "MM_RMS_RETURN-OPERATOR"
	RmsCsID                    = "ID_RMS_CS"
	RmsCsVN                    = "VN_RMS_CS"
	RmsCsTH                    = "TH_RMS_CS"
	RmsCsSG                    = "SG_RMS_CS"
	RmsCsPH                    = "PH_RMS_CS"
	RmsCsMY                    = "MY_RMS_CS"
	RmsCsPK                    = "PK_RMS_CS"
	RmsCsBD                    = "BD_RMS_CS"
	RmsCsLK                    = "LK_RMS_CS"
	RmsCsNP                    = "NP_RMS_CS"
	RmsCsMM                    = "MM_RMS_CS"
	RmsCrossborderOperatorID   = "ID_RMS_CROSSBORDER-OPERATOR"
	RmsCrossborderOperatorVN   = "VN_RMS_CROSSBORDER-OPERATOR"
	RmsCrossborderOperatorTH   = "TH_RMS_CROSSBORDER-OPERATOR"
	RmsCrossborderOperatorSG   = "SG_RMS_CROSSBORDER-OPERATOR"
	RmsCrossborderOperatorPH   = "PH_RMS_CROSSBORDER-OPERATOR"
	RmsCrossborderOperatorMY   = "MY_RMS_CROSSBORDER-OPERATOR"
	RmsCrossborderOperatorPK   = "PK_RMS_CROSSBORDER-OPERATOR"
	RmsCrossborderOperatorBD   = "BD_RMS_CROSSBORDER-OPERATOR"
	RmsCrossborderOperatorLK   = "LK_RMS_CROSSBORDER-OPERATOR"
	RmsCrossborderOperatorNP   = "NP_RMS_CROSSBORDER-OPERATOR"
	RmsCrossborderOperatorMM   = "MM_RMS_CROSSBORDER-OPERATOR"
)

User roles which should be identical with user-service

View Source
const DEFAULT_TIME_OUT = 10

Variables

View Source
var (
	ErrServiceUnavailable = errors.New("service is unavailable")

	ErrUnauthorized = errors.New("unauthorized")

	ErrNotFound = errors.New("not found user")

	ErrMissingToken = errors.New("missing token in response")
)
View Source
var DefaultRetryConfig = RetryConfig{
	MaxAttempt: defaultMaxAttempt,
	WaitTime:   defaultWaitTime,
}

Functions

func CheckRoles

func CheckRoles(roles ...string) func(http.Handler) http.Handler

func ContextWithToken

func ContextWithToken(ctx context.Context, token string) context.Context

ContextWithToken add user token to context

func ContextWithUser

func ContextWithUser(ctx context.Context, user *User) context.Context

ContextWithUser add user to context

func GetTokenFromContext

func GetTokenFromContext(ctx context.Context) string

GetTokenFromContext return token from context, empty string if token not exists

Types

type Cache

type Cache interface {
	Get(key string, obj interface{}) error
	Set(key string, obj interface{}) error
	Delete(key string) error
}

type CacheClient

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

func NewCacheClient

func NewCacheClient(client Client, cache Cache) *CacheClient

func (*CacheClient) Authenticate

func (c *CacheClient) Authenticate(username string, password string) (string, error)

func (*CacheClient) FindAll

func (c *CacheClient) FindAll(token string) ([]*User, error)

func (*CacheClient) FindById

func (c *CacheClient) FindById(token, userId string) (*User, error)

func (*CacheClient) Logout

func (c *CacheClient) Logout(token string) error

func (*CacheClient) Me

func (c *CacheClient) Me(token string) (*User, error)

func (*CacheClient) RevokedTokens

func (c *CacheClient) RevokedTokens(token string) ([]RevokedToken, error)

type CacheMock

type CacheMock struct {
	GetFn    func(key string, obj interface{}) error
	SetFn    func(key string, obj interface{}) error
	DeleteFn func(key string) error
}

func (*CacheMock) Delete

func (c *CacheMock) Delete(key string) error

func (*CacheMock) Get

func (c *CacheMock) Get(key string, obj interface{}) error

func (*CacheMock) Set

func (c *CacheMock) Set(key string, obj interface{}) error

type Client

type Client interface {
	Authenticate(username, password string) (string, error)

	Me(token string) (*User, error)
	Logout(token string) error

	FindById(token, userId string) (*User, error)
	FindAll(token string) ([]*User, error)

	RevokedTokens(token string) ([]RevokedToken, error)
}

type ClientWithToken

type ClientWithToken interface {
	Me() (*User, error)
	FindById(userId string) (*User, error)
	FindAll() ([]*User, error)
	RevokedTokens() ([]RevokedToken, error)
}

type ClientWithTokenHolder

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

func NewClientWithTokenHolder

func NewClientWithTokenHolder(c Client, h TokenHolder) *ClientWithTokenHolder

func (*ClientWithTokenHolder) FindAll

func (c *ClientWithTokenHolder) FindAll() ([]*User, error)

func (*ClientWithTokenHolder) FindById

func (c *ClientWithTokenHolder) FindById(userId string) (*User, error)

func (*ClientWithTokenHolder) Me

func (c *ClientWithTokenHolder) Me() (*User, error)

func (*ClientWithTokenHolder) RevokedTokens

func (c *ClientWithTokenHolder) RevokedTokens() ([]RevokedToken, error)

type HttpClient

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

func New

func NewDefault

func NewDefault(baseURL string) *HttpClient

func (*HttpClient) Authenticate

func (c *HttpClient) Authenticate(username string, password string) (string, error)

func (*HttpClient) FindAll

func (c *HttpClient) FindAll(token string) ([]*User, error)

func (*HttpClient) FindById

func (c *HttpClient) FindById(token, userId string) (*User, error)

func (*HttpClient) Logout

func (c *HttpClient) Logout(token string) error

func (*HttpClient) Me

func (c *HttpClient) Me(token string) (*User, error)

func (*HttpClient) RevokedTokens

func (c *HttpClient) RevokedTokens(token string) ([]RevokedToken, error)

type HttpRequestBuilder

type HttpRequestBuilder interface {
	BuildLoginRequest(username string, password string) (*http.Request, error)
	BuildMeRequest(token string) (*http.Request, error)
	BuildPlatformsRequest(token, userID string) (*http.Request, error)
	BuildLogoutRequest(token string) (*http.Request, error)
	BuildGetRequest(token, userID string) (*http.Request, error)
	BuildGetAllRequest(token string) (*http.Request, error)
	BuildRevokedTokensRequest(token string) (*http.Request, error)
}

type HttpRequestBuilderImpl

type HttpRequestBuilderImpl struct {
	BaseURL string
}

func (*HttpRequestBuilderImpl) BuildGetAllRequest

func (rb *HttpRequestBuilderImpl) BuildGetAllRequest(token string) (*http.Request, error)

func (*HttpRequestBuilderImpl) BuildGetRequest

func (rb *HttpRequestBuilderImpl) BuildGetRequest(token, userID string) (*http.Request, error)

func (*HttpRequestBuilderImpl) BuildLoginRequest

func (rb *HttpRequestBuilderImpl) BuildLoginRequest(username string, password string) (*http.Request, error)

func (*HttpRequestBuilderImpl) BuildLogoutRequest

func (rb *HttpRequestBuilderImpl) BuildLogoutRequest(token string) (*http.Request, error)

func (*HttpRequestBuilderImpl) BuildMeRequest

func (rb *HttpRequestBuilderImpl) BuildMeRequest(token string) (*http.Request, error)

func (*HttpRequestBuilderImpl) BuildPlatformsRequest

func (rb *HttpRequestBuilderImpl) BuildPlatformsRequest(token, userID string) (*http.Request, error)

func (*HttpRequestBuilderImpl) BuildRevokedTokensRequest

func (rb *HttpRequestBuilderImpl) BuildRevokedTokensRequest(token string) (*http.Request, error)

type HttpRequestBuilderMock

type HttpRequestBuilderMock struct {
	RequestURL                    string
	BuildLoginRequestMock         func(username string, password string) (*http.Request, error)
	BuildMeRequestMock            func(token string) (*http.Request, error)
	BuildPlatformsRequestMock     func(token, userID string) (*http.Request, error)
	BuildLogoutRequestMock        func(token string) (*http.Request, error)
	BuildGetRequestMock           func(token, userID string) (*http.Request, error)
	BuildGetAllRequestMock        func(token string) (*http.Request, error)
	BuildRevokedTokensRequestMock func(token string) (*http.Request, error)
}

func (*HttpRequestBuilderMock) BuildGetAllRequest

func (rb *HttpRequestBuilderMock) BuildGetAllRequest(token string) (*http.Request, error)

func (*HttpRequestBuilderMock) BuildGetRequest

func (rb *HttpRequestBuilderMock) BuildGetRequest(token, userID string) (*http.Request, error)

func (*HttpRequestBuilderMock) BuildLoginRequest

func (rb *HttpRequestBuilderMock) BuildLoginRequest(username string, password string) (*http.Request, error)

func (*HttpRequestBuilderMock) BuildLogoutRequest

func (rb *HttpRequestBuilderMock) BuildLogoutRequest(token string) (*http.Request, error)

func (*HttpRequestBuilderMock) BuildMeRequest

func (rb *HttpRequestBuilderMock) BuildMeRequest(token string) (*http.Request, error)

func (*HttpRequestBuilderMock) BuildPlatformsRequest

func (rb *HttpRequestBuilderMock) BuildPlatformsRequest(token, userID string) (*http.Request, error)

func (*HttpRequestBuilderMock) BuildRevokedTokensRequest

func (rb *HttpRequestBuilderMock) BuildRevokedTokensRequest(token string) (*http.Request, error)

type InMemoryTokenHolder

type InMemoryTokenHolder struct {
	sync.Mutex

	Username string
	Password string
	// contains filtered or unexported fields
}

Use this one for your consumers and other backend tasks this holder will automatically request a new token if the old one expired

func NewInMemoryTokenHolder

func NewInMemoryTokenHolder(username, password string) *InMemoryTokenHolder

func (*InMemoryTokenHolder) GetToken

func (h *InMemoryTokenHolder) GetToken(client Client) (string, error)

func (*InMemoryTokenHolder) Invalidate

func (h *InMemoryTokenHolder) Invalidate()

type Message

type Message struct {
	Source  string
	Event   string
	Payload string
}

Message pubsub message from user service

type Middleware

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

func NewMiddleware

func NewMiddleware(userServiceClient Client, config RetryConfig) *Middleware

func (*Middleware) Auth

func (m *Middleware) Auth(next http.Handler) http.Handler

func (*Middleware) SetLogger

func (m *Middleware) SetLogger(l logger)

type Platform

type Platform struct {
	Name string `json:"name"`
}

type PubSub

type PubSub interface {
	ReceiveMsg() (Message, error)
}

PubSub allows to subscribe to user service messages It's NOT safe for concurrent use by multiple goroutines.

func SubscribeRedis

func SubscribeRedis(c *redis.Client) (PubSub, error)

SubscribeRedis returns PubSub implemented on top of redis

type RetryConfig

type RetryConfig struct {
	MaxAttempt int
	WaitTime   int
}

type RevokedToken

type RevokedToken struct {
	Token     string    `json:"token"`
	ExpiredAt time.Time `json:"expiredAt"`
}

func (*RevokedToken) TTL

func (t *RevokedToken) TTL() time.Duration

type StaticTokenHolder

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

Use this one with token from auth middleware this holder doesn't retry to obtain new token

func NewStaticTokenHolder

func NewStaticTokenHolder(token string) *StaticTokenHolder

func (*StaticTokenHolder) GetToken

func (h *StaticTokenHolder) GetToken(Client) (string, error)

func (*StaticTokenHolder) Invalidate

func (h *StaticTokenHolder) Invalidate()

type TokenHolder

type TokenHolder interface {
	GetToken(Client) (string, error)
	Invalidate()
}

type User

type User struct {
	Id            string    `json:"id"`
	Username      string    `json:"username"`
	Email         string    `json:"email"`
	Active        bool      `json:"active"`
	Password      string    `json:"password"`
	Roles         []string  `json:"roles"`
	UpdatedAt     time.Time `json:"updatedAt"`
	CreatedAt     time.Time `json:"createdAt"`
	PlatformNames []string  `json:"platforms"`
}

func GetCurrentUserFromContext

func GetCurrentUserFromContext(ctx context.Context) *User

GetCurrentUserFromContext return user from context or nil

func (*User) HasAccessToPlatform

func (u *User) HasAccessToPlatform(name string) bool

func (*User) HasPlatformRole

func (u *User) HasPlatformRole() bool

func (User) HasRole

func (u User) HasRole(roles ...string) bool

type UserClientMock

type UserClientMock struct {
	AuthenticateMock func(username string, password string) (string, error)
	MeMock           func(token string) (*User, error)
	LogoutMock       func(token string) error

	FindByIdMock      func(token, userId string) (*User, error)
	FindAllMock       func(token string) ([]*User, error)
	RevokedTokensMock func(token string) ([]RevokedToken, error)
}

func (*UserClientMock) Authenticate

func (c *UserClientMock) Authenticate(username string, password string) (string, error)

func (*UserClientMock) FindAll

func (c *UserClientMock) FindAll(token string) ([]*User, error)

func (*UserClientMock) FindById

func (c *UserClientMock) FindById(token, userId string) (*User, error)

func (*UserClientMock) Logout

func (c *UserClientMock) Logout(token string) error

func (*UserClientMock) Me

func (c *UserClientMock) Me(token string) (*User, error)

func (*UserClientMock) RevokedTokens

func (c *UserClientMock) RevokedTokens(token string) ([]RevokedToken, error)

Jump to

Keyboard shortcuts

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