fakeapi

package
v0.54.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 24 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package fakeapi provides a fake Snyk API server for tests. It serves machine enrollment, tokens, whoami, the machine's and the authenticated principal's own records, and organizations. It independently verifies everything the client signs, so a client bug cannot pass by agreeing with itself.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeJWT

func DecodeJWT(t *testing.T, token string) (header, claims map[string]any)

DecodeJWT returns the header and claims of a compact JWS without verifying it.

func VerifyJWT

func VerifyJWT(token string, pub crypto.PublicKey) error

VerifyJWT verifies a compact JWS signed with ES256 or RS256 by pub.

Types

type Server

type Server struct {
	*httptest.Server

	// TokenRequests counts accepted token requests.
	TokenRequests int
	// contains filtered or unexported fields
}

Server is a fake API serving the enrollment and token endpoints.

func NewServer

func NewServer(t *testing.T) *Server

NewServer starts a fake API implementing the enrollment and token endpoints.

func (*Server) AddOrg

func (s *Server) AddOrg(orgID, tenantID string)

AddOrg makes the organization record for orgID exist, naming tenantID as its tenant, or no tenant if tenantID is empty.

func (*Server) AllowFleetKey

func (s *Server) AllowFleetKey(pub *ecdsa.PublicKey, machineIDs ...string)

AllowFleetKey registers the public half of a fleet key and allows it for the machines.

func (*Server) BearerTokens

func (s *Server) BearerTokens() []string

BearerTokens returns the bearer tokens that requests carried in their Authorization header, in the order they arrived.

func (*Server) ClientCount

func (s *Server) ClientCount() int

ClientCount returns how many clients are registered, whether active or not.

func (*Server) ClientKey

func (s *Server) ClientKey(clientID string) *rsa.PublicKey

ClientKey returns the key clientID enrolled with.

func (*Server) CredentialHeaders

func (s *Server) CredentialHeaders() []string

CredentialHeaders returns the names of the credential headers (Authorization, Session-Token) that requests carried, one entry per header per request. The enrollment endpoints take none.

func (*Server) DeprovisionMachine

func (s *Server) DeprovisionMachine(machineID string)

DeprovisionMachine retires a machine: it can't enroll again, with any join material.

func (*Server) EnrollmentRequests

func (s *Server) EnrollmentRequests() int

EnrollmentRequests returns how many requests reached the enrollment endpoint, accepted or not.

func (*Server) FailMachineSelf

func (s *Server) FailMachineSelf(status int)

FailMachineSelf makes the machine's own record answer with status and a JSON:API error, whoever asks.

func (*Server) IssueDeviceToken

func (s *Server) IssueDeviceToken(machineID string) string

IssueDeviceToken returns a new single-use device token for a registered machine, as an admin gets. Like the service, it revokes the machine's earlier device tokens.

func (*Server) MachineSelfRequests

func (s *Server) MachineSelfRequests() int

MachineSelfRequests returns how many requests reached the machine's own record, answered or not.

func (*Server) OrgRequests

func (s *Server) OrgRequests() int

OrgRequests returns how many requests reached an organization record, answered or not.

func (*Server) RegisterClient

func (s *Server) RegisterClient(clientID string, key *rsa.PublicKey)

RegisterClient makes the token endpoint accept client assertions for clientID signed by key.

func (*Server) RegisterClientSecret

func (s *Server) RegisterClientSecret(clientID, secret string)

RegisterClientSecret makes the token endpoint accept the shared secret for clientID, in the Authorization header or in the request body.

func (*Server) RegisterMachine

func (s *Server) RegisterMachine(serialNumbers ...string) string

RegisterMachine registers a machine with serial numbers, as an admin does, and returns its id.

func (*Server) RegisterPAT

func (s *Server) RegisterPAT(pat string)

RegisterPAT makes GET /rest/self accept pat, sent as the API sends a token: "Authorization: token <pat>".

func (*Server) RevokeClient

func (s *Server) RevokeClient(clientID string)

RevokeClient makes the token endpoint reject clientID and invalidates the access tokens issued to it, as an admin revoking the client does.

func (*Server) SetDefaultOrg

func (s *Server) SetDefaultOrg(orgID string)

SetDefaultOrg sets the organization the principal's own record names as its default.

func (*Server) SetErrorTitles

func (s *Server) SetErrorTitles(on bool)

SetErrorTitles sets whether enrollment error responses carry a JSON:API title.

func (*Server) SetTenantID

func (s *Server) SetTenantID(tenantID string)

SetTenantID sets the tenant the machine's own record names.

func (*Server) TokenAttempts

func (s *Server) TokenAttempts() int

TokenAttempts returns how many requests reached the token endpoint, accepted or not.

Jump to

Keyboard shortcuts

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