ctxtg

package module
v0.0.0-...-99935af Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2016 License: MIT Imports: 6 Imported by: 10

README

Context for QArea services Build Status GoDoc Coverage Status Go Report Card

Documentation

Overview

Package ctxtg provides Context which should be passed between all our services. Also ctxtg provides Token Signer, Parser and test helper implementations in ctxtgtest.

Index

Constants

View Source
const (
	TokenKey key
	TracingIDKey
	DataKey
)

Values to access ctxtg values inside context.Context

Variables

View Source
var (
	ErrInvalidToken = jsonrpc2.NewError(1, "INVALID_TOKEN")
	ErrTokenExpired = jsonrpc2.NewError(2, "TOKEN_EXPIRED")
)

Global errors for all projects

Functions

func DataFromContext

func DataFromContext(ctx context.Context) map[string]interface{}

DataFromContext returns map from Data key from ctx or nil

func ValueFromData

func ValueFromData(ctx context.Context, key string) interface{}

ValueFromData returns value from Data map inside ctx or nil

func WithDataValue

func WithDataValue(parent context.Context, key string, value interface{}) context.Context

WithDataValue add key-value to Data map inside context.Context and return new context.Context

Types

type Claims

type Claims struct {
	UserID UserID
}

Claims represents encoded into JWT info

type ClaimsFunc

type ClaimsFunc func(Claims) error

ClaimsFunc is function in which claims will be passed if JWT Token is fine

type Context

type Context struct {
	Token     Token
	Deadline  int64
	TracingID string
	Data      map[string]interface{}
}

Context for microservices communication

func FromContext

func FromContext(ctx context.Context) Context

FromContext convert context.Context to Context correctly extracting required fields

func (*Context) ToContext

func (c *Context) ToContext() (context.Context, context.CancelFunc)

ToContext convert to context.Context object

type CtxClaimsFunc

type CtxClaimsFunc func(context.Context, Claims) error

CtxClaimsFunc is function in which claims and converted context.Context will be passed if JWT Token is fine

type RSATokenParser

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

RSATokenParser for parsing JWT token Implements TokenParser

func NewRSATokenParser

func NewRSATokenParser(publicKey []byte) (*RSATokenParser, error)

NewRSATokenParser parse publicKey and return correct instance or error

func (*RSATokenParser) Parse

func (p *RSATokenParser) Parse(t Token) (*Claims, error)

Parse JWT token and return Claims or error

func (*RSATokenParser) ParseCtxWithClaims

func (p *RSATokenParser) ParseCtxWithClaims(context Context, f CtxClaimsFunc) error

ParseCtxWithClaims takes context, parse JWT token, convert context and, if token valid, calls f with converted context and JWT Claims

func (*RSATokenParser) ParseWithClaims

func (p *RSATokenParser) ParseWithClaims(t Token, f ClaimsFunc) error

ParseWithClaims takes t, parse JWT token and, if token valid, calls f with JWT Claims

type RSATokenSigner

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

RSATokenSigner for signing JWT tokens Implements TokenSigner

func NewRSATokenSigner

func NewRSATokenSigner(privateKey []byte) (*RSATokenSigner, error)

NewRSATokenSigner parse privateKey and return correct instance

func (*RSATokenSigner) Sign

func (r *RSATokenSigner) Sign(c Claims, timeout time.Duration) (Token, error)

Sign and encode c with timeout, returns signed Token or error

type Token

type Token string

Token represents JWT token

type TokenParser

type TokenParser interface {
	Parse(Token) (*Claims, error)
	ParseWithClaims(Token, ClaimsFunc) error
	ParseCtxWithClaims(Context, CtxClaimsFunc) error
}

TokenParser interface for JWT token parsers and point for mocking (see ctxtgtest subpackage)

type TokenSigner

type TokenSigner interface {
	Sign(c Claims, timeout time.Duration) (Token, error)
}

TokenSigner interface for JWT token signing and point for mocking (see ctxtgtest subpackage)

type UserID

type UserID int64

UserID represents user id in Timeguard system

Directories

Path Synopsis
Package ctxtgtest provides test utilities for ctgtg users
Package ctxtgtest provides test utilities for ctgtg users

Jump to

Keyboard shortcuts

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