2020-01-21 07:02:03 +01:00
|
|
|
use crate::{db_conn::DbConn, schema::api_tokens, Error, Result};
|
2018-10-22 15:30:04 +02:00
|
|
|
use chrono::NaiveDateTime;
|
|
|
|
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
|
2018-10-22 16:09:47 +02:00
|
|
|
use rocket::{
|
|
|
|
http::Status,
|
2018-11-24 12:44:17 +01:00
|
|
|
request::{self, FromRequest, Request},
|
|
|
|
Outcome,
|
2018-10-22 16:09:47 +02:00
|
|
|
};
|
2018-10-22 15:30:04 +02:00
|
|
|
|
|
|
|
#[derive(Clone, Queryable)]
|
|
|
|
pub struct ApiToken {
|
|
|
|
pub id: i32,
|
|
|
|
pub creation_date: NaiveDateTime,
|
|
|
|
pub value: String,
|
|
|
|
|
|
|
|
/// Scopes, separated by +
|
|
|
|
/// Global scopes are read and write
|
|
|
|
/// and both can be limited to an endpoint by affixing them with :ENDPOINT
|
|
|
|
///
|
|
|
|
/// Examples :
|
|
|
|
///
|
|
|
|
/// read
|
|
|
|
/// read+write
|
|
|
|
/// read:posts
|
|
|
|
/// read:posts+write:posts
|
|
|
|
pub scopes: String,
|
|
|
|
pub app_id: i32,
|
|
|
|
pub user_id: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Insertable)]
|
|
|
|
#[table_name = "api_tokens"]
|
|
|
|
pub struct NewApiToken {
|
|
|
|
pub value: String,
|
|
|
|
pub scopes: String,
|
|
|
|
pub app_id: i32,
|
|
|
|
pub user_id: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ApiToken {
|
|
|
|
get!(api_tokens);
|
|
|
|
insert!(api_tokens, NewApiToken);
|
2018-11-26 10:21:52 +01:00
|
|
|
find_by!(api_tokens, find_by_value, value as &str);
|
2018-10-22 16:09:47 +02:00
|
|
|
|
2018-10-23 11:37:24 +02:00
|
|
|
pub fn can(&self, what: &'static str, scope: &'static str) -> bool {
|
2018-10-22 16:09:47 +02:00
|
|
|
let full_scope = what.to_owned() + ":" + scope;
|
|
|
|
for s in self.scopes.split('+') {
|
|
|
|
if s == what || s == full_scope {
|
2018-11-24 12:44:17 +01:00
|
|
|
return true;
|
2018-10-22 16:09:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2018-10-29 21:21:00 +01:00
|
|
|
pub fn can_read(&self, scope: &'static str) -> bool {
|
|
|
|
self.can("read", scope)
|
2018-10-22 16:09:47 +02:00
|
|
|
}
|
|
|
|
|
2018-10-29 21:21:00 +01:00
|
|
|
pub fn can_write(&self, scope: &'static str) -> bool {
|
|
|
|
self.can("write", scope)
|
2018-10-22 16:09:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum TokenError {
|
|
|
|
/// The Authorization header was not present
|
|
|
|
NoHeader,
|
|
|
|
|
|
|
|
/// The type of the token was not specified ("Basic" or "Bearer" for instance)
|
|
|
|
NoType,
|
|
|
|
|
|
|
|
/// No value was provided
|
|
|
|
NoValue,
|
|
|
|
|
|
|
|
/// Error while connecting to the database to retrieve all the token metadata
|
|
|
|
DbError,
|
|
|
|
}
|
|
|
|
|
2018-10-22 16:09:47 +02:00
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for ApiToken {
|
2018-12-29 09:36:07 +01:00
|
|
|
type Error = TokenError;
|
2018-10-22 16:09:47 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
fn from_request(request: &'a Request<'r>) -> request::Outcome<ApiToken, TokenError> {
|
2018-10-22 16:09:47 +02:00
|
|
|
let headers: Vec<_> = request.headers().get("Authorization").collect();
|
|
|
|
if headers.len() != 1 {
|
2018-12-29 09:36:07 +01:00
|
|
|
return Outcome::Failure((Status::BadRequest, TokenError::NoHeader));
|
2018-10-22 16:09:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut parsed_header = headers[0].split(' ');
|
2021-11-27 23:53:13 +01:00
|
|
|
let auth_type = parsed_header
|
|
|
|
.next()
|
|
|
|
.map_or_else::<rocket::Outcome<&str, _, ()>, _, _>(
|
|
|
|
|| Outcome::Failure((Status::BadRequest, TokenError::NoType)),
|
|
|
|
Outcome::Success,
|
|
|
|
)?;
|
|
|
|
let val = parsed_header
|
|
|
|
.next()
|
|
|
|
.map_or_else::<rocket::Outcome<&str, _, ()>, _, _>(
|
|
|
|
|| Outcome::Failure((Status::BadRequest, TokenError::NoValue)),
|
|
|
|
Outcome::Success,
|
|
|
|
)?;
|
2018-10-22 16:09:47 +02:00
|
|
|
|
|
|
|
if auth_type == "Bearer" {
|
2019-03-20 17:56:17 +01:00
|
|
|
let conn = request
|
|
|
|
.guard::<DbConn>()
|
|
|
|
.map_failure(|_| (Status::InternalServerError, TokenError::DbError))?;
|
2023-01-02 18:45:13 +01:00
|
|
|
if let Ok(token) = ApiToken::find_by_value(&conn, val) {
|
2018-10-22 16:09:47 +02:00
|
|
|
return Outcome::Success(token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
Outcome::Forward(())
|
2018-10-22 16:09:47 +02:00
|
|
|
}
|
2018-10-22 15:30:04 +02:00
|
|
|
}
|