transport

package
v0.0.0-...-2c2efe1 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2019 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package transport - HTTP request and response methods

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMethodNotAllowed when HTTP request method is not handled
	ErrMethodNotAllowed = errors.New("method not allowed")
	// ErrInvalidRequest when HTTP request is invalid
	ErrInvalidRequest = errors.New("invalid request")
	// ErrInvalidJSON when the request is invalid json
	ErrInvalidJSON = errors.New("invalid json")
	// ErrUnauthorized when a protected endpoint is not authorized
	ErrUnauthorized = errors.New("unauthorized")
	// ErrHTTPClientError when HTTP request status is not 200
	ErrHTTPClientError = errors.New("request error")
)

Functions

func AddClientHeader

func AddClientHeader(ctx context.Context, header http.Header) context.Context

AddClientHeader adds header to the http client context

func AuthorizeOIDC

func AuthorizeOIDC(authorizer Authorizer, allowUnauthorized bool) httptransport.ServerOption

AuthorizeOIDC checks the Authorization header OIDC token, verifies it and gets the user info

func GetHeaders

func GetHeaders(ctx context.Context) http.Header

GetHeaders returns the HTTP Request headers

func GetParams

func GetParams(ctx context.Context) url.Values

GetParams returns Query params of a request

func GetURLParams

func GetURLParams(ctx context.Context) url.Values

GetURLParams returns URL params of the request path

func NewHTTPHandler

func NewHTTPHandler(endpoints HTTPEndpoints, logger *logger.Logger, duration metrics.Histogram) *mux.Router

NewHTTPHandler returns an HTTP handler that makes a set of endpoints

func ServerOptions

func ServerOptions(opt ...httptransport.ServerOption) []httptransport.ServerOption

ServerOptions returns an array of httptransport.ServerOption

func SetCors

func SetCors(domain string) httptransport.ServerOption

SetCors sets CORS HTTP response headers for specific domains

func SetJWTAuthHeader

func SetJWTAuthHeader(ctx context.Context, jwtToken string) context.Context

SetJWTAuthHeader sets the Bearer Authorization header to the http client context

Types

type Authorizer

type Authorizer interface {
	Authorize(header http.Header) (*UserClaims, error)
}

Authorizer interface for implementing API Authorization

func NewOIDCAuthorizer

func NewOIDCAuthorizer(clientID, oidcProvider string, claims ...string) (Authorizer, error)

NewOIDCAuthorizer creates a new instance of OIDCAuthorizer

type ClientEndpoints

type ClientEndpoints map[string]endpoint.Endpoint

ClientEndpoints is a map of all exported client endpoints

func NewHTTPClient

func NewHTTPClient(instance string, endpoints HTTPEndpoints, logger *logger.Logger) (ClientEndpoints, error)

NewHTTPClient returns an HTTP handler that makes a set of endpoints

type EmptyAuthorizer

type EmptyAuthorizer struct{}

EmptyAuthorizer implements empty Authorizer

func (*EmptyAuthorizer) Authorize

func (a *EmptyAuthorizer) Authorize(header http.Header) (*UserClaims, error)

Authorize checks the IDToken

type ErrorStatus

type ErrorStatus map[error]int

ErrorStatus is a map of errors to http response status codes

func (ErrorStatus) ResponseStatus

func (e ErrorStatus) ResponseStatus(err error) (statusCode int, statusText string)

ResponseStatus returns the status code and status text based on the error type

type HTTPEndpoint

type HTTPEndpoint struct {
	Path        string
	Method      string
	Endpoint    endpoint.Endpoint
	NewRequest  func() interface{}
	NewResponse func() interface{}
	ErrStatus   ErrorStatus
	Options     []httptransport.ServerOption
}

HTTPEndpoint defines a single endpoint

type HTTPEndpoints

type HTTPEndpoints map[string]HTTPEndpoint

HTTPEndpoints is a map of endpoints Key is the name of the endpoint

type LocalAuthorizer

type LocalAuthorizer struct{}

LocalAuthorizer implements JWT Authorizer without verifying the signature For testing purposes only

func (*LocalAuthorizer) Authorize

func (a *LocalAuthorizer) Authorize(header http.Header) (*UserClaims, error)

Authorize checks the IDToken

type OIDCAuthorizer

type OIDCAuthorizer struct {
	ClientID        string
	RequestUserInfo bool
	ClaimsSupported []string
	Provider        *oidc.Provider
}

OIDCAuthorizer implements OIDC/OAuth2 JWT token authorization token authorizer

func (*OIDCAuthorizer) Authorize

func (a *OIDCAuthorizer) Authorize(header http.Header) (*UserClaims, error)

Authorize checks the IDToken

type UserClaims

type UserClaims map[string]interface{}

UserClaims holds information about the claims from the UserInfo endpoint

func GetUserInfo

func GetUserInfo(ctx context.Context) *UserClaims

GetUserInfo returns the authorized user info

func (*UserClaims) GetString

func (uc *UserClaims) GetString(key string) string

GetString returns a value of a key if exists

Jump to

Keyboard shortcuts

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