Add token management
This commit is contained in:
parent
d622eba6c3
commit
c0b5128777
@ -0,0 +1 @@
|
||||
ALTER TABLE tokens DROP COLUMN bind_token;
|
@ -0,0 +1 @@
|
||||
ALTER TABLE tokens ADD COLUMN bind_token UUID;
|
@ -11,6 +11,7 @@ pub mod comments;
|
||||
pub mod issue_assignees;
|
||||
pub mod issues;
|
||||
pub mod projects;
|
||||
pub mod tokens;
|
||||
pub mod users;
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
|
58
jirs-server/src/db/tokens.rs
Normal file
58
jirs-server/src/db/tokens.rs
Normal file
@ -0,0 +1,58 @@
|
||||
use actix::{Handler, Message};
|
||||
use diesel::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use jirs_data::UserId;
|
||||
|
||||
use crate::db::DbExecutor;
|
||||
use crate::errors::ServiceErrors;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct FindBindToken {
|
||||
pub token: Uuid,
|
||||
}
|
||||
|
||||
impl Message for FindBindToken {
|
||||
type Result = Result<crate::models::Token, ServiceErrors>;
|
||||
}
|
||||
|
||||
impl Handler<FindBindToken> for DbExecutor {
|
||||
type Result = Result<crate::models::Token, ServiceErrors>;
|
||||
|
||||
fn handle(&mut self, msg: FindBindToken, _: &mut Self::Context) -> Self::Result {
|
||||
use crate::schema::tokens::dsl::{bind_token, tokens};
|
||||
let conn = &self
|
||||
.0
|
||||
.get()
|
||||
.map_err(|_| ServiceErrors::DatabaseConnectionLost)?;
|
||||
|
||||
let token: crate::models::Token = tokens
|
||||
.filter(bind_token.eq(msg.token))
|
||||
.first(conn)
|
||||
.map_err(|_e| {
|
||||
ServiceErrors::RecordNotFound(format!("token for {}", msg.access_token))
|
||||
})?;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct CreateBindToken {
|
||||
pub user_id: UserId,
|
||||
}
|
||||
|
||||
impl Message for CreateBindToken {
|
||||
type Result = Result<crate::models::Token, ServiceErrors>;
|
||||
}
|
||||
|
||||
impl Handler<CreateBindToken> for DbExecutor {
|
||||
type Result = Result<crate::models::Token, ServiceErrors>;
|
||||
|
||||
fn handle(&mut self, msg: CreateBindToken, _: &mut Self::Context) -> Self::Result {
|
||||
use crate::schema::tokens::dsl::{access_token, tokens};
|
||||
let conn = &self
|
||||
.0
|
||||
.get()
|
||||
.map_err(|_| ServiceErrors::DatabaseConnectionLost)?;
|
||||
}
|
||||
}
|
@ -213,6 +213,7 @@ pub struct Token {
|
||||
pub refresh_token: Uuid,
|
||||
pub created_at: NaiveDateTime,
|
||||
pub updated_at: NaiveDateTime,
|
||||
pub bind_token: Option<Uuid>,
|
||||
}
|
||||
|
||||
impl Into<jirs_data::Token> for Token {
|
||||
|
@ -286,6 +286,12 @@ table! {
|
||||
///
|
||||
/// (Automatically generated by Diesel.)
|
||||
updated_at -> Timestamp,
|
||||
/// The `bind_token` column of the `tokens` table.
|
||||
///
|
||||
/// Its SQL type is `Nullable<Uuid>`.
|
||||
///
|
||||
/// (Automatically generated by Diesel.)
|
||||
bind_token -> Nullable<Uuid>,
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user