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

Add token based api auth #217

Merged
merged 9 commits into from
Dec 24, 2023
Merged

Add token based api auth #217

merged 9 commits into from
Dec 24, 2023

Conversation

kaplanelad
Copy link
Contributor

token based API auth #115


#[derive(Debug, Deserialize, Serialize)]
// Represents the data structure for the API token.
pub struct ApiToken {
Copy link
Contributor

@jondot jondot Dec 23, 2023

Choose a reason for hiding this comment

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

I am wondering,
how possible is it to say:

pub struct ApiToken<T>
 ... everything depends on T
 ... includng the extractor 

and we know that T should eventually be users::Model,
maybe this T will be a T: Trait where T: FetchByApiKey,

and the User impl that FetchByApiKey.

Now this middleware becomes shareable by everyone

PS: i think we can adopt the same pattern for the JWT auth by creating a trait T: FetchByJWT

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The API token is generated within the user app (Loco framework is not familiar with that). In this scenario, I think the parameter is meaningless, as the user has complete control over it.

About the FetchByJWT i totally agree. maybe we can consider moving the claims to be a

Copy link
Contributor

@jondot jondot Dec 23, 2023

Choose a reason for hiding this comment

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

Notice what I did in the latest commit. I had a feeling if we move things around we might get something, I think it worked:

Authenticable trait lets us encapsulate all the logic, and then -- just refer to an extractor. Use ApiToken<T> and ApiToken<users::Model> to give Rust all the type information it needs to resolve.

On this "Authenticable" concept we can also encode the JWT behavior, and "capture" a user just like in api key auth.
There is a good chance that instead of having claims we can have the actual user model if we use the same trick.

@kaplanelad
Copy link
Contributor Author

Looks good @jondot, I reverted the middleware that I created. but keep the migration changes. wen releasing a new version I'll add the controller to the starters

Let me know if you want more changes from my side

@jondot jondot merged commit 6cd9312 into master Dec 24, 2023
16 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