mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-06 09:47:50 +03:00
Compare commits
3 Commits
release/v1
...
release/v1
| Author | SHA1 | Date | |
|---|---|---|---|
|
e04d462223
|
|||
|
22af02464d
|
|||
|
9a517519db
|
1
.github/workflows/release.yml
vendored
1
.github/workflows/release.yml
vendored
@@ -47,6 +47,7 @@ jobs:
|
||||
JWT_SECRET: "test-secret-at-least-256-bits-used"
|
||||
VKID_CLIENT_ID: 0
|
||||
VKID_REDIRECT_URI: "vk0://vk.com/blank.html"
|
||||
REQWEST_USER_AGENT: "Dalvik/2.1.0 (Linux; U; Android 6.0.1; OPPO R9s Build/MMB29M)"
|
||||
build:
|
||||
name: Build
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
2
.github/workflows/test.yml
vendored
2
.github/workflows/test.yml
vendored
@@ -3,6 +3,7 @@ name: cargo test
|
||||
on:
|
||||
push:
|
||||
branches: [ "master" ]
|
||||
tags-ignore: [ "release/v*" ]
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
@@ -28,3 +29,4 @@ jobs:
|
||||
JWT_SECRET: "test-secret-at-least-256-bits-used"
|
||||
VKID_CLIENT_ID: 0
|
||||
VKID_REDIRECT_URI: "vk0://vk.com/blank.html"
|
||||
REQWEST_USER_AGENT: "Dalvik/2.1.0 (Linux; U; Android 6.0.1; OPPO R9s Build/MMB29M)"
|
||||
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -2876,7 +2876,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "schedule-parser-rusted"
|
||||
version = "0.8.0"
|
||||
version = "1.0.1"
|
||||
dependencies = [
|
||||
"actix-macros 0.1.0",
|
||||
"actix-test",
|
||||
|
||||
@@ -3,7 +3,7 @@ members = ["actix-macros", "actix-test"]
|
||||
|
||||
[package]
|
||||
name = "schedule-parser-rusted"
|
||||
version = "0.8.0"
|
||||
version = "1.0.1"
|
||||
edition = "2024"
|
||||
publish = false
|
||||
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
use crate::utility::jwt::DEFAULT_ALGORITHM;
|
||||
use jsonwebtoken::errors::ErrorKind;
|
||||
use jsonwebtoken::{decode, DecodingKey, Validation};
|
||||
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::env;
|
||||
use std::sync::LazyLock;
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
struct TokenData {
|
||||
@@ -17,7 +14,7 @@ struct TokenData {
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct Claims {
|
||||
sub: String,
|
||||
sub: i32,
|
||||
iis: String,
|
||||
jti: i32,
|
||||
app: i32,
|
||||
@@ -52,17 +49,10 @@ const VK_PUBLIC_KEY: &str = concat!(
|
||||
"-----END PUBLIC KEY-----"
|
||||
);
|
||||
|
||||
static VK_ID_CLIENT_ID: LazyLock<i32> = LazyLock::new(|| {
|
||||
env::var("VK_ID_CLIENT_ID")
|
||||
.expect("VK_ID_CLIENT_ID must be set")
|
||||
.parse::<i32>()
|
||||
.expect("VK_ID_CLIENT_ID must be i32")
|
||||
});
|
||||
|
||||
pub fn parse_vk_id(token_str: &String) -> Result<i32, Error> {
|
||||
pub fn parse_vk_id(token_str: &String, client_id: i32) -> Result<i32, Error> {
|
||||
let dkey = DecodingKey::from_rsa_pem(VK_PUBLIC_KEY.as_bytes()).unwrap();
|
||||
|
||||
match decode::<Claims>(&token_str, &dkey, &Validation::new(DEFAULT_ALGORITHM)) {
|
||||
match decode::<Claims>(&token_str, &dkey, &Validation::new(Algorithm::RS256)) {
|
||||
Ok(token_data) => {
|
||||
let claims = token_data.claims;
|
||||
|
||||
@@ -70,13 +60,10 @@ pub fn parse_vk_id(token_str: &String) -> Result<i32, Error> {
|
||||
Err(Error::UnknownIssuer(claims.iis))
|
||||
} else if claims.jti != 21 {
|
||||
Err(Error::UnknownType(claims.jti))
|
||||
} else if claims.app != *VK_ID_CLIENT_ID {
|
||||
} else if claims.app != client_id {
|
||||
Err(Error::UnknownClientId(claims.app))
|
||||
} else {
|
||||
match claims.sub.parse::<i32>() {
|
||||
Ok(sub) => Ok(sub),
|
||||
Err(_) => Err(Error::InvalidToken),
|
||||
}
|
||||
Ok(claims.sub)
|
||||
}
|
||||
}
|
||||
Err(err) => Err(match err.into_kind() {
|
||||
|
||||
@@ -71,7 +71,7 @@ pub async fn sign_in_vk(
|
||||
) -> ServiceResponse {
|
||||
let data = data_json.into_inner();
|
||||
|
||||
match parse_vk_id(&data.access_token) {
|
||||
match parse_vk_id(&data.access_token, app_state.vk_id.client_id) {
|
||||
Ok(id) => sign_in_combined(Vk(id), &app_state).await.into(),
|
||||
Err(_) => ErrorCode::InvalidVkAccessToken.into_response(),
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ pub async fn sign_up_vk(
|
||||
) -> ServiceResponse {
|
||||
let data = data_json.into_inner();
|
||||
|
||||
match parse_vk_id(&data.access_token) {
|
||||
match parse_vk_id(&data.access_token, app_state.vk_id.client_id) {
|
||||
Ok(id) => sign_up_combined(
|
||||
SignUpData {
|
||||
username: data.username,
|
||||
|
||||
@@ -4,7 +4,7 @@ use crate::app_state::Schedule;
|
||||
use crate::parser::parse_xls;
|
||||
use crate::routes::schedule::schema::CacheStatus;
|
||||
use crate::routes::schema::{IntoResponseAsError, ResponseError};
|
||||
use crate::xls_downloader::interface::XLSDownloader;
|
||||
use crate::xls_downloader::interface::{FetchError, XLSDownloader};
|
||||
use actix_web::web::Json;
|
||||
use actix_web::{patch, web};
|
||||
use chrono::Utc;
|
||||
@@ -60,16 +60,18 @@ pub async fn update_download_url(
|
||||
}
|
||||
},
|
||||
Err(error) => {
|
||||
eprintln!("Unknown url provided {}", data.url);
|
||||
eprintln!("{:?}", error);
|
||||
if let FetchError::Unknown(error) = error {
|
||||
sentry::capture_error(&error);
|
||||
}
|
||||
|
||||
ErrorCode::DownloadFailed.into_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(error) => {
|
||||
eprintln!("Unknown url provided {}", data.url);
|
||||
eprintln!("{:?}", error);
|
||||
if let FetchError::Unknown(error) = error {
|
||||
sentry::capture_error(&error);
|
||||
}
|
||||
|
||||
ErrorCode::FetchFailed.into_response()
|
||||
}
|
||||
|
||||
@@ -59,13 +59,16 @@ async fn oauth(data: web::Json<Request>, app_state: web::Data<AppState>) -> Serv
|
||||
return ErrorCode::VkIdError.into_response();
|
||||
}
|
||||
|
||||
if let Ok(auth_data) = res.json::<VkIdAuthResponse>().await {
|
||||
Ok(Response {
|
||||
access_token: auth_data.id_token,
|
||||
})
|
||||
.into()
|
||||
} else {
|
||||
ErrorCode::VkIdError.into_response()
|
||||
match res.json::<VkIdAuthResponse>().await {
|
||||
Ok(auth_data) =>
|
||||
Ok(Response {
|
||||
access_token: auth_data.id_token,
|
||||
}).into(),
|
||||
Err(error) => {
|
||||
sentry::capture_error(&error);
|
||||
|
||||
ErrorCode::VkIdError.into_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(_) => ErrorCode::VkIdError.into_response(),
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
use crate::xls_downloader::interface::{FetchError, FetchOk, FetchResult, XLSDownloader};
|
||||
use chrono::{DateTime, Utc};
|
||||
use std::env;
|
||||
|
||||
pub struct BasicXlsDownloader {
|
||||
pub url: Option<String>,
|
||||
user_agent: String,
|
||||
}
|
||||
|
||||
async fn fetch_specified(url: &String, user_agent: String, head: bool) -> FetchResult {
|
||||
async fn fetch_specified(url: &String, user_agent: &String, head: bool) -> FetchResult {
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let response = if head {
|
||||
@@ -13,7 +15,7 @@ async fn fetch_specified(url: &String, user_agent: String, head: bool) -> FetchR
|
||||
} else {
|
||||
client.get(url)
|
||||
}
|
||||
.header("User-Agent", user_agent)
|
||||
.header("User-Agent", user_agent.clone())
|
||||
.send()
|
||||
.await;
|
||||
|
||||
@@ -49,13 +51,16 @@ async fn fetch_specified(url: &String, user_agent: String, head: bool) -> FetchR
|
||||
})
|
||||
}
|
||||
}
|
||||
Err(_) => Err(FetchError::Unknown),
|
||||
Err(e) => Err(FetchError::Unknown(e)),
|
||||
}
|
||||
}
|
||||
|
||||
impl BasicXlsDownloader {
|
||||
pub fn new() -> Self {
|
||||
BasicXlsDownloader { url: None }
|
||||
BasicXlsDownloader {
|
||||
url: None,
|
||||
user_agent: env::var("REQWEST_USER_AGENT").expect("USER_AGENT must be set"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,17 +69,12 @@ impl XLSDownloader for BasicXlsDownloader {
|
||||
if self.url.is_none() {
|
||||
Err(FetchError::NoUrlProvided)
|
||||
} else {
|
||||
fetch_specified(
|
||||
self.url.as_ref().unwrap(),
|
||||
"t.me/polytechnic_next".to_string(),
|
||||
head,
|
||||
)
|
||||
.await
|
||||
fetch_specified(self.url.as_ref().unwrap(), &self.user_agent, head).await
|
||||
}
|
||||
}
|
||||
|
||||
async fn set_url(&mut self, url: String) -> FetchResult {
|
||||
let result = fetch_specified(&url, "t.me/polytechnic_next".to_string(), true).await;
|
||||
let result = fetch_specified(&url, &self.user_agent, true).await;
|
||||
|
||||
if let Ok(_) = result {
|
||||
self.url = Some(url);
|
||||
@@ -95,8 +95,8 @@ mod tests {
|
||||
let user_agent = String::new();
|
||||
|
||||
let results = [
|
||||
fetch_specified(&url, user_agent.clone(), true).await,
|
||||
fetch_specified(&url, user_agent.clone(), false).await,
|
||||
fetch_specified(&url, &user_agent, true).await,
|
||||
fetch_specified(&url, &user_agent, false).await,
|
||||
];
|
||||
|
||||
assert!(results[0].is_err());
|
||||
@@ -109,8 +109,8 @@ mod tests {
|
||||
let user_agent = String::new();
|
||||
|
||||
let results = [
|
||||
fetch_specified(&url, user_agent.clone(), true).await,
|
||||
fetch_specified(&url, user_agent.clone(), false).await,
|
||||
fetch_specified(&url, &user_agent, true).await,
|
||||
fetch_specified(&url, &user_agent, false).await,
|
||||
];
|
||||
|
||||
assert!(results[0].is_err());
|
||||
@@ -132,8 +132,8 @@ mod tests {
|
||||
let user_agent = String::new();
|
||||
|
||||
let results = [
|
||||
fetch_specified(&url, user_agent.clone(), true).await,
|
||||
fetch_specified(&url, user_agent.clone(), false).await,
|
||||
fetch_specified(&url, &user_agent, true).await,
|
||||
fetch_specified(&url, &user_agent, false).await,
|
||||
];
|
||||
|
||||
assert!(results[0].is_err());
|
||||
@@ -149,8 +149,8 @@ mod tests {
|
||||
let user_agent = String::new();
|
||||
|
||||
let results = [
|
||||
fetch_specified(&url, user_agent.clone(), true).await,
|
||||
fetch_specified(&url, user_agent.clone(), false).await,
|
||||
fetch_specified(&url, &user_agent, true).await,
|
||||
fetch_specified(&url, &user_agent, false).await,
|
||||
];
|
||||
|
||||
assert!(results[0].is_err());
|
||||
@@ -172,8 +172,8 @@ mod tests {
|
||||
let user_agent = String::new();
|
||||
|
||||
let results = [
|
||||
fetch_specified(&url, user_agent.clone(), true).await,
|
||||
fetch_specified(&url, user_agent.clone(), false).await,
|
||||
fetch_specified(&url, &user_agent, true).await,
|
||||
fetch_specified(&url, &user_agent, false).await,
|
||||
];
|
||||
|
||||
assert!(results[0].is_ok());
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use std::mem::discriminant;
|
||||
|
||||
/// XLS data retrieval errors.
|
||||
#[derive(PartialEq, Debug)]
|
||||
#[derive(Debug)]
|
||||
pub enum FetchError {
|
||||
/// File url is not set.
|
||||
NoUrlProvided,
|
||||
|
||||
/// Unknown error.
|
||||
Unknown,
|
||||
Unknown(reqwest::Error),
|
||||
|
||||
/// Server returned a status code different from 200.
|
||||
BadStatusCode,
|
||||
@@ -19,6 +20,12 @@ pub enum FetchError {
|
||||
BadHeaders,
|
||||
}
|
||||
|
||||
impl PartialEq for FetchError {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
discriminant(self) == discriminant(other)
|
||||
}
|
||||
}
|
||||
|
||||
/// Result of XLS data retrieval.
|
||||
pub struct FetchOk {
|
||||
/// ETag object.
|
||||
|
||||
Reference in New Issue
Block a user