Skip to content

Commit

Permalink
Merge pull request #635 from craigcabrey/craigcabrey/feat/runtime-api…
Browse files Browse the repository at this point in the history
…-credentials

feat(model/client): make api credentials runtime configurable
  • Loading branch information
marhkb authored Jun 16, 2024
2 parents b16b6e0 + ef0c42e commit ab48a3e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
38 changes: 38 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod strings;
mod types;
mod utils;

use std::borrow::Cow;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::OnceLock;
Expand Down Expand Up @@ -77,6 +78,21 @@ fn main() -> glib::ExitCode {
application_opts.test_dc = true;
}

let client_id = dict.lookup("client-id").unwrap();
let client_secret = dict.lookup("client-secret").unwrap();

match (client_id, client_secret) {
(Some(client_id), Some(client_secret)) => {
application_opts.client_id = client_id;
application_opts.client_secret = Cow::Owned(client_secret);
}
(None, None) => (),
_ => {
log::error!("Both client-id and client-secret must be set together");
return 1;
}
};

APPLICATION_OPTS.set(application_opts).unwrap();

-1
Expand All @@ -103,12 +119,16 @@ fn main() -> glib::ExitCode {
pub(crate) struct ApplicationOptions {
pub(crate) data_dir: PathBuf,
pub(crate) test_dc: bool,
pub(crate) client_id: i32,
pub(crate) client_secret: Cow<'static, str>,
}
impl Default for ApplicationOptions {
fn default() -> Self {
Self {
data_dir: PathBuf::from(glib::user_data_dir().to_str().unwrap()).join("paper-plane"),
test_dc: Default::default(),
client_id: config::TG_API_ID,
client_secret: Cow::Borrowed(config::TG_API_HASH),
}
}
}
Expand All @@ -132,6 +152,24 @@ fn setup_cli<A: IsA<gio::Application>>(app: A) -> A {
Some("error|warn|info|debug|trace"),
);

app.add_main_option(
"client-id",
b'c'.into(),
glib::OptionFlags::NONE,
glib::OptionArg::Int,
&gettext("Override the builtin client id"),
None,
);

app.add_main_option(
"client-secret",
b's'.into(),
glib::OptionFlags::NONE,
glib::OptionArg::String,
&gettext("Override the builtin client secret"),
None,
);

app.add_main_option(
"test-dc",
b't'.into(),
Expand Down
7 changes: 5 additions & 2 deletions src/model/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::config;
use crate::model;
use crate::types::ClientId;
use crate::utils;
use crate::APPLICATION_OPTS;

/// A struct for storing information about a session's database.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -169,6 +170,8 @@ impl Client {
.expect("Data directory path is not a valid unicode string")
.into();

let application_opts = APPLICATION_OPTS.get().unwrap();

tdlib::functions::set_tdlib_parameters(
database_info.use_test_dc,
database_directory,
Expand All @@ -178,8 +181,8 @@ impl Client {
true,
true,
true,
config::TG_API_ID,
config::TG_API_HASH.into(),
application_opts.client_id,
application_opts.client_secret.to_string(),
system_language_code,
"Desktop".into(),
String::new(),
Expand Down

0 comments on commit ab48a3e

Please sign in to comment.