All Projects → tvallotton → rocket_auth

tvallotton / rocket_auth

Licence: Apache-2.0, MIT licenses found Licenses found Apache-2.0 LICENSE-APACHE MIT LICENSE-MIT
An implementation for an authentication API for Rocket applications.

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to rocket auth

user login and register
user login and register system in django
Stars: ✭ 43 (-33.85%)
Mutual labels:  login, users
Kratos Selfservice Ui React Native
A reference implementation of an app using ORY Kratos for auth (login), sign up (registration), profile settings (update password), MFA/2FA, account recovery (password reset), and more for React Native. This repository is available as an expo template!
Stars: ✭ 24 (-63.08%)
Mutual labels:  login, auth
Kratos
Next-gen identity server (think Auth0, Okta, Firebase) with Ory-hardened authentication, MFA, FIDO2, profile management, identity schemas, social sign in, registration, account recovery, and IoT auth. Golang, headless, API-only - without templating or theming headaches.
Stars: ✭ 4,684 (+7106.15%)
Mutual labels:  login, users
react-apple-signin-auth
 Apple signin for React using the official Apple JS SDK
Stars: ✭ 58 (-10.77%)
Mutual labels:  login, auth
Laravel Adminless Ldap Auth
Authenticate users in Laravel against an adminless LDAP server
Stars: ✭ 199 (+206.15%)
Mutual labels:  login, auth
auth-flow-react-apollo-saga
Full stack login/register flow with React, Apollo, Redux, Redux-saga and MongoDB.
Stars: ✭ 22 (-66.15%)
Mutual labels:  login, auth
Fastify Esso
The easiest authentication plugin for Fastify, with built-in support for Single sign-on
Stars: ✭ 20 (-69.23%)
Mutual labels:  login, auth
lua-resty-feishu-auth
适用于 OpenResty / ngx_lua 的基于飞书组织架构的登录认证
Stars: ✭ 28 (-56.92%)
Mutual labels:  login, auth
Flask simplelogin
Simple Login - Login Extension for Flask - maintainer @cuducos
Stars: ✭ 133 (+104.62%)
Mutual labels:  login, auth
Userfrosting
🍩 Modern PHP user login and management framework++.
Stars: ✭ 1,547 (+2280%)
Mutual labels:  login, users
JWT-user-auth-API-bolilerplate
Boilerplate for backend API user authentication with JWT
Stars: ✭ 13 (-80%)
Mutual labels:  login, users
rocket-auth-login
Authentication and login processing for Rust's Rocket web framework. Demonstrates a working example of how to authenticate users and process login as well as how to handle logging out.
Stars: ✭ 52 (-20%)
Mutual labels:  web-development, rocket
supabase-ui-svelte
Supabase authentication UI for Svelte
Stars: ✭ 83 (+27.69%)
Mutual labels:  login, auth
laravel-magiclink
Create link for authenticate in Laravel without password or get private content
Stars: ✭ 135 (+107.69%)
Mutual labels:  login, auth
EasyFirebase
No description or website provided.
Stars: ✭ 48 (-26.15%)
Mutual labels:  login, auth
Php Auth
Authentication for PHP. Simple, lightweight and secure.
Stars: ✭ 713 (+996.92%)
Mutual labels:  login, auth
hapi-doorkeeper
User authentication for web servers
Stars: ✭ 14 (-78.46%)
Mutual labels:  login, auth
authorize-me
Authorization with social networks
Stars: ✭ 44 (-32.31%)
Mutual labels:  login, auth
Appy Backend
A user system to bootstrap your app.
Stars: ✭ 96 (+47.69%)
Mutual labels:  login, users
Django Rest Registration
User-related REST API based on the awesome Django REST Framework
Stars: ✭ 240 (+269.23%)
Mutual labels:  login, auth

rocket_auth

