| 1 | package basicauthscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/middleware/basicauth`/ |
| 2 | |
| 3 | import ( |
| 4 | "encoding/base64scip-go gomod github.com/golang/go/src go1.20 `encoding/base64`/" |
| 5 | "stringsscip-go gomod github.com/golang/go/src go1.20 strings/" |
| 6 | |
| 7 | "github.com/gofiber/fiber/v2scip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/" |
| 8 | "github.com/gofiber/fiber/v2/utilsscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/utils`/" |
| 9 | ) |
| 10 | |
| 11 | // New creates a new middleware handler |
| 12 | func Newscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/middleware/basicauth`/New().(config Configscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/middleware/basicauth`/Config#) fiberscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/.Handlerscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/Handler# { |
| 13 | // Set default config |
| 14 | cfg := configDefaultscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/middleware/basicauth`/configDefault().(config) |
| 15 | |
| 16 | // Return new handler |
| 17 | return func(c *fiberscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/.Ctxscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/Ctx#) error { |
| 18 | // Don't execute middleware if Next returns true |
| 19 | if cfg.Nextscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/middleware/basicauth`/Config#Next. != nil && cfg.Nextscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/middleware/basicauth`/Config#Next.(c) { |
| 20 | return c.Nextscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/Ctx#Next().() |
| 21 | } |
| 22 | |
| 23 | // Get authorization header |
| 24 | auth := c.Getscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/Ctx#Get().(fiberscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/.HeaderAuthorizationscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/HeaderAuthorization.) |
| 25 | |
| 26 | // Check if the header contains content besides "basic". |
| 27 | if len(auth) <= 6 || !utilsscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/utils`/.EqualFoldscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/utils`/EqualFold().(auth[:6], "basic ") { |
| 28 | return cfg.Unauthorizedscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/middleware/basicauth`/Config#Unauthorized.(c) |
| 29 | } |
| 30 | |
| 31 | // Decode the header contents |
| 32 | raw, err := base64scip-go gomod github.com/golang/go/src go1.20 `encoding/base64`/.StdEncodingscip-go gomod github.com/golang/go/src go1.20 `encoding/base64`/StdEncoding..DecodeStringscip-go gomod github.com/golang/go/src go1.20 `encoding/base64`/Encoding#DecodeString().(auth[6:]) |
| 33 | if err != nil { |
| 34 | return cfg.Unauthorizedscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/middleware/basicauth`/Config#Unauthorized.(c) |
| 35 | } |
| 36 | |
| 37 | // Get the credentials |
| 38 | creds := utilsscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/utils`/.UnsafeStringscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/utils`/UnsafeString().(raw) |
| 39 | |
| 40 | // Check if the credentials are in the correct form |
| 41 | // which is "username:password". |
| 42 | index := stringsscip-go gomod github.com/golang/go/src go1.20 strings/.Indexscip-go gomod github.com/golang/go/src go1.20 strings/Index().(creds, ":") |
| 43 | if index == -1 { |
| 44 | return cfg.Unauthorizedscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/middleware/basicauth`/Config#Unauthorized.(c) |
| 45 | } |
| 46 | |
| 47 | // Get the username and password |
| 48 | username := creds[:index] |
| 49 | password := creds[index+1:] |
| 50 | |
| 51 | if cfg.Authorizerscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/middleware/basicauth`/Config#Authorizer.(username, password) { |
| 52 | c.Localsscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/Ctx#Locals().(cfg.ContextUsernamescip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/middleware/basicauth`/Config#ContextUsername., username) |
| 53 | c.Localsscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/Ctx#Locals().(cfg.ContextPasswordscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/middleware/basicauth`/Config#ContextPassword., password) |
| 54 | return c.Nextscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2`/Ctx#Next().() |
| 55 | } |
| 56 | |
| 57 | // Authentication failed |
| 58 | return cfg.Unauthorizedscip-go gomod github.com/gofiber/fiber/v2 . `github.com/gofiber/fiber/v2/middleware/basicauth`/Config#Unauthorized.(c) |
| 59 | } |
| 60 | } |