{"modulePath":"github.com/devaccuracy/ledgerforge","version":"v1.0.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package middleware\n\n## Constants\n\n```go\nconst (\n\t// Actions\n\tActionRead   Action = \"read\"\n\tActionWrite  Action = \"write\"\n\tActionDelete Action = \"delete\"\n\tActionAll    Action = \"*\"\n\n\t// Resources\n\tResourceLedgers         Resource = \"ledgers\"\n\tResourceBalances        Resource = \"balances\"\n\tResourceAccounts        Resource = \"accounts\"\n\tResourceIdentities      Resource = \"identities\"\n\tResourceTransactions    Resource = \"transactions\"\n\tResourceBalanceMonitors Resource = \"balance-monitors\"\n\tResourceHooks           Resource = \"hooks\"\n\tResourceAPIKeys         Resource = \"api-keys\"\n\tResourceSearch          Resource = \"search\"\n\tResourceReconciliation  Resource = \"reconciliation\"\n\tResourceMetadata        Resource = \"metadata\"\n\tResourceBackup          Resource = \"backup\"\n\tResourceAll             Resource = \"*\"\n)\n```\n\n```go\nconst (\n\tKeyHeader = \"X-LedgerForge-Key\"\n)\n```\n\n## Functions\n\n```go\nfunc BuildScope(resource Resource, action Action) string\n```\nBuildScope creates a scope string from resource and action\n\n```go\nfunc CanGrantScopes(callerScopes, requestedScopes []string) bool\n```\nCanGrantScopes reports whether all requested scopes are covered by the caller's scopes.\n\n```go\nfunc HasPermission(scopes []string, resource Resource, method string) bool\n```\nHasPermission checks if a set of scopes has permission for a given resource and HTTP method\n\n```go\nfunc MetricsAuth(secure bool, token string) gin.HandlerFunc\n```\nMetricsAuth returns a middleware that controls access to the /metrics endpoint.\n\nBehavior based on secure mode and token configuration:\n\n  - Secure mode OFF, no token: open access (no auth required)\n  - Secure mode OFF, token set: require bearer token\n  - Secure mode ON, token set: require bearer token\n  - Secure mode ON, no token: block all access (misconfiguration)\n\nWhen authentication is required, requests must include \"Authorization: Bearer \\\u003ctoken\u003e\". This uses the standard Authorization header that Prometheus natively supports via its scrape\\_configs authorization block.\n\n```go\nfunc MetricsAuthHandler(secure bool, token string, next http.Handler) http.Handler\n```\nMetricsAuthHandler wraps an http.Handler with bearer token authentication. This is the non-Gin equivalent of MetricsAuth, used for the worker monitoring server which uses a standard http.ServeMux instead of Gin. Same secure mode logic as MetricsAuth: blocks access when secure=true and token is empty.\n\n```go\nfunc ParseScope(scope string) (Resource, Action)\n```\nParseScope parses a scope string into resource and action\n\n```go\nfunc RateLimitMiddleware(conf *config.Configuration) gin.HandlerFunc\n```\nRateLimitMiddleware creates a middleware for rate limiting using Tollbooth. It sets up rate limiting based on the configuration parameters and applies it to incoming requests.\n\nParameters: - conf: The configuration object containing rate limit settings.\n\nReturns: - gin.HandlerFunc: A middleware function that applies rate limiting to requests.\n\n```go\nfunc RequestSizeLimit(maxBytes int64) gin.HandlerFunc\n```\nRequestSizeLimit caps the size of non-upload request bodies so a large POST cannot exhaust memory before a handler (or the auth middleware that reads the body to inject the API key) consumes it. Multipart reconciliation uploads use the upload handler's larger MaxUploadSizeMB limit.\n\n```go\nfunc ScopeCovers(grantedScope, requestedScope string) bool\n```\nScopeCovers reports whether a granted scope is broad enough to authorize a requested scope.\n\n```go\nfunc SecurityHeaders() gin.HandlerFunc\n```\nSecurityHeaders sets security headers to the response. It sets the following headers: - X-Content-Type-Options: nosniff - X-Frame-Options: DENY - Referrer-Policy: strict-origin-when-cross-origin - Content-Security-Policy: default-src 'none'; frame-ancestors 'none' - Cache-Control: no-store - Strict-Transport-Security: max-age=31536000; includeSubDomains\n\nReturns: - gin.HandlerFunc: A middleware function that sets security headers to the response.\n\n## Types\n\n```go\ntype Action string\n```\nAction represents the allowed actions on a resource. Actions include read, write, delete, and wildcard (\\*).\n\n```go\ntype AuthMiddleware struct {\n\t// contains filtered or unexported fields\n}\n```\nAuthMiddleware handles authentication and authorization for API routes. It supports both master key and API key authentication using the X-LedgerForge-Key header.\n\n```go\nfunc NewAuthMiddleware(ledgerforge *ledgerforge.LedgerForge) *AuthMiddleware\n```\nNewAuthMiddleware creates a new instance of AuthMiddleware.\n\nParameters: - ledgerforge: The LedgerForge service used to validate API keys.\n\nReturns: - \\*AuthMiddleware: A new instance of the authentication middleware.\n\n```go\nfunc (m *AuthMiddleware) Authenticate() gin.HandlerFunc\n```\nAuthenticate returns a middleware function that handles authentication and authorization for all routes. It checks for the X-LedgerForge-Key header and validates it against either the master key or API keys. For API keys, it verifies the key's validity and checks permissions based on the resource and HTTP method. For POST requests with API keys, it injects the API key ID into the metadata of the request body.\n\nReturns: - gin.HandlerFunc: A middleware function that performs the authentication.\n\nResponses: - 200 OK: When authentication succeeds. - 401 Unauthorized: When the API key is missing or invalid. - 403 Forbidden: When the API key lacks sufficient permissions.\n\n```go\ntype Resource string\n```\nResource represents a protected API resource that can be accessed via API keys. Each resource corresponds to a specific API endpoint category.\n\n","path":"github.com/devaccuracy/ledgerforge/api/middleware","name":"middleware","synopsis":"","isRedistributable":true}
