Skip to content

Commit

Permalink
feat(annim): load token from ANNIM_AUTH_TOKEN
Browse files Browse the repository at this point in the history
  • Loading branch information
Yesterday17 committed Sep 2, 2024
1 parent 7847e48 commit e7c3b25
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion annim/src/auth.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use std::sync::LazyLock;

/// https://github.com/async-graphql/examples/blob/0c4e5e29e97a41c8877c126cbcefb82721ae81af/models/token/src/lib.rs
use async_graphql::{Context, Data, Result};
use serde::Deserialize;

static TOKEN: LazyLock<String> =
LazyLock::new(|| std::env::var("ANNIM_AUTH_TOKEN").unwrap_or_else(|_| "114514".to_string()));

pub struct AuthToken(pub String);

pub async fn on_connection_init(value: serde_json::Value) -> Result<Data> {
Expand All @@ -25,7 +30,7 @@ pub fn require_auth<'ctx>(ctx: &Context<'ctx>) -> anyhow::Result<()> {
let token = ctx
.data::<AuthToken>()
.map_err(|_| anyhow::anyhow!("Token is required"))?;
if token.0 != "114514" {
if token.0 != TOKEN.as_str() {
anyhow::bail!("Invalid token");
}

Expand Down

0 comments on commit e7c3b25

Please sign in to comment.