//! Device tokens for the Android app: store only the SHA-256 of an API token,
//! as the email-verification tokens already are (and as party-slut does).
//!
//! Existing rows are hashed in place, so a token handed out before this
//! migration keeps working; it just can't be shown again.
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
let db = manager.get_connection();
db.execute_unprepared("ALTER TABLE api_tokens ADD COLUMN token_hash VARCHAR(64)").await?;
db.execute_unprepared(
"UPDATE api_tokens SET token_hash = encode(sha256(convert_to(token, 'UTF8')), 'hex')",
)
.await?;
db.execute_unprepared("ALTER TABLE api_tokens ALTER COLUMN token_hash SET NOT NULL").await?;
db.execute_unprepared("ALTER TABLE api_tokens DROP COLUMN token").await?;
db.execute_unprepared(
"CREATE UNIQUE INDEX idx_api_tokens_token_hash ON api_tokens (token_hash)",
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// A hash can't be turned back into a token: going down revokes every
// device (the rows go, the old column comes back empty).
let db = manager.get_connection();
db.execute_unprepared("DELETE FROM api_tokens").await?;
db.execute_unprepared("DROP INDEX IF EXISTS idx_api_tokens_token_hash").await?;
db.execute_unprepared("ALTER TABLE api_tokens DROP COLUMN token_hash").await?;
db.execute_unprepared("ALTER TABLE api_tokens ADD COLUMN token VARCHAR(128) NOT NULL UNIQUE").await?;
Ok(())
}
}
neiam
/recourse
public · Issues · Pulls · Labels · Forks · Compare · Actions running · Badges · Packages
⭐
Log in to mark this repository.
1.8 KiB · text
History
63417ef