Moving secrets to context.

remove_settings_and_secret_singletons
Dessalines 2021-09-22 11:57:09 -04:00
parent 30839365dc
commit 37e64684ce
39 changed files with 192 additions and 126 deletions

View File

@ -32,7 +32,8 @@ impl Perform for MarkCommentAsRead {
_websocket_id: Option<ConnectionId>,
) -> Result<CommentResponse, LemmyError> {
let data: &MarkCommentAsRead = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let comment_id = data.comment_id;
let orig_comment = blocking(context.pool(), move |conn| {
@ -88,7 +89,8 @@ impl Perform for SaveComment {
_websocket_id: Option<ConnectionId>,
) -> Result<CommentResponse, LemmyError> {
let data: &SaveComment = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let comment_saved_form = CommentSavedForm {
comment_id: data.comment_id,
@ -132,7 +134,8 @@ impl Perform for CreateCommentLike {
websocket_id: Option<ConnectionId>,
) -> Result<CommentResponse, LemmyError> {
let data: &CreateCommentLike = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let mut recipient_ids = Vec::<LocalUserId>::new();

View File

@ -32,7 +32,8 @@ impl Perform for CreateCommentReport {
websocket_id: Option<ConnectionId>,
) -> Result<CreateCommentReportResponse, LemmyError> {
let data: &CreateCommentReport = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
// check size of report and check for whitespace
let reason = data.reason.trim();
@ -96,7 +97,8 @@ impl Perform for ResolveCommentReport {
websocket_id: Option<ConnectionId>,
) -> Result<ResolveCommentReportResponse, LemmyError> {
let data: &ResolveCommentReport = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let report_id = data.report_id;
let report = blocking(context.pool(), move |conn| {
@ -149,7 +151,8 @@ impl Perform for ListCommentReports {
websocket_id: Option<ConnectionId>,
) -> Result<ListCommentReportsResponse, LemmyError> {
let data: &ListCommentReports = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let person_id = local_user_view.person.id;
let community_id = data.community;

View File

@ -53,7 +53,8 @@ impl Perform for FollowCommunity {
_websocket_id: Option<ConnectionId>,
) -> Result<CommunityResponse, LemmyError> {
let data: &FollowCommunity = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let community_id = data.community_id;
let community = blocking(context.pool(), move |conn| {
@ -121,7 +122,8 @@ impl Perform for BlockCommunity {
_websocket_id: Option<ConnectionId>,
) -> Result<BlockCommunityResponse, LemmyError> {
let data: &BlockCommunity = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let community_id = data.community_id;
let person_id = local_user_view.person.id;
@ -181,7 +183,8 @@ impl Perform for BanFromCommunity {
websocket_id: Option<ConnectionId>,
) -> Result<BanFromCommunityResponse, LemmyError> {
let data: &BanFromCommunity = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let community_id = data.community_id;
let banned_person_id = data.person_id;
@ -314,7 +317,8 @@ impl Perform for AddModToCommunity {
websocket_id: Option<ConnectionId>,
) -> Result<AddModToCommunityResponse, LemmyError> {
let data: &AddModToCommunity = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let community_id = data.community_id;
@ -397,7 +401,8 @@ impl Perform for TransferCommunity {
_websocket_id: Option<ConnectionId>,
) -> Result<GetCommunityResponse, LemmyError> {
let data: &TransferCommunity = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let site_creator_id = blocking(context.pool(), move |conn| {
Site::read(conn, 1).map(|s| s.creator_id)

View File

@ -190,7 +190,7 @@ mod tests {
use lemmy_api_common::check_validator_time;
use lemmy_db_queries::{
establish_unpooled_connection,
source::{local_user::LocalUser_, secret::SecretSingleton},
source::{local_user::LocalUser_, secret::Secret_},
Crud,
};
use lemmy_db_schema::source::{
@ -203,6 +203,7 @@ mod tests {
#[test]
fn test_should_not_validate_user_token_after_password_change() {
let conn = establish_unpooled_connection();
let secret = Secret::init(&conn).unwrap();
let new_person = PersonForm {
name: "Gerry9812".into(),
@ -219,9 +220,8 @@ mod tests {
let inserted_local_user = LocalUser::create(&conn, &local_user_form).unwrap();
let jwt_secret = Secret::get().jwt_secret;
let jwt = Claims::jwt(inserted_local_user.id.0, &jwt_secret).unwrap();
let claims = Claims::decode(&jwt, jwt_secret.as_ref()).unwrap().claims;
let jwt = Claims::jwt(inserted_local_user.id.0, &secret.jwt_secret).unwrap();
let claims = Claims::decode(&jwt, &secret.jwt_secret).unwrap().claims;
let check = check_validator_time(&inserted_local_user.validator_time, &claims);
assert!(check.is_ok());

View File

@ -25,7 +25,6 @@ use lemmy_db_queries::{
person_mention::PersonMention_,
post::Post_,
private_message::PrivateMessage_,
secret::SecretSingleton,
},
Blockable,
Crud,
@ -44,7 +43,6 @@ use lemmy_db_schema::{
person_mention::*,
post::Post,
private_message::PrivateMessage,
secret::Secret,
site::*,
},
};
@ -105,9 +103,11 @@ impl Perform for Login {
}
// Return the jwt
let jwt_secret = Secret::get().jwt_secret;
Ok(LoginResponse {
jwt: Claims::jwt(local_user_view.local_user.id.0, &jwt_secret)?,
jwt: Claims::jwt(
local_user_view.local_user.id.0,
&context.secret().jwt_secret,
)?,
})
}
}
@ -167,7 +167,8 @@ impl Perform for SaveUserSettings {
_websocket_id: Option<ConnectionId>,
) -> Result<LoginResponse, LemmyError> {
let data: &SaveUserSettings = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let avatar = diesel_option_overwrite_to_url(&data.avatar)?;
let banner = diesel_option_overwrite_to_url(&data.banner)?;
@ -271,9 +272,8 @@ impl Perform for SaveUserSettings {
};
// Return the jwt
let jwt_secret = Secret::get().jwt_secret;
Ok(LoginResponse {
jwt: Claims::jwt(updated_local_user.id.0, &jwt_secret)?,
jwt: Claims::jwt(updated_local_user.id.0, &context.secret().jwt_secret)?,
})
}
}
@ -288,7 +288,8 @@ impl Perform for ChangePassword {
_websocket_id: Option<ConnectionId>,
) -> Result<LoginResponse, LemmyError> {
let data: &ChangePassword = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
password_length_check(&data.new_password)?;
@ -315,9 +316,8 @@ impl Perform for ChangePassword {
.await??;
// Return the jwt
let jwt_secret = Secret::get().jwt_secret;
Ok(LoginResponse {
jwt: Claims::jwt(updated_local_user.id.0, &jwt_secret)?,
jwt: Claims::jwt(updated_local_user.id.0, &context.secret().jwt_secret)?,
})
}
}
@ -332,7 +332,8 @@ impl Perform for AddAdmin {
websocket_id: Option<ConnectionId>,
) -> Result<AddAdminResponse, LemmyError> {
let data: &AddAdmin = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
// Make sure user is an admin
is_admin(&local_user_view)?;
@ -394,7 +395,8 @@ impl Perform for BanPerson {
websocket_id: Option<ConnectionId>,
) -> Result<BanPersonResponse, LemmyError> {
let data: &BanPerson = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
// Make sure user is an admin
is_admin(&local_user_view)?;
@ -486,7 +488,8 @@ impl Perform for BlockPerson {
_websocket_id: Option<ConnectionId>,
) -> Result<BlockPersonResponse, LemmyError> {
let data: &BlockPerson = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let target_id = data.person_id;
let person_id = local_user_view.person.id;
@ -539,7 +542,8 @@ impl Perform for GetReplies {
_websocket_id: Option<ConnectionId>,
) -> Result<GetRepliesResponse, LemmyError> {
let data: &GetReplies = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
@ -576,7 +580,8 @@ impl Perform for GetPersonMentions {
_websocket_id: Option<ConnectionId>,
) -> Result<GetPersonMentionsResponse, LemmyError> {
let data: &GetPersonMentions = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
@ -610,7 +615,8 @@ impl Perform for MarkPersonMentionAsRead {
_websocket_id: Option<ConnectionId>,
) -> Result<PersonMentionResponse, LemmyError> {
let data: &MarkPersonMentionAsRead = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let person_mention_id = data.person_mention_id;
let read_person_mention = blocking(context.pool(), move |conn| {
@ -653,7 +659,8 @@ impl Perform for MarkAllAsRead {
_websocket_id: Option<ConnectionId>,
) -> Result<GetRepliesResponse, LemmyError> {
let data: &MarkAllAsRead = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let person_id = local_user_view.person.id;
let replies = blocking(context.pool(), move |conn| {
@ -775,9 +782,8 @@ impl Perform for PasswordChange {
.map_err(|_| ApiError::err("couldnt_update_user"))?;
// Return the jwt
let jwt_secret = Secret::get().jwt_secret;
Ok(LoginResponse {
jwt: Claims::jwt(updated_local_user.id.0, &jwt_secret)?,
jwt: Claims::jwt(updated_local_user.id.0, &context.secret().jwt_secret)?,
})
}
}
@ -792,7 +798,8 @@ impl Perform for GetReportCount {
websocket_id: Option<ConnectionId>,
) -> Result<GetReportCountResponse, LemmyError> {
let data: &GetReportCount = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let person_id = local_user_view.person.id;
let community_id = data.community;

View File

@ -38,7 +38,8 @@ impl Perform for CreatePostLike {
websocket_id: Option<ConnectionId>,
) -> Result<PostResponse, LemmyError> {
let data: &CreatePostLike = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
// Don't do a downvote if site has downvotes disabled
check_downvotes_enabled(data.score, context.pool()).await?;
@ -120,7 +121,8 @@ impl Perform for LockPost {
websocket_id: Option<ConnectionId>,
) -> Result<PostResponse, LemmyError> {
let data: &LockPost = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let post_id = data.post_id;
let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
@ -186,7 +188,8 @@ impl Perform for StickyPost {
websocket_id: Option<ConnectionId>,
) -> Result<PostResponse, LemmyError> {
let data: &StickyPost = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let post_id = data.post_id;
let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
@ -256,7 +259,8 @@ impl Perform for SavePost {
_websocket_id: Option<ConnectionId>,
) -> Result<PostResponse, LemmyError> {
let data: &SavePost = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let post_saved_form = PostSavedForm {
post_id: data.post_id,

View File

@ -39,7 +39,8 @@ impl Perform for CreatePostReport {
websocket_id: Option<ConnectionId>,
) -> Result<CreatePostReportResponse, LemmyError> {
let data: &CreatePostReport = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
// check size of report and check for whitespace
let reason = data.reason.trim();
@ -105,7 +106,8 @@ impl Perform for ResolvePostReport {
websocket_id: Option<ConnectionId>,
) -> Result<ResolvePostReportResponse, LemmyError> {
let data: &ResolvePostReport = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let report_id = data.report_id;
let report = blocking(context.pool(), move |conn| {
@ -157,7 +159,8 @@ impl Perform for ListPostReports {
websocket_id: Option<ConnectionId>,
) -> Result<ListPostReportsResponse, LemmyError> {
let data: &ListPostReports = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let person_id = local_user_view.person.id;
let community_id = data.community;

View File

@ -20,7 +20,8 @@ impl Perform for MarkPrivateMessageAsRead {
websocket_id: Option<ConnectionId>,
) -> Result<PrivateMessageResponse, LemmyError> {
let data: &MarkPrivateMessageAsRead = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
// Checking permissions
let private_message_id = data.private_message_id;

View File

@ -151,7 +151,8 @@ impl Perform for Search {
) -> Result<SearchResponse, LemmyError> {
let data: &Search = self;
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
let show_bot_accounts = local_user_view
@ -384,7 +385,8 @@ impl Perform for ResolveObject {
context: &Data<LemmyContext>,
_websocket_id: Option<ConnectionId>,
) -> Result<ResolveObjectResponse, LemmyError> {
let local_user_view = get_local_user_view_from_jwt_opt(&self.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt_opt(&self.auth, context.pool(), context.secret()).await?;
let res = search_by_apub_id(&self.q, context)
.await
.map_err(|_| ApiError::err("couldnt_find_object"))?;
@ -443,7 +445,8 @@ impl Perform for TransferSite {
_websocket_id: Option<ConnectionId>,
) -> Result<GetSiteResponse, LemmyError> {
let data: &TransferSite = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
is_admin(&local_user_view)?;
@ -504,7 +507,8 @@ impl Perform for GetSiteConfig {
_websocket_id: Option<ConnectionId>,
) -> Result<GetSiteConfigResponse, LemmyError> {
let data: &GetSiteConfig = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
// Only let admins read this
is_admin(&local_user_view)?;
@ -525,7 +529,8 @@ impl Perform for SaveSiteConfig {
_websocket_id: Option<ConnectionId>,
) -> Result<GetSiteConfigResponse, LemmyError> {
let data: &SaveSiteConfig = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
// Only let admins read this
is_admin(&local_user_view)?;

View File

@ -17,7 +17,8 @@ impl Perform for UserJoin {
websocket_id: Option<ConnectionId>,
) -> Result<UserJoinResponse, LemmyError> {
let data: &UserJoin = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
if let Some(ws_id) = websocket_id {
context.chat_server().do_send(JoinUserRoom {

View File

@ -11,7 +11,6 @@ use lemmy_db_queries::{
source::{
community::{CommunityModerator_, Community_},
person_block::PersonBlock_,
secret::SecretSingleton,
site::Site_,
},
Crud,
@ -246,9 +245,9 @@ pub async fn mark_post_as_read(
pub async fn get_local_user_view_from_jwt(
jwt: &str,
pool: &DbPool,
secret: &Secret,
) -> Result<LocalUserView, LemmyError> {
let jwt_secret = Secret::get().jwt_secret;
let claims = Claims::decode(jwt, &jwt_secret)
let claims = Claims::decode(jwt, &secret.jwt_secret)
.map_err(|_| ApiError::err("not_logged_in"))?
.claims;
let local_user_id = LocalUserId(claims.sub);
@ -285,9 +284,10 @@ pub fn check_validator_time(
pub async fn get_local_user_view_from_jwt_opt(
jwt: &Option<String>,
pool: &DbPool,
secret: &Secret,
) -> Result<Option<LocalUserView>, LemmyError> {
match jwt {
Some(jwt) => Ok(Some(get_local_user_view_from_jwt(jwt, pool).await?)),
Some(jwt) => Ok(Some(get_local_user_view_from_jwt(jwt, pool, secret).await?)),
None => Ok(None),
}
}
@ -295,9 +295,9 @@ pub async fn get_local_user_view_from_jwt_opt(
pub async fn get_local_user_settings_view_from_jwt(
jwt: &str,
pool: &DbPool,
secret: &Secret,
) -> Result<LocalUserSettingsView, LemmyError> {
let jwt_secret = Secret::get().jwt_secret;
let claims = Claims::decode(jwt, &jwt_secret)
let claims = Claims::decode(jwt, &secret.jwt_secret)
.map_err(|_| ApiError::err("not_logged_in"))?
.claims;
let local_user_id = LocalUserId(claims.sub);
@ -318,10 +318,11 @@ pub async fn get_local_user_settings_view_from_jwt(
pub async fn get_local_user_settings_view_from_jwt_opt(
jwt: &Option<String>,
pool: &DbPool,
secret: &Secret,
) -> Result<Option<LocalUserSettingsView>, LemmyError> {
match jwt {
Some(jwt) => Ok(Some(
get_local_user_settings_view_from_jwt(jwt, pool).await?,
get_local_user_settings_view_from_jwt(jwt, pool, secret).await?,
)),
None => Ok(None),
}

View File

@ -40,7 +40,8 @@ impl PerformCrud for CreateComment {
websocket_id: Option<ConnectionId>,
) -> Result<CommentResponse, LemmyError> {
let data: &CreateComment = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let content_slurs_removed = remove_slurs(&data.content.to_owned());

View File

@ -25,7 +25,8 @@ impl PerformCrud for DeleteComment {
websocket_id: Option<ConnectionId>,
) -> Result<CommentResponse, LemmyError> {
let data: &DeleteComment = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let comment_id = data.comment_id;
let orig_comment = blocking(context.pool(), move |conn| {
@ -102,7 +103,8 @@ impl PerformCrud for RemoveComment {
websocket_id: Option<ConnectionId>,
) -> Result<CommentResponse, LemmyError> {
let data: &RemoveComment = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let comment_id = data.comment_id;
let orig_comment = blocking(context.pool(), move |conn| {

View File

@ -17,7 +17,8 @@ impl PerformCrud for GetComments {
_websocket_id: Option<ConnectionId>,
) -> Result<GetCommentsResponse, LemmyError> {
let data: &GetComments = self;
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
let show_bot_accounts = local_user_view
.as_ref()

View File

@ -32,7 +32,8 @@ impl PerformCrud for EditComment {
websocket_id: Option<ConnectionId>,
) -> Result<CommentResponse, LemmyError> {
let data: &EditComment = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let comment_id = data.comment_id;
let orig_comment = blocking(context.pool(), move |conn| {

View File

@ -45,7 +45,8 @@ impl PerformCrud for CreateCommunity {
_websocket_id: Option<ConnectionId>,
) -> Result<CommunityResponse, LemmyError> {
let data: &CreateCommunity = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let site = blocking(context.pool(), move |conn| Site::read(conn, 0)).await??;
if site.community_creation_admin_only && is_admin(&local_user_view).is_err() {

View File

@ -21,7 +21,8 @@ impl PerformCrud for DeleteCommunity {
websocket_id: Option<ConnectionId>,
) -> Result<CommunityResponse, LemmyError> {
let data: &DeleteCommunity = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
// Fetch the community mods
let community_id = data.community_id;
@ -75,7 +76,8 @@ impl PerformCrud for RemoveCommunity {
websocket_id: Option<ConnectionId>,
) -> Result<CommunityResponse, LemmyError> {
let data: &RemoveCommunity = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
// Verify its an admin (only an admin can remove a community)
is_admin(&local_user_view)?;

View File

@ -27,7 +27,8 @@ impl PerformCrud for GetCommunity {
_websocket_id: Option<ConnectionId>,
) -> Result<GetCommunityResponse, LemmyError> {
let data: &GetCommunity = self;
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
let person_id = local_user_view.map(|u| u.person.id);
let community_id = match data.id {
@ -89,7 +90,8 @@ impl PerformCrud for ListCommunities {
_websocket_id: Option<ConnectionId>,
) -> Result<ListCommunitiesResponse, LemmyError> {
let data: &ListCommunities = self;
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
let person_id = local_user_view.to_owned().map(|l| l.person.id);

View File

@ -26,7 +26,8 @@ impl PerformCrud for EditCommunity {
websocket_id: Option<ConnectionId>,
) -> Result<CommunityResponse, LemmyError> {
let data: &EditCommunity = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
check_slurs_opt(&data.title)?;
check_slurs_opt(&data.description)?;

View File

@ -38,7 +38,8 @@ impl PerformCrud for CreatePost {
websocket_id: Option<ConnectionId>,
) -> Result<PostResponse, LemmyError> {
let data: &CreatePost = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
check_slurs(&data.name)?;
check_slurs_opt(&data.body)?;

View File

@ -23,7 +23,8 @@ impl PerformCrud for DeletePost {
websocket_id: Option<ConnectionId>,
) -> Result<PostResponse, LemmyError> {
let data: &DeletePost = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let post_id = data.post_id;
let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
@ -83,7 +84,8 @@ impl PerformCrud for RemovePost {
websocket_id: Option<ConnectionId>,
) -> Result<PostResponse, LemmyError> {
let data: &RemovePost = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let post_id = data.post_id;
let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;

View File

@ -24,7 +24,8 @@ impl PerformCrud for GetPost {
_websocket_id: Option<ConnectionId>,
) -> Result<GetPostResponse, LemmyError> {
let data: &GetPost = self;
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
let show_bot_accounts = local_user_view
.as_ref()
@ -112,7 +113,8 @@ impl PerformCrud for GetPosts {
_websocket_id: Option<ConnectionId>,
) -> Result<GetPostsResponse, LemmyError> {
let data: &GetPosts = self;
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
let person_id = local_user_view.to_owned().map(|l| l.person.id);

View File

@ -23,7 +23,8 @@ impl PerformCrud for EditPost {
websocket_id: Option<ConnectionId>,
) -> Result<PostResponse, LemmyError> {
let data: &EditPost = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
check_slurs_opt(&data.name)?;
check_slurs_opt(&data.body)?;

View File

@ -31,7 +31,8 @@ impl PerformCrud for CreatePrivateMessage {
websocket_id: Option<ConnectionId>,
) -> Result<PrivateMessageResponse, LemmyError> {
let data: &CreatePrivateMessage = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let content_slurs_removed = remove_slurs(&data.content.to_owned());

View File

@ -24,7 +24,8 @@ impl PerformCrud for DeletePrivateMessage {
websocket_id: Option<ConnectionId>,
) -> Result<PrivateMessageResponse, LemmyError> {
let data: &DeletePrivateMessage = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
// Checking permissions
let private_message_id = data.private_message_id;

View File

@ -20,7 +20,8 @@ impl PerformCrud for GetPrivateMessages {
_websocket_id: Option<ConnectionId>,
) -> Result<PrivateMessagesResponse, LemmyError> {
let data: &GetPrivateMessages = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let person_id = local_user_view.person.id;
let page = data.page;

View File

@ -24,7 +24,8 @@ impl PerformCrud for EditPrivateMessage {
websocket_id: Option<ConnectionId>,
) -> Result<PrivateMessageResponse, LemmyError> {
let data: &EditPrivateMessage = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
// Checking permissions
let private_message_id = data.private_message_id;

View File

@ -39,7 +39,8 @@ impl PerformCrud for CreateSite {
return Err(ApiError::err("site_already_exists").into());
};
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
check_slurs(&data.name)?;
check_slurs_opt(&data.description)?;

View File

@ -91,7 +91,8 @@ impl PerformCrud for GetSite {
// Build the local user
let my_user = if let Some(local_user_view) =
get_local_user_settings_view_from_jwt_opt(&data.auth, context.pool()).await?
get_local_user_settings_view_from_jwt_opt(&data.auth, context.pool(), context.secret())
.await?
{
let person_id = local_user_view.person.id;
let follows = blocking(context.pool(), move |conn| {

View File

@ -30,7 +30,8 @@ impl PerformCrud for EditSite {
websocket_id: Option<ConnectionId>,
) -> Result<SiteResponse, LemmyError> {
let data: &EditSite = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
check_slurs_opt(&data.name)?;
check_slurs_opt(&data.description)?;

View File

@ -9,7 +9,7 @@ use lemmy_apub::{
EndpointType,
};
use lemmy_db_queries::{
source::{local_user::LocalUser_, secret::SecretSingleton, site::Site_},
source::{local_user::LocalUser_, site::Site_},
Crud,
Followable,
Joinable,
@ -21,7 +21,6 @@ use lemmy_db_schema::{
community::*,
local_user::{LocalUser, LocalUserForm},
person::*,
secret::Secret,
site::*,
},
CommunityId,
@ -219,9 +218,8 @@ impl PerformCrud for Register {
}
// Return the jwt
let jwt_secret = Secret::get().jwt_secret;
Ok(LoginResponse {
jwt: Claims::jwt(inserted_local_user.id.0, &jwt_secret)?,
jwt: Claims::jwt(inserted_local_user.id.0, &context.secret().jwt_secret)?,
})
}
}

View File

@ -17,7 +17,8 @@ impl PerformCrud for DeleteAccount {
_websocket_id: Option<ConnectionId>,
) -> Result<LoginResponse, LemmyError> {
let data: &DeleteAccount = self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
// Verify the password
let valid: bool = verify(

View File

@ -22,7 +22,8 @@ impl PerformCrud for GetPersonDetails {
_websocket_id: Option<ConnectionId>,
) -> Result<GetPersonDetailsResponse, LemmyError> {
let data: &GetPersonDetails = self;
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
let local_user_view =
get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
let show_bot_accounts = local_user_view

View File

@ -1,36 +1,18 @@
use diesel::{result::Error, *};
use lemmy_db_schema::source::secret::Secret;
use lemmy_utils::settings::structs::Settings;
use std::sync::RwLock;
use crate::get_database_url_from_env;
lazy_static! {
static ref SECRET: RwLock<Secret> = RwLock::new(init().expect("Failed to load secrets from DB."));
pub trait Secret_ {
fn init(conn: &PgConnection) -> Result<Secret, Error>;
}
pub trait SecretSingleton {
fn get() -> Secret;
}
impl SecretSingleton for Secret {
/// Returns the Secret as a struct
fn get() -> Self {
SECRET.read().expect("read secrets").to_owned()
impl Secret_ for Secret {
/// Initialize the Secrets from the DB.
/// Warning: You should only call this once.
fn init(conn: &PgConnection) -> Result<Secret, Error> {
read_secrets(conn)
}
}
/// Reads the secrets from the DB
fn init() -> Result<Secret, Error> {
let db_url = match get_database_url_from_env() {
Ok(url) => url,
Err(_) => Settings::get().get_database_url(),
};
let conn = PgConnection::establish(&db_url).expect("Couldn't get DB connection for Secrets.");
read_secrets(&conn)
}
fn read_secrets(conn: &PgConnection) -> Result<Secret, Error> {
use lemmy_db_schema::schema::secret::dsl::*;
secret.first::<Secret>(conn)

View File

@ -4,13 +4,13 @@ use chrono::{DateTime, NaiveDateTime, Utc};
use diesel::PgConnection;
use lemmy_api_common::blocking;
use lemmy_db_queries::{
source::{community::Community_, person::Person_, secret::SecretSingleton},
source::{community::Community_, person::Person_},
Crud,
ListingType,
SortType,
};
use lemmy_db_schema::{
source::{community::Community, local_user::LocalUser, person::Person, secret::Secret},
source::{community::Community, local_user::LocalUser, person::Person},
LocalUserId,
};
use lemmy_db_views::{
@ -141,11 +141,13 @@ async fn get_feed(
_ => return Err(ErrorBadRequest(LemmyError::from(anyhow!("wrong_type")))),
};
let jwt_secret = context.secret().jwt_secret.to_owned();
let builder = blocking(context.pool(), move |conn| match request_type {
RequestType::User => get_feed_user(conn, &sort_type, param),
RequestType::Community => get_feed_community(conn, &sort_type, param),
RequestType::Front => get_feed_front(conn, &sort_type, param),
RequestType::Inbox => get_feed_inbox(conn, param),
RequestType::Front => get_feed_front(conn, &jwt_secret, &sort_type, param),
RequestType::Inbox => get_feed_inbox(conn, &jwt_secret, param),
})
.await?
.map_err(ErrorBadRequest)?;
@ -225,12 +227,12 @@ fn get_feed_community(
fn get_feed_front(
conn: &PgConnection,
jwt_secret: &str,
sort_type: &SortType,
jwt: String,
) -> Result<ChannelBuilder, LemmyError> {
let site_view = SiteView::read(conn)?;
let jwt_secret = Secret::get().jwt_secret;
let local_user_id = LocalUserId(Claims::decode(&jwt, &jwt_secret)?.claims.sub);
let local_user_id = LocalUserId(Claims::decode(&jwt, jwt_secret)?.claims.sub);
let local_user = LocalUser::read(conn, local_user_id)?;
let posts = PostQueryBuilder::create(conn)
@ -257,10 +259,13 @@ fn get_feed_front(
Ok(channel_builder)
}
fn get_feed_inbox(conn: &PgConnection, jwt: String) -> Result<ChannelBuilder, LemmyError> {
fn get_feed_inbox(
conn: &PgConnection,
jwt_secret: &str,
jwt: String,
) -> Result<ChannelBuilder, LemmyError> {
let site_view = SiteView::read(conn)?;
let jwt_secret = Secret::get().jwt_secret;
let local_user_id = LocalUserId(Claims::decode(&jwt, &jwt_secret)?.claims.sub);
let local_user_id = LocalUserId(Claims::decode(&jwt, jwt_secret)?.claims.sub);
let local_user = LocalUser::read(conn, local_user_id)?;
let person_id = local_user.person_id;
let show_bot_accounts = local_user.show_bot_accounts;

View File

@ -2,9 +2,8 @@ use actix_http::http::header::ACCEPT_ENCODING;
use actix_web::{body::BodyStream, http::StatusCode, web::Data, *};
use anyhow::anyhow;
use awc::Client;
use lemmy_db_queries::source::secret::SecretSingleton;
use lemmy_db_schema::source::secret::Secret;
use lemmy_utils::{claims::Claims, rate_limit::RateLimit, settings::structs::Settings, LemmyError};
use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize};
use std::time::Duration;
@ -48,14 +47,14 @@ async fn upload(
req: HttpRequest,
body: web::Payload,
client: web::Data<Client>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse, Error> {
// TODO: check rate limit here
let jwt = req
.cookie("jwt")
.expect("No auth header for picture upload");
let jwt_secret = Secret::get().jwt_secret;
if Claims::decode(jwt.value(), &jwt_secret).is_err() {
if Claims::decode(jwt.value(), &context.secret().jwt_secret).is_err() {
return Ok(HttpResponse::Unauthorized().finish());
};

View File

@ -14,7 +14,7 @@ use diesel::{
PgConnection,
};
use lemmy_api_common::{comment::*, post::*};
use lemmy_db_schema::{CommunityId, LocalUserId, PostId};
use lemmy_db_schema::{source::secret::Secret, CommunityId, LocalUserId, PostId};
use lemmy_utils::{
location_info,
rate_limit::RateLimit,
@ -71,6 +71,9 @@ pub struct ChatServer {
/// The DB Pool
pub(super) pool: Pool<ConnectionManager<PgConnection>>,
/// The Secrets
pub(super) secret: Secret,
/// Rate limiting based on rate type and IP addr
pub(super) rate_limiter: RateLimit,
@ -102,6 +105,7 @@ impl ChatServer {
message_handler_crud: MessageHandlerCrudType,
client: Client,
activity_queue: QueueHandle,
secret: Secret,
) -> ChatServer {
ChatServer {
sessions: HashMap::new(),
@ -117,6 +121,7 @@ impl ChatServer {
message_handler_crud,
client,
activity_queue,
secret,
}
}
@ -452,6 +457,7 @@ impl ChatServer {
chat_server: ctx.address(),
client: self.client.to_owned(),
activity_queue: self.activity_queue.to_owned(),
secret: self.secret.to_owned(),
};
let message_handler_crud = self.message_handler_crud;
let message_handler = self.message_handler;

View File

@ -5,6 +5,7 @@ use crate::chat_server::ChatServer;
use actix::Addr;
use background_jobs::QueueHandle;
use lemmy_db_queries::DbPool;
use lemmy_db_schema::source::secret::Secret;
use lemmy_utils::LemmyError;
use reqwest::Client;
use serde::Serialize;
@ -20,6 +21,7 @@ pub struct LemmyContext {
pub chat_server: Addr<ChatServer>,
pub client: Client,
pub activity_queue: QueueHandle,
pub secret: Secret,
}
impl LemmyContext {
@ -28,12 +30,14 @@ impl LemmyContext {
chat_server: Addr<ChatServer>,
client: Client,
activity_queue: QueueHandle,
secret: Secret,
) -> LemmyContext {
LemmyContext {
pool,
chat_server,
client,
activity_queue,
secret,
}
}
pub fn pool(&self) -> &DbPool {
@ -48,6 +52,9 @@ impl LemmyContext {
pub fn activity_queue(&self) -> &QueueHandle {
&self.activity_queue
}
pub fn secret(&self) -> &Secret {
&self.secret
}
}
impl Clone for LemmyContext {
@ -57,6 +64,7 @@ impl Clone for LemmyContext {
chat_server: self.chat_server.clone(),
client: self.client.clone(),
activity_queue: self.activity_queue.clone(),
secret: self.secret.clone(),
}
}
}

View File

@ -11,7 +11,8 @@ use lemmy_api::match_websocket_operation;
use lemmy_api_common::blocking;
use lemmy_api_crud::match_websocket_operation_crud;
use lemmy_apub::activity_queue::create_activity_queue;
use lemmy_db_queries::get_database_url_from_env;
use lemmy_db_queries::{get_database_url_from_env, source::secret::Secret_};
use lemmy_db_schema::source::secret::Secret;
use lemmy_routes::{feeds, images, nodeinfo, webfinger};
use lemmy_server::{api_routes, code_migrations::run_advanced_migrations, scheduled_tasks};
use lemmy_utils::{
@ -42,6 +43,12 @@ async fn main() -> Result<(), LemmyError> {
.build(manager)
.unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
// Initialize the secrets
let conn = pool.get()?;
let secret = Secret::init(&conn).expect("Couldn't initialize secrets");
// TODO init settings
// Run the migrations from code
blocking(&pool, move |conn| {
embedded_migrations::run(conn)?;
@ -74,6 +81,7 @@ async fn main() -> Result<(), LemmyError> {
|c, i, o, d| Box::pin(match_websocket_operation_crud(c, i, o, d)),
Client::default(),
activity_queue.clone(),
secret.clone(),
)
.start();
@ -84,6 +92,7 @@ async fn main() -> Result<(), LemmyError> {
chat_server.to_owned(),
Client::default(),
activity_queue.to_owned(),
secret.to_owned(),
);
let rate_limiter = rate_limiter.clone();
App::new()