idptest

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2024 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package idptest provides helper primitives and functions required for testing signing and key generation and a simple HTTP server with JWKS, issuer and IDP configuration endpoints.

Index

Constants

View Source
const (
	OpenIDConfigurationPath        = "/.well-known/openid-configuration"
	JWKSEndpointPath               = "/idp/keys"
	TokenEndpointPath              = "/idp/token"
	TokenIntrospectionEndpointPath = "/idp/introspect_token" // nolint:gosec // This server is used for testing purposes only.
)
View Source
const TestKeyID = "fac01c070cd08ba08809762da6e4f74af14e4790"

TestKeyID is a key ID of the pre-defined key for testing.

View Source
const TestPlainPrivateJWK = `` /* 1741-byte string literal not displayed */

TestPlainPrivateJWK is a plaintext representation of the pre-defined private key for testing. nolint: lll

Variables

View Source
var ErrUnauthorized = errors.New("unauthorized")

Functions

func GetTestRSAPrivateKey

func GetTestRSAPrivateKey() crypto.PrivateKey

GetTestRSAPrivateKey returns pre-defined RSA private key for testing.

func MakeTokenString

func MakeTokenString(claims jwtgo.Claims, kid string, rsaPrivateKey interface{}) (string, error)

MakeTokenString create signed token with claims.

func MakeTokenStringSignedWithTestKey

func MakeTokenStringSignedWithTestKey(claims jwtgo.Claims) (string, error)

MakeTokenStringSignedWithTestKey create test token signed with the pre-defined private key (TestKeyID) for testing.

func MakeTokenStringWithHeader

func MakeTokenStringWithHeader(
	claims jwtgo.Claims, kid string, rsaPrivateKey interface{}, header map[string]interface{},
) (string, error)

MakeTokenStringWithHeader create test signed token with claims and headers.

func MustMakeTokenString

func MustMakeTokenString(claims jwtgo.Claims, kid string, rsaPrivateKey interface{}) string

MustMakeTokenString create signed token with claims. It panics if error occurs.

func MustMakeTokenStringSignedWithTestKey

func MustMakeTokenStringSignedWithTestKey(claims jwtgo.Claims) string

MustMakeTokenStringSignedWithTestKey create test token signed with the pre-defined private key (TestKeyID) for testing. It panics if error occurs.

func MustMakeTokenStringWithHeader

func MustMakeTokenStringWithHeader(
	claims jwtgo.Claims, kid string, rsaPrivateKey interface{}, header map[string]interface{},
) string

MustMakeTokenStringWithHeader create test signed token with claims and headers. It panics if error occurs.

func MustSignToken added in v0.4.0

func MustSignToken(token *jwtgo.Token, rsaPrivateKey interface{}) string

MustSignToken signs token with key. It panics if error occurs.

func SignToken

func SignToken(token *jwtgo.Token, rsaPrivateKey interface{}) (string, error)

SignToken signs token with key.

Types

type GRPCServer

type GRPCServer struct {
	pb.UnimplementedIDPTokenServiceServer
	*grpc.Server
	// contains filtered or unexported fields
}

func NewGRPCServer

func NewGRPCServer(
	opts ...GRPCServerOption,
) *GRPCServer

NewGRPCServer creates a new instance of GRPCServer.

func (*GRPCServer) Addr

func (s *GRPCServer) Addr() string

Addr returns the server address.

func (*GRPCServer) CreateToken

func (*GRPCServer) IntrospectToken

func (*GRPCServer) Start

func (s *GRPCServer) Start() error

Start starts the GRPC server

func (*GRPCServer) StartAndWaitForReady

func (s *GRPCServer) StartAndWaitForReady(timeout time.Duration) error

StartAndWaitForReady starts the server waits for the server to start listening.

type GRPCServerOption

type GRPCServerOption func(*GRPCServer)

func WithGRPCAddr

func WithGRPCAddr(addr string) GRPCServerOption

func WithGRPCServerOptions

func WithGRPCServerOptions(opts ...grpc.ServerOption) GRPCServerOption

func WithGRPCTokenCreator

func WithGRPCTokenCreator(tokenCreator GRPCTokenCreator) GRPCServerOption

func WithGRPCTokenIntrospector

func WithGRPCTokenIntrospector(tokenIntrospector GRPCTokenIntrospector) GRPCServerOption

type GRPCTokenCreator

type GRPCTokenCreator interface {
	CreateToken(ctx context.Context, req *pb.CreateTokenRequest) (*pb.CreateTokenResponse, error)
}

type GRPCTokenIntrospector

type GRPCTokenIntrospector interface {
	IntrospectToken(ctx context.Context, req *pb.IntrospectTokenRequest) (*pb.IntrospectTokenResponse, error)
}

type HTTPClaimsProvider

type HTTPClaimsProvider interface {
	Provide(r *http.Request) (jwt.Claims, error)
}

HTTPClaimsProvider is an interface for providing JWT claims in HTTP handlers.

type HTTPServer

type HTTPServer struct {
	*http.Server

	KeysHandler                http.Handler
	TokenHandler               http.Handler
	TokenIntrospectionHandler  http.Handler
	OpenIDConfigurationHandler http.Handler
	Router                     *http.ServeMux
	// contains filtered or unexported fields
}

HTTPServer is a mock IDP server for testing purposes.

func NewHTTPServer

func NewHTTPServer(options ...HTTPServerOption) *HTTPServer

NewHTTPServer creates a new IDPMockServer with provided options.

func (*HTTPServer) Start

func (s *HTTPServer) Start() error

Start starts the HTTPServer.

