feat: make sure tokens are not expired
token lifetime is configured per-session serverside
This commit is contained in:
parent
fc22ed413f
commit
0408dc8593
3 changed files with 14 additions and 2 deletions
|
@ -31,6 +31,10 @@ struct ConfigArgs {
|
||||||
#[arg(short, long, default_value = "127.0.0.1:26656")]
|
#[arg(short, long, default_value = "127.0.0.1:26656")]
|
||||||
bind_addr: String,
|
bind_addr: String,
|
||||||
|
|
||||||
|
/// Access tokens lifetime, in seconds
|
||||||
|
#[arg(long, default_value_t = 3600)]
|
||||||
|
token_lifetime: u32,
|
||||||
|
|
||||||
/// Valid time for join requests, in seconds
|
/// Valid time for join requests, in seconds
|
||||||
#[arg(short, long, default_value_t = 10)]
|
#[arg(short, long, default_value_t = 10)]
|
||||||
time_window: u32,
|
time_window: u32,
|
||||||
|
|
|
@ -13,7 +13,12 @@ pub async fn validate(State(state): State<AppState>, Json(payload): Json<proto::
|
||||||
entities::token::Column::AccessToken.eq(payload.accessToken)
|
entities::token::Column::AccessToken.eq(payload.accessToken)
|
||||||
).one(&state.db).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
).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)
|
Ok(StatusCode::NO_CONTENT)
|
||||||
} else {
|
} else {
|
||||||
warn!(target: "AUTH", "[VALIDATE] invalid token!");
|
warn!(target: "AUTH", "[VALIDATE] invalid token!");
|
||||||
|
|
|
@ -22,7 +22,10 @@ pub async fn join(State(state): State<AppState>, Json(payload): Json<proto::Join
|
||||||
).all(&state.db).await
|
).all(&state.db).await
|
||||||
.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, proto::Error::simple("db error").json()))?;
|
.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()));
|
state.store.lock().await.insert(payload.selectedProfile, JoinAttempt::new(payload.serverId.clone()));
|
||||||
info!(target: "SESSION", "[JOIN] user {} has joined server {}", payload.selectedProfile, payload.serverId);
|
info!(target: "SESSION", "[JOIN] user {} has joined server {}", payload.selectedProfile, payload.serverId);
|
||||||
Ok(Json(()))
|
Ok(Json(()))
|
||||||
|
|
Loading…
Reference in a new issue