Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📝 [Proposal]: Access ctx from Authorizer function from BasicAuth middleware. #3095

Open
3 tasks done
nakisen opened this issue Aug 3, 2024 · 4 comments
Open
3 tasks done

Comments

@nakisen
Copy link

nakisen commented Aug 3, 2024

Feature Proposal Description

I needed to access ctx from the Authorizer function. I make a database query in this function and I needed to transfer some values ​​to the main handler. Since ctx cannot be accessed, a DB query needs to be made again in the main handler. I somehow solved my need, but if this becomes a permanent feature, it would be useful for different usage scenarios.

Alignment with Express API

This can be done in Express.js.

HTTP RFC Standards Compliance

This is not an issue related to the http RFC.

API Stability

A feature that could be used in the future.

Feature Examples

func initHttpServer() error {
...
	basicAuthCfg := basicauth.Config{
		Authorizer:   authorizeHttpBasic,
	}
	basicAuthMiddleware := basicauth.New(basicAuthCfg)
	httpSrv.Use("/pb", basicAuthMiddleware)

	httpSrv.Get("/pb/:brand", brand_xml)
...
}

func authorizeHttpBasic(user, pass string, c fiber.Ctx) bool {
	list := chekUserAndGetListFromDB(user, pass)
	if len(list) > 0 {
		c.Locals("list", list)
		return true
	}

	return false
}

func brand_xml(c fiber.Ctx) error {
...
	brand := c.Params("brand")
	list := c.Locals("list").([]map[string]string)
	return tmpl.ExecuteTemplate(c.Response().BodyWriter(), brand, list)
...
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have searched for existing issues that describe my proposal before opening this one.
  • I understand that a proposal that does not meet these guidelines may be closed without explanation.
Copy link

welcome bot commented Aug 3, 2024

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@gaby
Copy link
Member

gaby commented Aug 3, 2024

@nakisen Can you provide a URL to see how Express.js is implementing this?

@nakisen
Copy link
Author

nakisen commented Aug 4, 2024

I guess I have entered some incomplete information about Express.js. You can easily do this by writing your own middleware in Express.js.

Yes, you can do this by writing middleware in Fiber and that's what I did. It would be nice to have this feature in the built-in "basicauth" middleware.

There will only be a small change in the config struct and a few lines of related code will change.

// middleware/basicauth/config.go
// Before
type Config struct {
...
	Authorizer func(string, string) bool
...
}

// After
type Config struct {
...
	Authorizer func(string, string, fiber.Ctx) bool
...
}

@gaby gaby self-assigned this Aug 13, 2024
@gaby
Copy link
Member

gaby commented Aug 13, 2024

@nakisen I will submit a PR for this in v3 this week/weekend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants