mirror of
https://framagit.org/veretcle/tootube.git
synced 2025-07-20 20:41:17 +02:00
✨: write back tokens to the config file
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use crate::{config::PeertubeConfig, error::TootubeError};
|
||||
use crate::{config::PeertubeConfig, config::PeertubeConfigOauth2, error::TootubeError};
|
||||
use log::debug;
|
||||
use reqwest::{
|
||||
header::{HeaderMap, HeaderValue},
|
||||
@@ -8,7 +8,6 @@ use reqwest::{
|
||||
use rpassword::prompt_password;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{boxed::Box, cmp::Ordering, error::Error, io::stdin};
|
||||
use tokio::fs::{read_to_string, write};
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct PeerTubeVideos {
|
||||
@@ -149,7 +148,7 @@ struct PeerTubeVideoPlaylistsPlaylistIdVideosData {
|
||||
|
||||
/// This function makes the registration process a little bit easier
|
||||
#[tokio::main]
|
||||
pub async fn register(config: &PeertubeConfig) -> Result<(), Box<dyn Error>> {
|
||||
pub async fn register(config: &PeertubeConfig) -> Result<PeertubeConfigOauth2, Box<dyn Error>> {
|
||||
// Get client ID/secret
|
||||
let oauth2_client = reqwest::get(format!("{}/api/v1/oauth-clients/local", config.base_url))
|
||||
.await?
|
||||
@@ -185,26 +184,25 @@ pub async fn register(config: &PeertubeConfig) -> Result<(), Box<dyn Error>> {
|
||||
.json::<PeerTubeUsersTokenResponse>()
|
||||
.await?;
|
||||
|
||||
println!("You can now paste the following lines inside the `peertube` section of your tootube.toml file:");
|
||||
|
||||
println!();
|
||||
|
||||
println!(
|
||||
"The following lines will be written to the `peertube` section of your tootube.toml file:"
|
||||
);
|
||||
println!("[peertube.oauth2]");
|
||||
println!("client_id=\"{}\"", oauth2_client.client_id);
|
||||
println!("client_secret=\"{}\"", oauth2_client.client_secret);
|
||||
println!("refresh_token=<path to refresh_token>");
|
||||
println!("refresh_token=\"{}\"", oauth2_token.refresh_token);
|
||||
|
||||
println!();
|
||||
|
||||
println!("Finally, add the refresh token inside the refresh_token path:");
|
||||
println!("{}", oauth2_token.refresh_token);
|
||||
|
||||
Ok(())
|
||||
Ok(PeertubeConfigOauth2 {
|
||||
client_id: oauth2_client.client_id,
|
||||
client_secret: oauth2_client.client_secret,
|
||||
refresh_token: oauth2_token.refresh_token,
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PeerTube {
|
||||
base_url: String,
|
||||
pub refresh_token: Option<String>,
|
||||
client: Client,
|
||||
}
|
||||
|
||||
@@ -213,6 +211,7 @@ impl PeerTube {
|
||||
pub fn new(base_url: &str) -> Self {
|
||||
PeerTube {
|
||||
base_url: format!("{}/api/v1", base_url),
|
||||
refresh_token: None,
|
||||
client: Client::new(),
|
||||
}
|
||||
}
|
||||
@@ -221,17 +220,13 @@ impl PeerTube {
|
||||
/// the default required header
|
||||
pub async fn with_client(
|
||||
mut self,
|
||||
client_id: &str,
|
||||
client_secret: &str,
|
||||
refresh_token_path: &str,
|
||||
pt_oauth2: &PeertubeConfigOauth2,
|
||||
) -> Result<Self, Box<dyn Error>> {
|
||||
let refresh_token = read_to_string(refresh_token_path).await?;
|
||||
|
||||
let params = PeerTubeUsersToken {
|
||||
client_id: client_id.to_string(),
|
||||
client_secret: client_secret.to_string(),
|
||||
client_id: pt_oauth2.client_id.to_owned(),
|
||||
client_secret: pt_oauth2.client_secret.to_owned(),
|
||||
grant_type: "refresh_token".to_string(),
|
||||
refresh_token: Some(refresh_token),
|
||||
refresh_token: Some(pt_oauth2.refresh_token.to_owned()),
|
||||
username: None,
|
||||
password: None,
|
||||
};
|
||||
@@ -245,8 +240,6 @@ impl PeerTube {
|
||||
.json::<PeerTubeUsersTokenResponse>()
|
||||
.await?;
|
||||
|
||||
write(refresh_token_path, req.refresh_token).await?;
|
||||
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
"Authorization",
|
||||
@@ -257,6 +250,8 @@ impl PeerTube {
|
||||
.default_headers(headers)
|
||||
.build()?;
|
||||
|
||||
self.refresh_token = Some(req.refresh_token);
|
||||
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user