#oauth2 #strategies #passport

passport-strategies

A thin wrapper on top of oauth2-rs that simplifies oauth2 authentication

7 releases

0.1.14 Aug 29, 2024
0.1.11 Aug 11, 2024
0.1.5 Feb 28, 2024
0.1.2 Jan 21, 2024

#239 in Authentication

25 downloads per month

MIT license

31KB
522 lines

Passport strategies

Passport strategies for authenticating with Discord, 42, Facebook, Reddit, Google, Microsoft and Github using the OAuth 2.0 API. This library is a thin wrapper of oauth2 that simplifies authentication. This module lets you authenticate with the above mentioned providers in your applications. By plugging into passport-strategies, (Discord, Microsoft, Google, 42, Reddit, Github and Facebook) authentication can be easily and unobtrusively integrated into any rust application or rust framework.

Adding passport-strategies

passport-strategies = { version = "0.1.11" }

Usage

Create an Application

Before using passport-strategies, you must register an application with the respective provider. If you have not already done so, a new application can be created at Facebook, Google, Github, Microsoft, Reddit, Discord and 42. Your application will be issued an app ID and app secret, which need to be provided to the strategy. You will also need to configure a redirect URI which matches the route in your application.

Configure Strategy

The passport-strategies authenticates users using the desired provider account and OAuth 2.0 tokens. The app ID(or in some cases client id), redirect url and client secret obtained when creating an application are supplied as requirements when creating the strategy. You do not need to provide the authorization url and token url.Unlike passportjs, the strategy does not require a verify callback, which receives the access token and optional refresh token, as well as profile which contains the authenticated user's provider profile. Instead, the profile, the access token and optional refresh token is returned to complete authentication.

Example (Microsoft)

 use passport_strategies::strategies::MicrosoftStrategy;
 use passport_strategies::passport::Passport;

 let passport = Passport::default()
        .redirect_urls(passport_strategies::passport::Redirect::new(
            "http://localhost:<redirect_url_port>/signup",
            "http://localhost:<redirect_url_port>/success",
        )?)
        .strategize(
            Choice::Microsoft,
            MicrosoftStrategy::new(
                "<client_id>",
                "<client_secret>",
                &["user.read"],
                "<redirect_url>",
            ),
        )?;

See here for more examples.

What's new

  1. Support for the axum web framework.
  2. Reddit Strategy integration.
  3. Remove of the logic error in previous versions when multiple users try to authenticate at the same time.
  4. Clearing the nolonger needed verifiers from the memory

Dependencies

~7–19MB
~267K SLoC