Skip to content

Basic Auth

BasicAuth is a small middleware for admin panels, internal tools, and simple protected routes.

admin.Use(middleware.BasicAuthWithConfig(middleware.BasicAuthConfig{
Validator: middleware.BasicAuthStatic("admin", "secret"),
}))
  • BasicAuthFromAuthorizationHeader()
  • BasicAuthFromHeader(name)
  • BasicAuthFromHeaderPrefix(name, prefix)
  • BasicAuthFromFirst(extractors...)
  • BasicAuthStatic(username, password)
  • BasicAuthStaticPairs(pairs...)

BasicAuthStaticPairs hashes usernames and passwords before comparison and uses constant-time checks.

Field Meaning
Skipper Skip auth for selected requests
Extractor Read credentials from a custom source
Validator Required validator
SuccessHandler Override the success path
ErrorHandler Override error/challenge behavior
Realm Sets the WWW-Authenticate realm
identity, ok := middleware.BasicAuthCurrent(c)
username, ok := middleware.BasicAuthUsername(c)

Zinc stores:

  • Username
  • Source

That lets you distinguish whether credentials came from the normal authorization header or a custom header-based extractor.

Use Basic Auth when you genuinely want Basic Auth. For opaque API keys, use Key Auth. Use JWT for signed bearer tokens with claims.