From 7c82fb1512f49c0741dd83bf8f1936e8beabee7c Mon Sep 17 00:00:00 2001 From: syui Date: Sat, 17 Feb 2024 12:56:11 +0900 Subject: [PATCH] test refresh --- src/data.rs | 49 ++++++++++++++++++++++++++++++++++++++++++ src/describe.rs | 2 +- src/follow.rs | 5 +++-- src/followers.rs | 4 ++-- src/follows.rs | 4 ++-- src/img.rs | 3 ++- src/img_reply.rs | 3 ++- src/img_upload.rs | 3 ++- src/like.rs | 3 ++- src/main.rs | 15 +++++++------ src/mention.rs | 3 ++- src/notify.rs | 4 ++-- src/notify_read.rs | 4 ++-- src/post.rs | 3 ++- src/post_link.rs | 3 ++- src/profile.rs | 4 ++-- src/reply.rs | 3 ++- src/reply_link.rs | 3 ++- src/reply_og.rs | 3 ++- src/repost.rs | 3 ++- src/session.rs | 4 ++-- src/timeline_author.rs | 4 ++-- 22 files changed, 97 insertions(+), 35 deletions(-) diff --git a/src/data.rs b/src/data.rs index 06b3627..ba83651 100644 --- a/src/data.rs +++ b/src/data.rs @@ -18,6 +18,7 @@ pub fn data_file(s: &str) -> String { match &*s { "toml" => f + &"token.toml", "json" => f + &"token.json", + "refresh" => f + &"refresh.toml", _ => f + &"." + &s, } } @@ -59,6 +60,17 @@ impl Data { } } +impl Refresh { + pub fn new() -> Result { + let d = data_file("refresh"); + let s = Config::builder() + .add_source(File::with_name(&d)) + .add_source(config::Environment::with_prefix("APP")) + .build()?; + s.try_deserialize() + } +} + #[derive(Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct Token { @@ -78,6 +90,13 @@ pub struct Data { pub refresh: String, } +#[derive(Debug, Serialize, Deserialize)] +#[allow(non_snake_case)] +pub struct Refresh { + pub access: String, + pub refresh: String, +} + #[derive(Debug, Serialize, Deserialize)] pub struct BaseUrl { pub profile_get: String, @@ -190,6 +209,20 @@ pub fn data_toml(s: &str) -> String { } } +pub fn data_refresh(s: &str) -> String { + let s = String::from(s); + let refresh = Refresh::new().unwrap(); + let refresh = Refresh { + access: refresh.access, + refresh: refresh.refresh, + }; + match &*s { + "access" => refresh.access, + "refresh" => refresh.refresh, + _ => s, + } +} + pub fn data_scpt(s: &str) -> String { let s = String::from(s); let file = "/.config/ai/scpt/".to_owned() + &s + &".zsh"; @@ -475,6 +508,21 @@ pub fn w_cfg(h: &str, res: &str) { ff.write_all(&toml.as_bytes()).unwrap(); } +pub fn w_refresh(res: &str) { + let f = data_file(&"json"); + let ff = data_file(&"refresh"); + let mut f = fs::File::create(f.clone()).unwrap(); + let mut ff = fs::File::create(ff.clone()).unwrap(); + f.write_all(&res.as_bytes()).unwrap(); + let json: Token = serde_json::from_str(&res).unwrap(); + let refreshs = Refresh { + access: json.accessJwt.to_string(), + refresh: json.refreshJwt.to_string(), + }; + let toml = toml::to_string(&refreshs).unwrap(); + ff.write_all(&toml.as_bytes()).unwrap(); +} + pub fn w_cid(cid: String, file: String, t: bool) -> bool { let f = file; let mut file = match OpenOptions::new() @@ -558,3 +606,4 @@ pub struct OpenData { pub struct Choices { text: String, } + diff --git a/src/describe.rs b/src/describe.rs index 31c59e3..75e5ae5 100644 --- a/src/describe.rs +++ b/src/describe.rs @@ -3,7 +3,7 @@ extern crate reqwest; use crate::url; pub async fn get_request(user: String) -> String { - //let token = data_toml(&"access"); + //let token = data_refresh(&"access"); let url = url(&"describe"); let client = reqwest::Client::new(); diff --git a/src/follow.rs b/src/follow.rs index 764f0e3..d1317f6 100644 --- a/src/follow.rs +++ b/src/follow.rs @@ -1,5 +1,6 @@ extern crate reqwest; use crate::data_toml; +use crate::data_refresh; use crate::url; use iso8601_timestamp::Timestamp; use serde_json::json; @@ -7,7 +8,7 @@ use serde_json::json; //use crate::data::Follow; pub async fn post_request(u: String) -> String { - let token = data_toml(&"access"); + let token = data_refresh(&"access"); let did = data_toml(&"did"); let handle = data_toml(&"handle"); @@ -43,7 +44,7 @@ pub async fn post_request(u: String) -> String { } pub async fn delete_request(u: String, rkey: String) -> String { - let token = data_toml(&"access"); + let token = data_refresh(&"access"); let did = data_toml(&"did"); let handle = data_toml(&"handle"); diff --git a/src/followers.rs b/src/followers.rs index 016246e..0927289 100644 --- a/src/followers.rs +++ b/src/followers.rs @@ -1,10 +1,10 @@ extern crate reqwest; -use crate::data_toml; +use crate::data_refresh; use crate::url; //use serde_json::json; pub async fn get_request(actor: String, cursor: Option) -> String { - let token = data_toml(&"access"); + let token = data_refresh(&"access"); let url = url(&"followers"); let cursor = cursor.unwrap(); diff --git a/src/follows.rs b/src/follows.rs index 4ae5d09..bb517e0 100644 --- a/src/follows.rs +++ b/src/follows.rs @@ -1,10 +1,10 @@ extern crate reqwest; -use crate::data_toml; +use crate::data_refresh; use crate::url; //use serde_json::json; pub async fn get_request(actor: String, cursor: Option) -> String { - let token = data_toml(&"access"); + let token = data_refresh(&"access"); let url = url(&"follows"); let cursor = cursor.unwrap(); //let cursor = "1682386039125::bafyreihwgwozmvqxcxrhbr65agcaa4v357p27ccrhzkjf3mz5xiozjvzfa".to_string(); diff --git a/src/img.rs b/src/img.rs index b27bace..013917b 100644 --- a/src/img.rs +++ b/src/img.rs @@ -1,12 +1,13 @@ extern crate reqwest; use crate::data_toml; +use crate::data_refresh; use crate::url; use serde_json::json; use iso8601_timestamp::Timestamp; pub async fn post_request(text: String, link: String) -> String { - let token = data_toml(&"access"); + let token = data_refresh(&"access"); let did = data_toml(&"did"); let handle = data_toml(&"handle"); diff --git a/src/img_reply.rs b/src/img_reply.rs index 5aec56b..f7b3a9a 100644 --- a/src/img_reply.rs +++ b/src/img_reply.rs @@ -1,5 +1,6 @@ extern crate reqwest; use crate::data_toml; +use crate::data_refresh; use crate::url; use iso8601_timestamp::Timestamp; use serde_json::json; @@ -11,7 +12,7 @@ pub async fn post_request( uri: String, itype: String, ) -> String { - let token = data_toml(&"access"); + let token = data_refresh(&"access"); let did = data_toml(&"did"); let handle = data_toml(&"handle"); diff --git a/src/img_upload.rs b/src/img_upload.rs index b27bace..013917b 100644 --- a/src/img_upload.rs +++ b/src/img_upload.rs @@ -1,12 +1,13 @@ extern crate reqwest; use crate::data_toml; +use crate::data_refresh; use crate::url; use serde_json::json; use iso8601_timestamp::Timestamp; pub async fn post_request(text: String, link: String) -> String { - let token = data_toml(&"access"); + let token = data_refresh(&"access"); let did = data_toml(&"did"); let handle = data_toml(&"handle"); diff --git a/src/like.rs b/src/like.rs index 39fe44d..ffc4821 100644 --- a/src/like.rs +++ b/src/like.rs @@ -1,11 +1,12 @@ extern crate reqwest; use crate::data_toml; +use crate::data_refresh; use crate::url; use iso8601_timestamp::Timestamp; use serde_json::json; pub async fn post_request(cid: String, uri: String) -> String { - let token = data_toml(&"access"); + let token = data_refresh(&"access"); let did = data_toml(&"did"); let handle = data_toml(&"handle"); diff --git a/src/main.rs b/src/main.rs index a1cd966..bea1c57 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,8 +6,10 @@ use crate::bot::c_bot; use crate::data::c_follow_all; use crate::data::c_openai_key; use crate::data::data_toml; +use crate::data::data_refresh; use crate::data::url; use crate::data::w_cfg; +use crate::data::w_refresh; use data::ProfileIdentityResolve; @@ -280,12 +282,14 @@ fn token(c: &Context) { if let Ok(p) = c.string_flag("password") { if let Ok(s) = c.string_flag("server") { let res = token::post_request(m.to_string(), p.to_string(), s.to_string()).await; - w_cfg(&s, &res) + w_cfg(&s, &res); + w_refresh(&res); } else { let res = token::post_request(m.to_string(), p.to_string(), "bsky.social".to_string()) .await; - w_cfg(&"bsky.social", &res) + w_cfg(&"bsky.social", &res); + w_refresh(&res); } } }; @@ -294,15 +298,12 @@ fn token(c: &Context) { } fn refresh(_c: &Context) { - let server = data_toml(&"host"); let h = async { let session = session::get_request().await; if session == "err" { let res = refresh::post_request().await; println!("{}", res); - w_cfg(&server, &res) - } else { - println!("no refresh"); + w_refresh(&res) } }; let res = tokio::runtime::Runtime::new().unwrap().block_on(h); @@ -497,7 +498,7 @@ fn reply(c: &Context) { #[tokio::main] async fn c_img_upload(c: &Context) -> reqwest::Result<()> { - let token = data_toml(&"access"); + let token = data_refresh(&"access"); let atoken = "Authorization: Bearer ".to_owned() + &token; let con = "Content-Type: image/png"; let url = url(&"upload_blob"); diff --git a/src/mention.rs b/src/mention.rs index cd88422..84cf978 100644 --- a/src/mention.rs +++ b/src/mention.rs @@ -1,11 +1,12 @@ extern crate reqwest; use crate::data_toml; +use crate::data_refresh; use crate::url; use iso8601_timestamp::Timestamp; use serde_json::json; pub async fn post_request(text: String, at: String, udid: String, s: i32, e: i32) -> String { - let token = data_toml(&"access"); + let token = data_refresh(&"access"); let did = data_toml(&"did"); let handle = data_toml(&"handle"); diff --git a/src/notify.rs b/src/notify.rs index b0f13ab..cd74660 100644 --- a/src/notify.rs +++ b/src/notify.rs @@ -1,10 +1,10 @@ extern crate reqwest; -use crate::data_toml; +use crate::data_refresh; use crate::url; //use serde_json::json; pub async fn get_request(limit: i32) -> String { - let token = data_toml(&"access"); + let token = data_refresh(&"access"); let url = url(&"notify_list"); let client = reqwest::Client::new(); diff --git a/src/notify_read.rs b/src/notify_read.rs index 9ec0383..e8941ec 100644 --- a/src/notify_read.rs +++ b/src/notify_read.rs @@ -1,10 +1,10 @@ extern crate reqwest; -use crate::data_toml; +use crate::data_refresh; use crate::url; use serde_json::json; pub async fn post_request(time: String) -> String { - let token = data_toml(&"access"); + let token = data_refresh(&"access"); let url = url(&"notify_update"); let post = Some(json!({ diff --git a/src/post.rs b/src/post.rs index c5274ba..c90992b 100644 --- a/src/post.rs +++ b/src/post.rs @@ -1,11 +1,12 @@ extern crate reqwest; use crate::data_toml; +use crate::data_refresh; use crate::url; use iso8601_timestamp::Timestamp; use serde_json::json; pub async fn post_request(text: String) -> String { - let token = data_toml(&"access"); + let token = data_refresh(&"access"); let did = data_toml(&"did"); let handle = data_toml(&"handle"); diff --git a/src/post_link.rs b/src/post_link.rs index 29a6f44..ba50aea 100644 --- a/src/post_link.rs +++ b/src/post_link.rs @@ -1,11 +1,12 @@ extern crate reqwest; use crate::data_toml; +use crate::data_refresh; use crate::url; use iso8601_timestamp::Timestamp; use serde_json::json; pub async fn post_request(text: String, link: String, s: i32, e: i32) -> String { - let token = data_toml(&"access"); + let token = data_refresh(&"access"); let did = data_toml(&"did"); let handle = data_toml(&"handle"); diff --git a/src/profile.rs b/src/profile.rs index 6d56703..6c49ebf 100644 --- a/src/profile.rs +++ b/src/profile.rs @@ -1,9 +1,9 @@ extern crate reqwest; -use crate::data_toml; +use crate::data_refresh; use crate::url; pub async fn get_request(user: String) -> String { - let token = data_toml(&"access"); + let token = data_refresh(&"access"); let url = url(&"profile_get") + &"?handle=" + &user; let client = reqwest::Client::new(); diff --git a/src/reply.rs b/src/reply.rs index 586db7b..812229f 100644 --- a/src/reply.rs +++ b/src/reply.rs @@ -1,5 +1,6 @@ extern crate reqwest; use crate::data_toml; +use crate::data_refresh; use crate::url; use iso8601_timestamp::Timestamp; use serde_json::json; @@ -11,7 +12,7 @@ pub async fn post_request( cid_root: String, uri_root: String, ) -> String { - let token = data_toml(&"access"); + let token = data_refresh(&"access"); let did = data_toml(&"did"); let handle = data_toml(&"handle"); diff --git a/src/reply_link.rs b/src/reply_link.rs index 97cfd07..d34b9eb 100644 --- a/src/reply_link.rs +++ b/src/reply_link.rs @@ -1,5 +1,6 @@ extern crate reqwest; use crate::data_toml; +use crate::data_refresh; use crate::url; use iso8601_timestamp::Timestamp; use serde_json::json; @@ -14,7 +15,7 @@ pub async fn post_request( cid_root: String, uri_root: String, ) -> String { - let token = data_toml(&"access"); + let token = data_refresh(&"access"); let did = data_toml(&"did"); let handle = data_toml(&"handle"); diff --git a/src/reply_og.rs b/src/reply_og.rs index 43940b9..af27c9d 100644 --- a/src/reply_og.rs +++ b/src/reply_og.rs @@ -1,5 +1,6 @@ extern crate reqwest; use crate::data_toml; +use crate::data_refresh; use crate::url; use iso8601_timestamp::Timestamp; use serde_json::json; @@ -13,7 +14,7 @@ pub async fn post_request( title: String, description: String, ) -> String { - let token = data_toml(&"access"); + let token = data_refresh(&"access"); let did = data_toml(&"did"); let handle = data_toml(&"handle"); diff --git a/src/repost.rs b/src/repost.rs index 73f46b8..44f202a 100644 --- a/src/repost.rs +++ b/src/repost.rs @@ -1,11 +1,12 @@ extern crate reqwest; use crate::data_toml; +use crate::data_refresh; use crate::url; use iso8601_timestamp::Timestamp; use serde_json::json; pub async fn post_request(cid: String, uri: String) -> String { - let token = data_toml(&"access"); + let token = data_refresh(&"access"); let did = data_toml(&"did"); let handle = data_toml(&"handle"); diff --git a/src/session.rs b/src/session.rs index 204c322..ad49cb1 100644 --- a/src/session.rs +++ b/src/session.rs @@ -1,9 +1,9 @@ extern crate reqwest; -use crate::data_toml; +use crate::data_refresh; use crate::url; pub async fn get_request() -> String { - let token = data_toml(&"access"); + let token = data_refresh(&"access"); let url = url(&"session_get"); let client = reqwest::Client::new(); diff --git a/src/timeline_author.rs b/src/timeline_author.rs index 33ed60e..bbb447e 100644 --- a/src/timeline_author.rs +++ b/src/timeline_author.rs @@ -1,9 +1,9 @@ extern crate reqwest; -use crate::data_toml; +use crate::data_refresh; use crate::url; pub async fn get_request(actor: String) -> String { - let token = data_toml(&"access"); + let token = data_refresh(&"access"); let url = url(&"record_list"); let actor = actor.to_string();