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

refactor auth middleware for supporting bearer, cookie and query #560

Merged
merged 3 commits into from
May 1, 2024

Conversation

kaplanelad
Copy link
Contributor

Extract JWT token from cookie

pub fn extract_token_from_cookie(parts: &Parts) -> eyre::Result<String> {
let jar = cookie::CookieJar::from_headers(&parts.headers);
Ok(jar
.get("token")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would create some kind of unique Loco session ID key name.
in Rails it is: _APPNAME_session

and is also configurable:

config.session_store :cookie_store, key: '_somethingelse_session'

so we might need to reach into AppContext and then configuration for the cookie to grab the name of the cookie, and then when generating a new app give it a default name of _APP_NAME_loco-session (naming it loco-session to avoid conflict with rails)

/// # Errors
/// when token value from cookie is not found
pub fn extract_token_from_cookie(parts: &Parts) -> eyre::Result<String> {
let jar = cookie::CookieJar::from_headers(&parts.headers);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if possible, to take only httponly cookie or else put out an error. this will forcefully educate users to do the right thing

///
/// # Errors
/// when token value from cookie is not found
pub fn extract_token_from_cookie(parts: &Parts) -> eyre::Result<String> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we sure we want eyre::Result here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function returns an error if the token cannot strip the value to get only the token.
I prefer not to return an Option and return an error with a message that explains where the error came from.

you prefer Result<String, String> instead of eyre::Result?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

@kaplanelad kaplanelad changed the title extract authentication token from a cookie within middleware refactor auth middleware for supporting bearer, cookie and query May 1, 2024
@kaplanelad kaplanelad merged commit ddce44f into master May 1, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants