7 Commits

12 changed files with 105 additions and 96 deletions

View File

@@ -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

View File

@@ -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
View File

@@ -2876,7 +2876,7 @@ dependencies = [
[[package]]
name = "schedule-parser-rusted"
version = "0.8.0"
version = "1.0.3"
dependencies = [
"actix-macros 0.1.0",
"actix-test",

View File

@@ -3,7 +3,7 @@ members = ["actix-macros", "actix-test"]
[package]
name = "schedule-parser-rusted"
version = "0.8.0"
version = "1.0.3"
edition = "2024"
publish = false

View File

@@ -6,7 +6,7 @@ ARG BINARY_NAME
WORKDIR /app/
RUN apt update && \
apt install -y libpq5
apt install -y libpq5 ca-certificates openssl
COPY ./${BINARY_NAME} /bin/main
RUN chmod +x /bin/main

View File

@@ -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() {

View File

@@ -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(),
}

View File

@@ -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,

View File

@@ -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,18 +60,20 @@ 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()
ErrorCode::DownloadFailed(error).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()
ErrorCode::FetchFailed(error).into_response()
}
}
}
@@ -83,6 +85,7 @@ mod schema {
use derive_more::Display;
use serde::{Deserialize, Serialize, Serializer};
use utoipa::ToSchema;
use crate::xls_downloader::interface::FetchError;
pub type ServiceResponse = crate::routes::schema::Response<CacheStatus, ErrorCode>;
@@ -101,12 +104,12 @@ mod schema {
NonWhitelistedHost,
/// Failed to retrieve file metadata.
#[display("Unable to retrieve metadata from the specified URL.")]
FetchFailed,
#[display("Unable to retrieve metadata from the specified URL: {_0}")]
FetchFailed(FetchError),
/// Failed to download the file.
#[display("Unable to retrieve data from the specified URL.")]
DownloadFailed,
#[display("Unable to retrieve data from the specified URL: {_0}")]
DownloadFailed(FetchError),
/// The link leads to an outdated schedule.
///
@@ -127,8 +130,8 @@ mod schema {
{
match self {
ErrorCode::NonWhitelistedHost => serializer.serialize_str("NON_WHITELISTED_HOST"),
ErrorCode::FetchFailed => serializer.serialize_str("FETCH_FAILED"),
ErrorCode::DownloadFailed => serializer.serialize_str("DOWNLOAD_FAILED"),
ErrorCode::FetchFailed(_) => serializer.serialize_str("FETCH_FAILED"),
ErrorCode::DownloadFailed(_) => serializer.serialize_str("DOWNLOAD_FAILED"),
ErrorCode::OutdatedSchedule => serializer.serialize_str("OUTDATED_SCHEDULE"),
ErrorCode::InvalidSchedule(_) => serializer.serialize_str("INVALID_SCHEDULE"),
}

View File

@@ -59,15 +59,18 @@ 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 {
match res.json::<VkIdAuthResponse>().await {
Ok(auth_data) =>
Ok(Response {
access_token: auth_data.id_token,
})
.into()
} else {
}).into(),
Err(error) => {
sentry::capture_error(&error);
ErrorCode::VkIdError.into_response()
}
}
}
Err(_) => ErrorCode::VkIdError.into_response(),
}
}

View File

@@ -1,11 +1,14 @@
use crate::xls_downloader::interface::{FetchError, FetchOk, FetchResult, XLSDownloader};
use chrono::{DateTime, Utc};
use std::env;
use std::sync::Arc;
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,14 +16,14 @@ 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;
match response {
Ok(r) => {
if r.status().as_u16() != 200 {
return Err(FetchError::BadStatusCode);
return Err(FetchError::BadStatusCode(r.status().as_u16()));
}
let headers = r.headers();
@@ -30,11 +33,18 @@ async fn fetch_specified(url: &String, user_agent: String, head: bool) -> FetchR
let last_modified = headers.get("last-modified");
let date = headers.get("date");
if content_type.is_none() || etag.is_none() || last_modified.is_none() || date.is_none()
{
Err(FetchError::BadHeaders)
if content_type.is_none() {
Err(FetchError::BadHeaders("Content-Type".to_string()))
} else if etag.is_none() {
Err(FetchError::BadHeaders("ETag".to_string()))
} else if last_modified.is_none() {
Err(FetchError::BadHeaders("Last-Modified".to_string()))
} else if date.is_none() {
Err(FetchError::BadHeaders("Date".to_string()))
} else if content_type.unwrap() != "application/vnd.ms-excel" {
Err(FetchError::BadContentType)
Err(FetchError::BadContentType(
content_type.unwrap().to_str().unwrap().to_string(),
))
} else {
let etag = etag.unwrap().to_str().unwrap().to_string();
let last_modified =
@@ -49,13 +59,16 @@ async fn fetch_specified(url: &String, user_agent: String, head: bool) -> FetchR
})
}
}
Err(_) => Err(FetchError::Unknown),
Err(error) => Err(FetchError::Unknown(Arc::new(error))),
}
}
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 +77,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);
@@ -86,7 +94,7 @@ impl XLSDownloader for BasicXlsDownloader {
#[cfg(test)]
mod tests {
use crate::xls_downloader::basic_impl::{BasicXlsDownloader, fetch_specified};
use crate::xls_downloader::basic_impl::{fetch_specified, BasicXlsDownloader};
use crate::xls_downloader::interface::{FetchError, XLSDownloader};
#[tokio::test]
@@ -95,8 +103,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,21 +117,17 @@ 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());
assert!(results[1].is_err());
assert_eq!(
*results[0].as_ref().err().unwrap(),
FetchError::BadStatusCode
);
assert_eq!(
*results[1].as_ref().err().unwrap(),
FetchError::BadStatusCode
);
let expected_error = FetchError::BadStatusCode(404);
assert_eq!(*results[0].as_ref().err().unwrap(), expected_error);
assert_eq!(*results[1].as_ref().err().unwrap(), expected_error);
}
#[tokio::test]
@@ -132,15 +136,17 @@ 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());
assert!(results[1].is_err());
assert_eq!(*results[0].as_ref().err().unwrap(), FetchError::BadHeaders);
assert_eq!(*results[1].as_ref().err().unwrap(), FetchError::BadHeaders);
let expected_error = FetchError::BadHeaders("ETag".to_string());
assert_eq!(*results[0].as_ref().err().unwrap(), expected_error);
assert_eq!(*results[1].as_ref().err().unwrap(), expected_error);
}
#[tokio::test]
@@ -149,21 +155,12 @@ 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());
assert!(results[1].is_err());
assert_eq!(
*results[0].as_ref().err().unwrap(),
FetchError::BadContentType
);
assert_eq!(
*results[1].as_ref().err().unwrap(),
FetchError::BadContentType
);
}
#[tokio::test]
@@ -172,8 +169,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());

View File

@@ -1,22 +1,38 @@
use chrono::{DateTime, Utc};
use derive_more::Display;
use std::mem::discriminant;
use std::sync::Arc;
use utoipa::ToSchema;
/// XLS data retrieval errors.
#[derive(PartialEq, Debug)]
#[derive(Clone, Debug, ToSchema, Display)]
pub enum FetchError {
/// File url is not set.
#[display("The link to the timetable was not provided earlier.")]
NoUrlProvided,
/// Unknown error.
Unknown,
#[display("An unknown error occurred while downloading the file.")]
#[schema(value_type = String)]
Unknown(Arc<reqwest::Error>),
/// Server returned a status code different from 200.
BadStatusCode,
#[display("Server returned a status code {_0}.")]
BadStatusCode(u16),
/// The url leads to a file of a different type.
BadContentType,
#[display("The link leads to a file of type '{_0}'.")]
BadContentType(String),
/// Server doesn't return expected headers.
BadHeaders,
#[display("Server doesn't return expected header(s) '{_0}'.")]
BadHeaders(String),
}
impl PartialEq for FetchError {
fn eq(&self, other: &Self) -> bool {
discriminant(self) == discriminant(other)
}
}
/// Result of XLS data retrieval.