rocket_auth provides a ready-to-use backend agnostic API for authentication management. It supports connections for SQLite and Postgresql. It lets you create, delete, and authenticate users. The available features are:

  • sqlx-sqlite: for interacting with a SQLite database using sqlx.
  • sqlx-postgres: for interacting with a Postgresql database with sqlx.
  • sqlx-mysql: for interacting with a MySql database with sqlx.
  • redis: for storing sessions on a redis server using redis.
  • rusqlite: for interacting with a SQLite database using rusqlite.
  • tokio-postgres: for interacting with a Postgresql database with tokio-postgres.

rocket_auth uses private cookies to store session data. This means that in order for cookies to be properly decrypted between launches, a secret_key must be set. For more information visit rocket's configuration guide.

To use rocket_auth include it as a dependency in your Cargo.toml file:

[dependencies.rocket_auth]
version = "0.4.0"
features = ["sqlx-sqlite"]

Quick overview

This crate provides three guards:

  • Auth: Manages authentication.
  • Session: It's used to retrieve session data from client cookies.
  • User: It restricts content, so it can be viewed by authenticated clients only.

It also includes two structs to be parsed from forms and json data:

  • Signup: Used to create new users.
  • Login: Used to authenticate users.

Finally, it has two structures for queries:

  • Users: It allows to query users to the database.
  • User: It is the response of a query.

The Auth guard allows to log in, log out, sign up, modify, and delete the currently (un)authenticated user. For more information see Auth. A working example:

use rocket::{get, post, form::Form, routes};
use rocket_auth::{Users, Error, Auth, Signup, Login};

#[post("/signup", data="<form>")]
async fn signup(form: Form<Signup>, auth: Auth<'_>) -> Result<&'static str, Error> {
    auth.signup(&form).await?;
    auth.login(&form.into());
    Ok("You signed up.")
}

#[post("/login", data="<form>")]
async fn login(form: Form<Login>, auth: Auth<'_>) -> Result<&'static str, Error>{
    auth.login(&form).await?;
    Ok("You're logged in.")
}

#[post("/logout")]
fn logout(auth: Auth<'_>) {
    auth.logout();
}
#[tokio::main]
async fn main() -> Result<(), Error>{
    let users = Users::open_sqlite("mydb.db").await?;

    rocket::build()
        .mount("/", routes![signup, login, logout])
        .manage(users)
        .launch();
    Ok(())
}

Users struct

The Users struct administers interactions with the database. It lets you query, create, modify and delete users. Unlike the Auth guard, a Users instance can manage any user in the database. Note that the Auth guards includes a Users instance stored on the public users field. So it is not necessary to retrieve Users when using Auth. A simple example of how to query a user with the Users struct:

use rocket_auth::Users;

#[get("/see-user/<id>")]
async fn see_user(id: i32, users: &State<Users>) -> String {
    let user = users.get_by_id(id).await.unwrap();
    format!("{}", json!(user))
}

A Users instance can be constructed by connecting it to the database with the methods open_sqlite, open_postgres. Furthermore, it can be constructed from a working connection.

User guard

The User guard can be used to restrict content, so that it can only be viewed by authenticated users. Additionally, you can use it to render special content if the client is authenticated or not.

#[get("/private-content")]
fn private_content(user: User) -> &'static str {
    "If you can see this, you are logged in."
}

#[get("/special-content")]
fn special_content(option: Option<User>) -> String {
    if let Some(user) = option {
        format!("hello, {}.", user.email())
    } else {
        "hello, anonymous user".into()
    }
}
#[get("/admins-only")]
fn admins_only(user: AdminUser) -> &'static str {
   "Hello administrator."
}

AdminUser guard

The AdminUser guard can be used analogously to User. It will restrict content, so that it can be viewed by admins only.

# use rocket::*;
# use rocket_auth::AdminUser;
#[get("/admin-panel")]
fn admin_panel(user: AdminUser) -> String {
   format!("Hello {}.", user.email());
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].