1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
use crate::prelude::*;
use rocket::http::Status;
use rocket::http::{Cookie, CookieJar};
use rocket::request::FromRequest;
use rocket::request::Outcome;
use rocket::Request;
use rocket::State;
use serde_json::json;
use std::time::Duration;
/// 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:
/// ```rust,no_run
///
/// use rocket::{*, form::Form};
/// use rocket_auth::{Users, Error, Auth, Signup, Login};
///
/// #[post("/signup", data="<form>")]
/// async fn signup(form: Form<Signup>, auth: Auth<'_>) {
/// auth.signup(&form).await;
/// auth.login(&form.into());
/// }
///
/// #[post("/login", data="<form>")]
/// fn login(form: Form<Login>, auth: Auth) {
/// auth.login(&form);
/// }
///
/// #[get("/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()
/// .await;
/// Ok(())
/// }
/// ```
#[allow(missing_docs)]
pub struct Auth<'a> {
/// `Auth` includes in its fields a [`Users`] instance. Therefore, it is not necessary to retrieve `Users` when using this guard.
pub users: &'a State<Users>,
pub cookies: &'a CookieJar<'a>,
pub session: Option<Session>,
}
#[async_trait]
impl<'r> FromRequest<'r> for Auth<'r> {
type Error = Error;
async fn from_request(req: &'r Request<'_>) -> Outcome<Auth<'r>, Error> {
let session: Option<Session> = if let Outcome::Success(users) = req.guard().await {
Some(users)
} else {
None
};
let users: &State<Users> = if let Outcome::Success(users) = req.guard().await {
users
} else {
return Outcome::Failure((Status::InternalServerError, Error::UnmanagedStateError));
};
Outcome::Success(Auth {
users,
session,
cookies: req.cookies(),
})
}
}
impl<'a> Auth<'a> {
/// Logs in the user through a parsed form or json.
/// The session is set to expire in one year by default.
/// For a custom expiration date use [`Auth::login_for`].
/// ```rust
/// # use rocket::{get, post, form::Form};
/// # use rocket_auth::{Auth, Login};
/// #[post("/login", data="<form>")]
/// fn login(form: Form<Login>, auth: Auth) {
/// auth.login(&form);
/// }
/// ```
#[throws(Error)]
pub async fn login(&self, form: &Login) {
let key = self.users.login(form).await?;
let user = self.users.get_by_email(&form.email).await?;
let session = Session {
id: user.id,
email: user.email,
auth_key: key,
time_stamp: now(),
};
let to_str = format!("{}", json!(session));
self.cookies.add_private(Cookie::new("rocket_auth", to_str));
}
/// Logs a user in for the specified period of time.
/// ```rust
/// # use rocket::{post, form::Form};
/// # use rocket_auth::{Login, Auth};
/// # use std::time::Duration;
/// #[post("/login", data="<form>")]
/// fn login(form: Form<Login>, auth: Auth) {
/// let one_hour = Duration::from_secs(60 * 60);
/// auth.login_for(&form, one_hour);
/// }
/// ```
#[throws(Error)]
pub async fn login_for(&self, form: &Login, time: Duration) {
let key = self.users.login_for(form, time).await?;
let user = self.users.get_by_email(&form.email).await?;
let session = Session {
id: user.id,
email: user.email,
auth_key: key,
time_stamp: now(),
};
let to_str = format!("{}", json!(session));
let cookie = Cookie::new("rocket_auth", to_str);
self.cookies.add_private(cookie);
}
/// Creates a new user from a form or a json. The user will not be authenticated by default.
/// In order to authenticate the user, cast the signup form to a login form or use `signup_for`.
/// ```rust
/// # use rocket::{post, form::Form};
/// # use rocket_auth::{Auth, Signup, Error};
/// # use std::time::Duration;
/// #[post("/signup", data="<form>")]
/// async fn signup(form: Form<Signup>, auth: Auth<'_>) -> Result<&'static str, Error>{
/// auth.signup(&form).await?;
/// auth.login(&form.into()).await?;
/// Ok("Logged in")
/// }
/// ```
#[throws(Error)]
pub async fn signup(&self, form: &Signup) {
self.users.signup(form).await?;
}
/// Creates a new user from a form or a json.
/// The session will last the specified period of time.
/// ```rust
/// # use rocket::{post, form::Form};
/// # use rocket_auth::{Auth, Signup};
/// # use std::time::Duration;
/// #[post("/signup", data="<form>")]
/// fn signup_for(form: Form<Signup>, auth: Auth) {
/// let one_hour = Duration::from_secs(60 * 60);
/// auth.signup_for(&form, one_hour);
/// }
/// ```
#[throws(Error)]
pub async fn signup_for(&self, form: &Signup, time: Duration) {
self.users.signup(form).await?;
self.login_for(&form.clone().into(), time).await?;
}
///
///
/// It allows to know if the current client is authenticated or not.
/// ```rust
/// # use rocket::{get};
/// # use rocket_auth::{Auth};
/// #[get("/am-I-authenticated")]
/// fn is_auth(auth: Auth<'_>) -> &'static str {
/// if auth.is_auth() {
/// "Yes you are."
/// } else {
/// "nope."
/// }
/// }
/// # fn main() {}
/// ```
pub fn is_auth(&self) -> bool {
if let Some(session) = &self.session {
self.users.is_auth(session)
} else {
false
}
}
/// It retrieves the current logged user.
/// ```
/// # use rocket::get;
/// # use rocket_auth::Auth;
/// #[get("/display-me")]
/// async fn display_me(auth: Auth<'_>) -> String {
/// format!("{:?}", auth.get_user().await)
/// }
/// ```
pub async fn get_user(&self) -> Option<User> {
if !self.is_auth() {
return None;
}
let id = self.session.as_ref()?.id;
if let Ok(user) = self.users.get_by_id(id).await {
Some(user)
} else {
None
}
}
/// Logs the currently authenticated user out.
/// ```rust
/// # use rocket::get;
/// # use rocket_auth::Auth;
/// #[get("/logout")]
/// fn logout(auth: Auth) {
/// auth.logout();
/// }
/// ```
#[throws(Error)]
pub fn logout(&self) {
let session = self.get_session()?;
self.users.logout(session)?;
self.cookies.remove_private(Cookie::named("rocket_auth"));
}
/// Deletes the account of the currently authenticated user.
/// ```rust
/// # use rocket::get;
/// # use rocket_auth::Auth;
/// #[get("/delete-my-account")]
/// fn delete(auth: Auth) {
/// auth.delete();
/// }
/// ```
#[throws(Error)]
pub async fn delete(&self) {
if self.is_auth() {
let session = self.get_session()?;
self.users.delete(session.id).await?;
self.cookies.remove_private(Cookie::named("rocket_auth"));
} else {
throw!(Error::UnauthenticatedError)
}
}
/// Changes the password of the currently authenticated user
/// ```
/// # use rocket_auth::Auth;
/// # use rocket::post;
/// # #[post("/change")]
/// # fn example(auth: Auth<'_>) {
/// auth.change_password("new password");
/// # }
/// ```
#[throws(Error)]
pub async fn change_password(&self, password: &str) {
if self.is_auth() {
let session = self.get_session()?;
let mut user = self.users.get_by_id(session.id).await?;
user.set_password(password)?;
self.users.modify(&user).await?;
} else {
throw!(Error::UnauthorizedError)
}
}
/// Changes the email of the currently authenticated user
/// ```
/// # use rocket_auth::Auth;
/// # fn func(auth: Auth) {
/// auth.change_email("new@email.com".into());
/// # }
/// ```
#[throws(Error)]
pub async fn change_email(&self, email: String) {
if self.is_auth() {
if !validator::validate_email(&email) {
throw!(Error::InvalidEmailAddressError)
}
let session = self.get_session()?;
let mut user = self.users.get_by_id(session.id).await?;
user.email = email;
self.users.modify(&user).await?;
} else {
throw!(Error::UnauthorizedError)
}
}
/// This method is useful when the function returns a Result type.
/// It is intended to be used primarily
/// with the `?` operator.
/// ```
/// # fn func(auth: rocket_auth::Auth) -> Result<(), rocket_auth::Error> {
/// auth.get_session()?;
/// # Ok(())
/// # }
/// ```
pub fn get_session(&self) -> Result<&Session> {
let session = self.session.as_ref().ok_or(Error::UnauthenticatedError)?;
Ok(session)
}
}