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
- Variables
- func GetTestRSAPrivateKey() crypto.PrivateKey
- func MakeTokenString(claims jwtgo.Claims, kid string, rsaPrivateKey interface{}) (string, error)
- func MakeTokenStringSignedWithTestKey(claims jwtgo.Claims) (string, error)
- func MakeTokenStringWithHeader(claims jwtgo.Claims, kid string, rsaPrivateKey interface{}, ...) (string, error)
- func MustMakeTokenString(claims jwtgo.Claims, kid string, rsaPrivateKey interface{}) string
- func MustMakeTokenStringSignedWithTestKey(claims jwtgo.Claims) string
- func MustMakeTokenStringWithHeader(claims jwtgo.Claims, kid string, rsaPrivateKey interface{}, ...) string
- func SignToken(token *jwtgo.Token, rsaPrivateKey interface{}) (string, error)
- type GRPCServer
- func (s *GRPCServer) Addr() string
- func (s *GRPCServer) CreateToken(ctx context.Context, req *pb.CreateTokenRequest) (*pb.CreateTokenResponse, error)
- func (s *GRPCServer) IntrospectToken(ctx context.Context, req *pb.IntrospectTokenRequest) (*pb.IntrospectTokenResponse, error)
- func (s *GRPCServer) Start() error
- func (s *GRPCServer) StartAndWaitForReady(timeout time.Duration) error
- type GRPCServerOption
- type GRPCTokenCreator
- type GRPCTokenIntrospector
- type HTTPClaimsProvider
- type HTTPServer
- type HTTPServerOption
- func WithHTTPAddress(addr string) HTTPServerOption
- func WithHTTPClaimsProvider(claimsProvider HTTPClaimsProvider) HTTPServerOption
- func WithHTTPIntrospectTokenHandler(handler http.Handler) HTTPServerOption
- func WithHTTPKeysHandler(handler http.Handler) HTTPServerOption
- func WithHTTPMiddleware(mw func(http.Handler) http.Handler) HTTPServerOption
- func WithHTTPOpenIDConfigurationHandler(handler http.HandlerFunc) HTTPServerOption
- func WithHTTPPublicJWKS(keys []PublicJWK) HTTPServerOption
- func WithHTTPTokenHandler(handler http.Handler) HTTPServerOption
- func WithHTTPTokenIntrospector(introspector HTTPTokenIntrospector) HTTPServerOption
- type HTTPTokenIntrospector
- type JWKSHandler
- type OpenIDConfigurationHandler
- type PublicJWK
- type PublicJWKSResponse
- type SimpleTokenProvider
- type TokenHandler
- type TokenIntrospectionHandler
Constants ¶
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. )
const TestKeyID = "fac01c070cd08ba08809762da6e4f74af14e4790"
TestKeyID is a key ID of the pre-defined key for testing.
const TestPlainPrivateJWK = `` /* 1741-byte string literal not displayed */
TestPlainPrivateJWK is a plaintext representation of the pre-defined private key for testing. nolint: lll
Variables ¶
Functions ¶
func GetTestRSAPrivateKey ¶
func GetTestRSAPrivateKey() crypto.PrivateKey
GetTestRSAPrivateKey returns pre-defined RSA private key for testing.
func MakeTokenString ¶
MakeTokenString create signed token with claims.
func MakeTokenStringSignedWithTestKey ¶
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 ¶
MustMakeTokenString create signed token with claims. It panics if error occurs.
func MustMakeTokenStringSignedWithTestKey ¶
MustMakeTokenStringSignedWithTestKey create test token signed with the pre-defined private key (TestKeyID) for testing. It panics if error occurs.
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) CreateToken ¶
func (s *GRPCServer) CreateToken(ctx context.Context, req *pb.CreateTokenRequest) (*pb.CreateTokenResponse, error)
func (*GRPCServer) IntrospectToken ¶
func (s *GRPCServer) IntrospectToken(ctx context.Context, req *pb.IntrospectTokenRequest) (*pb.IntrospectTokenResponse, error)
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 ¶
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) 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 (h *OpenIDConfigurationHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request)
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) 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 (h *TokenIntrospectionHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request)
func (*TokenIntrospectionHandler) ServedCount ¶
func (h *TokenIntrospectionHandler) ServedCount() uint64
ServedCount returns the number of times the handler has been served.