-
Notifications
You must be signed in to change notification settings - Fork 117
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
Bearer token can be fetched dynamically. #417
Conversation
Hi @wojtekmach, do you prefer having a conversation in an issue, or is just opening a PR OK? |
PR is much appreciated, thank you! I'd like to take some time thinking this through. If we allow FWIW we can already achieve this today like this: Req.new()
|> Req.Request.prepend_request_steps(
auth_bearer_dynamic: fn req ->
with %{options: %{auth: {:bearer, fun}}} when is_function(fun) <- req do
put_in(req.options[:auth], {:bearer, fun.()})
end
end
) |
In my opinion, the dynamic capability is not so important for basic auth (unless you want symmetry). A basic authN is certainly a known user password, which doesn't change or expire frequently. However, a bearer token typically expires after a (short) period of time. |
Right, sorry, I meant to say for basic auth dynamic function seems unnecessary but we may need to add it for consistency if we were add it for bearer auth. But maybe we don't. I'll think about it some more. Thank you. |
@chgeuer OK, I think I have the answer, a more generalised solution without explicitly supporting auth: {:bearer, fn -> ... end} let's go with: auth: fn -> {:bearer, ...} end in other words instead of: @type auth :: string | {:basic, string} | {:bearer, string} we have: @type auth :: auth | (-> auth)
when auth:
string
| {:basic, string}
| {:bearer, string} WDYT? If this sounds good, please remember to update "redact" test in test/req_test.exs. |
@wojtekmach Can we run this in the CI/CD here? |
Thank you! |
Thank you for creating and maintaining it, good stuff. |
QQ, would it be possible to release this as 0.5.7 to hex.pm? |
Hi @wojtekmach,
This PR allows the bearer token to be fetched dynamically, by supplying a 0-arity function:
This is e.g. helpful when having a GenServer (like here) that constantly keeps a valid token in-memory. This way, you just tell :req to grab the latest token whenever it needs to make a request.
I haven't open an issuer for this, can do so if you'd prefer that.
Without a bearer token being dynamically fetched, I do something like this (which is like, nah):