feat: make sure tokens are not expired

token lifetime is configured per-session serverside
This commit is contained in:
əlemi 2023-03-06 18:52:47 +01:00
parent fc22ed413f
commit 0408dc8593
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 14 additions and 2 deletions

View file

@ -31,6 +31,10 @@ struct ConfigArgs {
#[arg(short, long, default_value = "127.0.0.1:26656")]
bind_addr: String,
/// Access tokens lifetime, in seconds
#[arg(long, default_value_t = 3600)]
token_lifetime: u32,
/// Valid time for join requests, in seconds
#[arg(short, long, default_value_t = 10)]
time_window: u32,

View file

@ -13,7 +13,12 @@ pub async fn validate(State(state): State<AppState>, Json(payload): Json<proto::
entities::token::Column::AccessToken.eq(payload.accessToken)
).one(&state.db).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
if let Some(_t) = token {
if let Some(t) = token {
if Utc::now() - t.created_at > Duration::seconds(state.cfg.token_lifetime as i64) {
warn!(target: "AUTH", "[VALIDATE] expired token!");
return Err(StatusCode::UNAUTHORIZED);
}
Ok(StatusCode::NO_CONTENT)
} else {
warn!(target: "AUTH", "[VALIDATE] invalid token!");

View file

@ -22,7 +22,10 @@ pub async fn join(State(state): State<AppState>, Json(payload): Json<proto::Join
).all(&state.db).await
.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, proto::Error::simple("db error").json()))?;
if tokens.iter().any(|x| x.access_token == payload.accessToken) {
if tokens.iter().any(|x| {
x.access_token == payload.accessToken
&& Utc::now() - x.created_at < Duration::seconds(state.cfg.token_lifetime as i64)
}) {
state.store.lock().await.insert(payload.selectedProfile, JoinAttempt::new(payload.serverId.clone()));
info!(target: "SESSION", "[JOIN] user {} has joined server {}", payload.selectedProfile, payload.serverId);
Ok(Json(()))