A decentralized music tracking and discovery platform built on AT Protocol 馃幍
rocksky.app
spotify
atproto
lastfm
musicbrainz
scrobbling
listenbrainz
1use std::env;
2
3use aes::{
4 cipher::{KeyIvInit, StreamCipher},
5 Aes256,
6};
7use anyhow::Error;
8use hex::decode;
9use jsonwebtoken::{EncodingKey, Header};
10use serde::{Deserialize, Serialize};
11
12type Aes256Ctr = ctr::Ctr64BE<Aes256>;
13
14#[derive(Debug, Serialize, Deserialize)]
15pub struct Claims {
16 exp: usize,
17 iat: usize,
18 did: String,
19}
20
21pub fn decrypt_aes_256_ctr(encrypted_text: &str, key: &[u8]) -> Result<String, Error> {
22 let iv = decode(env::var("SPOTIFY_ENCRYPTION_IV")?)?;
23 let ciphertext = decode(encrypted_text)?;
24
25 let mut cipher =
26 Aes256Ctr::new_from_slices(key, &iv).map_err(|_| Error::msg("Invalid key or IV"))?;
27 let mut decrypted_data = ciphertext.clone();
28 cipher.apply_keystream(&mut decrypted_data);
29
30 Ok(String::from_utf8(decrypted_data)?)
31}
32
33pub fn generate_token(did: &str) -> Result<String, Error> {
34 if env::var("JWT_SECRET").is_err() {
35 return Err(Error::msg("JWT_SECRET is not set"));
36 }
37
38 let claims = Claims {
39 exp: chrono::Utc::now().timestamp() as usize + 3600,
40 iat: chrono::Utc::now().timestamp() as usize,
41 did: did.to_string(),
42 };
43
44 jsonwebtoken::encode(
45 &Header::default(),
46 &claims,
47 &EncodingKey::from_secret(env::var("JWT_SECRET")?.as_ref()),
48 )
49 .map_err(Into::into)
50}