func (*HTTPServer) StartAndWaitForReady

func (s *HTTPServer) StartAndWaitForReady(timeout time.Duration) error

StartAndWaitForReady starts the server waits for the server to start listening.

func (*HTTPServer) URL

func (s *HTTPServer) URL() string

URL method returns the URL of the server.

type HTTPServerOption

type HTTPServerOption func(s *HTTPServer)

func WithHTTPAddress

func WithHTTPAddress(addr string) HTTPServerOption

WithHTTPAddress is an option to set HTTP server address.

func WithHTTPClaimsProvider

func WithHTTPClaimsProvider(claimsProvider HTTPClaimsProvider) HTTPServerOption

WithHTTPClaimsProvider is an option to set ClaimsProvider for TokenHandler which will be used for POST /idp/token.

func WithHTTPIntrospectTokenHandler

func WithHTTPIntrospectTokenHandler(handler http.Handler) HTTPServerOption

WithHTTPIntrospectTokenHandler is an option to set custom handler for POST /idp/introspect_token.

func WithHTTPKeysHandler

func WithHTTPKeysHandler(handler http.Handler) HTTPServerOption

WithHTTPKeysHandler is an option to set custom handler for GET /idp/keys. Otherwise, JWKSHandler will be used.

func WithHTTPMiddleware added in v0.2.0

func WithHTTPMiddleware(mw func(http.Handler) http.Handler) HTTPServerOption

func WithHTTPOpenIDConfigurationHandler

func WithHTTPOpenIDConfigurationHandler(handler http.HandlerFunc) HTTPServerOption

WithHTTPOpenIDConfigurationHandler is an option to set custom handler for GET /.well-known/openid-configuration. Otherwise, OpenIDConfigurationHandler will be used.

func WithHTTPPublicJWKS

func WithHTTPPublicJWKS(keys []PublicJWK) HTTPServerOption

WithHTTPPublicJWKS is an option to set public JWKS for JWKSHandler which will be used for GET /idp/keys.

func WithHTTPTokenHandler

func WithHTTPTokenHandler(handler http.Handler) HTTPServerOption

WithHTTPTokenHandler is an option to set custom handler for POST /idp/token.

func WithHTTPTokenIntrospector

func WithHTTPTokenIntrospector(introspector HTTPTokenIntrospector) HTTPServerOption

WithHTTPTokenIntrospector is an option to set TokenIntrospector for TokenIntrospectionHandler which will be used for POST /idp/introspect_token.

type HTTPTokenIntrospector

type HTTPTokenIntrospector interface {
	IntrospectToken(r *http.Request, token string) (idptoken.IntrospectionResult, error)
}

HTTPTokenIntrospector is an interface for introspecting tokens.

type JWKSHandler

type JWKSHandler struct {
	PublicJWKS []PublicJWK
	// contains filtered or unexported fields
}

JWKSHandler is an HTTP handler that responds JWKS.

func (*JWKSHandler) ServeHTTP

func (h *JWKSHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request)

func (*JWKSHandler) ServedCount

func (h *JWKSHandler) ServedCount() uint64

ServedCount returns the number of times JWKS handler has been served.

type OpenIDConfigurationHandler

type OpenIDConfigurationHandler struct {
	BaseURLFunc              func() string // for cases when 'host:port' of providers' addresses to be determined during runtime
	JWKSURL                  string
	TokenEndpointURL         string
	IntrospectionEndpointURL string
	// contains filtered or unexported fields
}

OpenIDConfigurationHandler is an HTTP handler that responds token's issuer OpenID configuration.

func (*OpenIDConfigurationHandler) ServeHTTP

func (*OpenIDConfigurationHandler) ServedCount

func (h *OpenIDConfigurationHandler) ServedCount() uint64

ServedCount returns the number of times the handler has been served.

type PublicJWK

type PublicJWK struct {
	Alg string `json:"alg"`
	E   string `json:"e"`
	Kid string `json:"kid"`
	Kty string `json:"kty"`
	N   string `json:"n"`
	Use string `json:"use"`
}

func GetTestPublicJWKS

func GetTestPublicJWKS() []PublicJWK

type PublicJWKSResponse

type PublicJWKSResponse struct {
	Keys []PublicJWK `json:"keys"`
}

type SimpleTokenProvider

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

func NewSimpleTokenProvider

func NewSimpleTokenProvider(token string) *SimpleTokenProvider

func (*SimpleTokenProvider) GetToken

func (m *SimpleTokenProvider) GetToken(ctx context.Context, scope ...string) (string, error)

func (*SimpleTokenProvider) Invalidate

func (m *SimpleTokenProvider) Invalidate()

func (*SimpleTokenProvider) SetToken

func (m *SimpleTokenProvider) SetToken(token string)

type TokenHandler

type TokenHandler struct {
	ClaimsProvider HTTPClaimsProvider
	// contains filtered or unexported fields
}

TokenHandler is an implementation of a handler responding with IDP token.

func (*TokenHandler) ServeHTTP

func (h *TokenHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request)

func (*TokenHandler) ServedCount

func (h *TokenHandler) ServedCount() uint64

ServedCount returns the number of times the handler has been served.

type TokenIntrospectionHandler

type TokenIntrospectionHandler struct {
	TokenIntrospector HTTPTokenIntrospector
	// contains filtered or unexported fields
}

func (*TokenIntrospectionHandler) ServeHTTP

func (*TokenIntrospectionHandler) ServedCount

func (h *TokenIntrospectionHandler) ServedCount() uint64

ServedCount returns the number of times the handler has been served.

Jump to

Keyboard shortcuts

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