Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use PostPasteResponse utility methods to improve json output #7

Merged
merged 5 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,21 @@ precedence. To avoid specifying the host / url everytime you can
take advantage of a config file as described [here](#Configuration-File).

When posting a paste you can specify `--json` to receive post details. The output
includes the base58 encoded key used to encrypt/decrypt the paste
and can be used to construct the paste url.
includes the base58 encoded key used to encrypt/decrypt the paste.
Constructed paste url (including key) and delete url (including token) are also provided for convenience.

Example output:
```
{"deletetoken":"ajae8c36aa945ff93a04bef4ff08fa505f96d49e1z28eb09a36l797c2eaeg952",
"id":"e6a227cfbc0fec3e",
"status":0,
"url":"/?e6a227cfbc0fec3e",
"bs58key":"31rvVHezWQH7sh7tgZGxfQJGKK4WLLCwFBL64Jr5nhLu"}
```json
{
"baseurl":"https://privatebin.net/",
"bs58key":"GN3qty1kAFbsGi9FbKKXigXwux1eofhiZQXNVFRMrNQd",
"deletetoken":"8536f6f8310ed4a9aae0e111b1763f5851cdbefe4c35e4b96bd690269635354a",
"deleteurl":"https://privatebin.net/?pasteid=31e2e7b19481fa7d&deletetoken=8536f6f8310ed4a9aae0e111b1763f5851cdbefe4c35e4b96bd690269635354a",
"id":"31e2e7b19481fa7d",
"pasteurl":"https://privatebin.net/?31e2e7b19481fa7d#GN3qty1kAFbsGi9FbKKXigXwux1eofhiZQXNVFRMrNQd",
"status":0,
"url":"/?31e2e7b19481fa7d"
}
```
---
#### Example usages to get a paste:
Expand Down
1 change: 1 addition & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ impl API {
.send()?;
let mut rsv: serde_json::Value = response.json()?;
rsv["bs58key"] = serde_json::Value::String(bs58::encode(paste_passphrase).into_string());
rsv["baseurl"] = serde_json::Value::String(self.base.to_string());
let status: u32 = rsv.get("status").unwrap().as_u64().unwrap() as u32;

match status {
Expand Down
8 changes: 6 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use pbcli::error::{PasteError, PbResult};
use pbcli::opts::Opts;
use pbcli::privatebin::DecryptedPaste;
use pbcli::util::check_filesize;
use serde_json::Value;
use std::io::{Read, Write};

fn get_stdin() -> std::io::Result<String> {
Expand Down Expand Up @@ -113,9 +114,12 @@ fn handle_post(opts: &Opts) -> PbResult<()> {
let res = api.post_paste(&paste, password, opts)?;

if opts.json {
std::io::stdout().write_all(res.to_json().as_bytes())?;
let mut output: Value = serde_json::to_value(res.clone())?;
output["pasteurl"] = Value::String(res.to_paste_url().to_string());
output["deleteurl"] = Value::String(res.to_delete_url().to_string());
std::io::stdout().write_all(output.to_string().as_bytes())?;
} else {
std::io::stdout().write_all(res.to_url(api.base()).as_str().as_bytes())?;
std::io::stdout().write_all(res.to_paste_url().as_str().as_bytes())?;
writeln!(std::io::stdout())?;
}

Expand Down
19 changes: 10 additions & 9 deletions src/privatebin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,33 +74,34 @@ pub struct DecryptedPaste {
pub attachment_name: Option<String>,
}

#[derive(Deserialize, Debug, Serialize)]
#[derive(Deserialize, Debug, Serialize, Clone)]
pub struct PostPasteResponse {
pub deletetoken: String,
pub id: String,
pub status: u32,
pub url: String,
pub baseurl: String,
nain-F49FF806 marked this conversation as resolved.
Show resolved Hide resolved
pub bs58key: String,
}

impl PostPasteResponse {
/// Return full paste url, i.e (base + ?id + #bs58key)
pub fn to_url(&self, base: &url::Url) -> url::Url {
let mut url = base.clone();
url.set_query(Some(&self.id));
url.set_fragment(Some(&self.bs58key));
url
pub fn to_paste_url(&self) -> url::Url {
let mut paste_url: url::Url = self.baseurl.parse().expect("Proper baseurl should be set");
paste_url.set_query(Some(&self.id));
paste_url.set_fragment(Some(&self.bs58key));
paste_url
}
/// Return url that can be used to delete paste
pub fn to_delete_url(&self, base: &url::Url) -> url::Url {
let mut delete_url = base.clone();
pub fn to_delete_url(&self) -> url::Url {
let mut delete_url: url::Url = self.baseurl.parse().expect("Proper baseurl should be set");
delete_url
.query_pairs_mut()
.append_pair("pasteid", &self.id)
.append_pair("deletetoken", &self.deletetoken);
delete_url
}
pub fn to_json(&self) -> String {
pub fn to_json_string(&self) -> String {
serde_json::to_string(&self).unwrap()
}
pub fn is_success(&self) -> bool {
Expand Down
Loading