Skip to content

Commit

Permalink
add list_logins endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Sep 22, 2023
1 parent f591ee1 commit 053dff9
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 6 deletions.
14 changes: 14 additions & 0 deletions crates/api/src/local_user/list_logins.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use actix_web::web::{Data, Json};
use lemmy_api_common::context::LemmyContext;
use lemmy_db_schema::source::login_token::LoginToken;
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::LemmyError;

pub async fn list_logins(
context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<Vec<LoginToken>>, LemmyError> {
let logins = LoginToken::list(&mut context.pool(), local_user_view.local_user.id).await?;

Ok(Json(logins))

This comment has been minimized.

Copy link
@phiresky

phiresky Sep 22, 2023

Collaborator

you'll probably not want the actual token to be part of the response because otherwise you can steal the other sessions if you have one (if I understand this change correctly)

This comment has been minimized.

Copy link
@Nutomic

Nutomic Sep 22, 2023

Author Member

Thats true, I will mark the field as serde(skip).

}
1 change: 1 addition & 0 deletions crates/api/src/local_user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod change_password_after_reset;
pub mod generate_totp_secret;
pub mod get_captcha;
pub mod list_banned;
pub mod list_logins;
pub mod login;
pub mod logout;
pub mod notifications;
Expand Down
7 changes: 4 additions & 3 deletions crates/api_common/src/claims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ impl Claims {
let user_agent = req
.headers()
.get(USER_AGENT)
.map(|ua| ua.to_str().ok())
.flatten()
.and_then(|ua| ua.to_str().ok())
.map(ToString::to_string);
let form = LoginTokenCreateForm {
token: token.clone(),
Expand All @@ -77,6 +76,7 @@ mod tests {
#![allow(clippy::indexing_slicing)]

use crate::{claims::Claims, context::LemmyContext};
use actix_web::test::TestRequest;
use lemmy_db_schema::{
source::{
instance::Instance,
Expand Down Expand Up @@ -126,7 +126,8 @@ mod tests {

let inserted_local_user = LocalUser::create(pool, &local_user_form).await.unwrap();

let jwt = Claims::generate(inserted_local_user.id, &context)
let req = TestRequest::default().to_http_request();
let jwt = Claims::generate(inserted_local_user.id, req, &context)
.await
.unwrap();

Expand Down
12 changes: 12 additions & 0 deletions crates/db_schema/src/impls/login_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ impl LoginToken {
.await
}

pub async fn list(
pool: &mut DbPool<'_>,
user_id_: LocalUserId,
) -> Result<Vec<LoginToken>, Error> {
let conn = &mut get_conn(pool).await?;

login_token
.filter(user_id.eq(user_id_))
.get_results(conn)
.await
}

/// Invalidate specific token on user logout.
pub async fn invalidate(pool: &mut DbPool<'_>, token_: &str) -> Result<usize, Error> {
let conn = &mut get_conn(pool).await?;
Expand Down
3 changes: 2 additions & 1 deletion crates/db_schema/src/source/login_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use crate::newtypes::LocalUserId;
#[cfg(feature = "full")]
use crate::schema::login_token;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

#[derive(Clone, PartialEq, Eq, Debug)]
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "full", derive(Queryable, Identifiable))]
#[cfg_attr(feature = "full", diesel(table_name = login_token))]
pub struct LoginToken {
Expand Down
4 changes: 3 additions & 1 deletion src/api_routes_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use lemmy_api::{
generate_totp_secret::generate_totp_secret,
get_captcha::get_captcha,
list_banned::list_banned_users,
list_logins::list_logins,
login::login,
logout::logout,
notifications::{
Expand Down Expand Up @@ -293,7 +294,8 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
.route("/verify_email", web::post().to(verify_email))
.route("/leave_admin", web::post().to(leave_admin))
.route("/totp/generate", web::post().to(generate_totp_secret))
.route("/totp/update", web::post().to(update_totp)),
.route("/totp/update", web::post().to(update_totp))
.route("/list_logins", web::get().to(list_logins)),
)
// Admin Actions
.service(
Expand Down
4 changes: 3 additions & 1 deletion src/session_middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ mod tests {
#![allow(clippy::indexing_slicing)]

use super::*;
use actix_web::test::TestRequest;
use lemmy_db_schema::{
source::{
instance::Instance,
Expand Down Expand Up @@ -177,7 +178,8 @@ mod tests {

let inserted_local_user = LocalUser::create(pool, &local_user_form).await.unwrap();

let jwt = Claims::generate(inserted_local_user.id, &context)
let req = TestRequest::default().to_http_request();
let jwt = Claims::generate(inserted_local_user.id, req, &context)
.await
.unwrap();

Expand Down

0 comments on commit 053dff9

Please sign in to comment.