Compare commits
3
Commits
e20b41649f
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
26c3666104 | ||
|
|
e5068cc833 | ||
|
|
c7b1dd4658 |
No files matched your search
Generated
+270
-407
File diff suppressed because it is too large.
Load diff
+2
-4
@@ -26,12 +26,10 @@ diesel_migrations = "2.3"
|
||||
dotenvy = "0.15"
|
||||
bcrypt = "0.19"
|
||||
uuid = {version = "1.23", features=["serde", "v4"]}
|
||||
jwt = "0.16.0"
|
||||
hmac = "0.12"
|
||||
sha2 = "0.10"
|
||||
jsonwebtoken = { version = "11", default-features = false, features = ["aws_lc_rs"] }
|
||||
log = "0.4.32"
|
||||
env_logger = "0.11"
|
||||
scraper = "0.27"
|
||||
scraper = "0.29"
|
||||
actix-cors = "0.7"
|
||||
chrono = { version = "0.4.45", features = ["serde"] }
|
||||
dateparser = "0.3"
|
||||
|
||||
+3
-3
@@ -1,8 +1,8 @@
|
||||
# --- builder ---
|
||||
FROM rust:1-slim-bookworm AS builder
|
||||
FROM rust:1-slim-trixie AS builder
|
||||
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
libpq-dev pkg-config libssl-dev \
|
||||
libpq-dev pkg-config \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
WORKDIR /app
|
||||
@@ -12,7 +12,7 @@ RUN cargo build --release && \
|
||||
rm -rf target
|
||||
|
||||
# --- runtime ---
|
||||
FROM debian:bookworm-slim
|
||||
FROM debian:trixie-slim
|
||||
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
libpq5 ca-certificates \
|
||||
|
||||
+49
-38
@@ -1,16 +1,12 @@
|
||||
extern crate hmac;
|
||||
extern crate jwt;
|
||||
extern crate sha2;
|
||||
|
||||
use std::env;
|
||||
|
||||
use actix_web::HttpRequest;
|
||||
use chrono::{Duration, Utc};
|
||||
use dotenvy::dotenv;
|
||||
use hmac::{Hmac, Mac};
|
||||
use jwt::{Header, SignWithKey, Token, VerifyWithKey};
|
||||
use jsonwebtoken::{
|
||||
decode, encode, errors::ErrorKind, Algorithm, DecodingKey, EncodingKey, Header, Validation,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::Sha256;
|
||||
|
||||
/// How long a freshly issued token remains valid for.
|
||||
const TOKEN_LIFETIME_HOURS: i64 = 730;
|
||||
@@ -30,45 +26,42 @@ struct Claims {
|
||||
exp: i64,
|
||||
}
|
||||
|
||||
type HmacSha256 = Hmac<Sha256>;
|
||||
|
||||
fn signing_key() -> HmacSha256 {
|
||||
fn jwt_secret() -> String {
|
||||
dotenv().ok();
|
||||
let secret = env::var("JWT_SECRET").expect("JWT_SECRET must be set");
|
||||
// HMAC-SHA256 accepts a key of any length, so this cannot fail.
|
||||
HmacSha256::new_from_slice(secret.as_bytes()).expect("HMAC accepts a key of any length")
|
||||
env::var("JWT_SECRET").expect("JWT_SECRET must be set")
|
||||
}
|
||||
|
||||
fn encoding_key() -> EncodingKey {
|
||||
EncodingKey::from_secret(jwt_secret().as_bytes())
|
||||
}
|
||||
|
||||
fn validation() -> Validation {
|
||||
let mut validation = Validation::new(Algorithm::HS256);
|
||||
// Reject a token the moment `exp` passes, matching the previous hand-rolled check.
|
||||
validation.leeway = 0;
|
||||
validation
|
||||
}
|
||||
|
||||
impl JwtToken {
|
||||
pub fn encode(user_id: i32, token_version: i32) -> String {
|
||||
let key: HmacSha256 = signing_key();
|
||||
let claims = Claims {
|
||||
user_id,
|
||||
tv: token_version,
|
||||
exp: (Utc::now() + Duration::hours(TOKEN_LIFETIME_HOURS)).timestamp(),
|
||||
};
|
||||
// Signing claims with a valid HMAC key cannot fail.
|
||||
claims
|
||||
.sign_with_key(&key)
|
||||
encode(&Header::new(Algorithm::HS256), &claims, &encoding_key())
|
||||
.expect("signing claims with a valid HMAC key cannot fail")
|
||||
}
|
||||
|
||||
pub fn decode(encoded_token: String) -> Result<JwtToken, &'static str> {
|
||||
let key: HmacSha256 = signing_key();
|
||||
let token_str: &str = encoded_token.as_str();
|
||||
let token: Result<Token<Header, Claims, jwt::Verified>, jwt::Error> =
|
||||
VerifyWithKey::verify_with_key(token_str, &key);
|
||||
|
||||
match token {
|
||||
Ok(token) => {
|
||||
let claims = token.claims();
|
||||
if claims.exp < Utc::now().timestamp() {
|
||||
return Err("token has expired");
|
||||
}
|
||||
Ok(JwtToken {
|
||||
user_id: claims.user_id,
|
||||
token_version: claims.tv,
|
||||
})
|
||||
let key = DecodingKey::from_secret(jwt_secret().as_bytes());
|
||||
match decode::<Claims>(&encoded_token, &key, &validation()) {
|
||||
Ok(token) => Ok(JwtToken {
|
||||
user_id: token.claims.user_id,
|
||||
token_version: token.claims.tv,
|
||||
}),
|
||||
Err(err) if matches!(err.kind(), ErrorKind::ExpiredSignature) => {
|
||||
Err("token has expired")
|
||||
}
|
||||
Err(_err) => Err("could not decode token"),
|
||||
}
|
||||
@@ -90,11 +83,9 @@ impl JwtToken {
|
||||
mod jwt_test {
|
||||
use actix_web::{http::header, test};
|
||||
use chrono::{Duration, Utc};
|
||||
use hmac::Hmac;
|
||||
use jwt::SignWithKey;
|
||||
use sha2::Sha256;
|
||||
use jsonwebtoken::{encode, Algorithm, EncodingKey, Header};
|
||||
|
||||
use super::{Claims, JwtToken};
|
||||
use super::{encoding_key, Claims, JwtToken};
|
||||
|
||||
#[test]
|
||||
async fn encode_decode() {
|
||||
@@ -116,13 +107,13 @@ mod jwt_test {
|
||||
|
||||
#[test]
|
||||
async fn decode_expired_token() {
|
||||
let key: Hmac<Sha256> = super::signing_key();
|
||||
let claims = Claims {
|
||||
user_id: 32,
|
||||
tv: 0,
|
||||
exp: (Utc::now() - Duration::hours(1)).timestamp(),
|
||||
};
|
||||
let expired_token: String = claims.sign_with_key(&key).unwrap();
|
||||
let expired_token: String =
|
||||
encode(&Header::new(Algorithm::HS256), &claims, &encoding_key()).unwrap();
|
||||
|
||||
match JwtToken::decode(expired_token) {
|
||||
Err(message) => assert_eq!(message, "token has expired"),
|
||||
@@ -130,6 +121,26 @@ mod jwt_test {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
async fn decode_rejects_token_signed_with_another_secret() {
|
||||
let claims = Claims {
|
||||
user_id: 32,
|
||||
tv: 0,
|
||||
exp: (Utc::now() + Duration::hours(1)).timestamp(),
|
||||
};
|
||||
let forged_token: String = encode(
|
||||
&Header::new(Algorithm::HS256),
|
||||
&claims,
|
||||
&EncodingKey::from_secret(b"not-the-real-secret"),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
match JwtToken::decode(forged_token) {
|
||||
Err(message) => assert_eq!(message, "could not decode token"),
|
||||
_ => panic!("A token signed with a different secret must be rejected."),
|
||||
}
|
||||
}
|
||||
|
||||
#[actix_web::test]
|
||||
async fn decode_from_request_with_correct_token() {
|
||||
let encoded_token: String = JwtToken::encode(32, 0);
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
/* eslint-env node */
|
||||
require('@rushstack/eslint-patch/modern-module-resolution')
|
||||
|
||||
module.exports = {
|
||||
root: true,
|
||||
env: {
|
||||
browser: true,
|
||||
es2022: true
|
||||
},
|
||||
'extends': [
|
||||
'plugin:vue/essential',
|
||||
'eslint:recommended',
|
||||
'@vue/eslint-config-prettier/skip-formatting'
|
||||
],
|
||||
parserOptions: {
|
||||
ecmaVersion: 'latest'
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import js from '@eslint/js'
|
||||
import pluginVue from 'eslint-plugin-vue'
|
||||
import skipFormatting from '@vue/eslint-config-prettier/skip-formatting'
|
||||
import globals from 'globals'
|
||||
|
||||
export default [
|
||||
{
|
||||
ignores: ['dist/**', 'dist-ssr/**', 'coverage/**']
|
||||
},
|
||||
{
|
||||
files: ['**/*.{js,mjs,cjs,vue}'],
|
||||
languageOptions: {
|
||||
ecmaVersion: 'latest',
|
||||
globals: globals.browser
|
||||
}
|
||||
},
|
||||
js.configs.recommended,
|
||||
...pluginVue.configs['flat/essential'],
|
||||
skipFormatting
|
||||
]
|
||||
Generated
+709
-1265
File diff suppressed because it is too large.
Load diff
+10
-5
@@ -8,7 +8,7 @@
|
||||
"build": "vite build",
|
||||
"preview": "vite preview",
|
||||
"test": "vitest run",
|
||||
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs --fix --ignore-path .gitignore",
|
||||
"lint": "eslint . --fix",
|
||||
"format": "prettier --write src/"
|
||||
},
|
||||
"dependencies": {
|
||||
@@ -18,16 +18,21 @@
|
||||
"vue-router": "^5.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@rushstack/eslint-patch": "^1.16.1",
|
||||
"@eslint/js": "^10.0.1",
|
||||
"@vitejs/plugin-vue": "^6.0.7",
|
||||
"@vue/eslint-config-prettier": "^8.0.0",
|
||||
"@vue/eslint-config-prettier": "^10.2.0",
|
||||
"@vue/test-utils": "^2.4.11",
|
||||
"eslint": "^8.57.0",
|
||||
"eslint": "^10.14.0",
|
||||
"eslint-plugin-vue": "^10.11.1",
|
||||
"jsdom": "^29.1.1",
|
||||
"globals": "^17.14.0",
|
||||
"jsdom": "^30.1.3",
|
||||
"prettier": "^3.8.3",
|
||||
"vite": "^8.0.16",
|
||||
"vitest": "^5.0.2",
|
||||
"vue-eslint-parser": "^10.4.1"
|
||||
},
|
||||
"overrides": {
|
||||
"glob": "^14.0.3",
|
||||
"minimatch": "^11.0.4"
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ async function loadFeeds() {
|
||||
try {
|
||||
const response = await axios.get(`/api/v1/article/feeds/${userId}`, authHeaders())
|
||||
feeds.value = response.data.feeds
|
||||
} catch (e) {
|
||||
} catch {
|
||||
error.value = 'Failed to load feeds.'
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,7 @@ async function deleteFeed(feedId) {
|
||||
try {
|
||||
await axios.delete(`/api/v1/article/feed/${feedId}`, authHeaders())
|
||||
feeds.value = feeds.value.filter(f => f.id !== feedId)
|
||||
} catch (e) {
|
||||
} catch {
|
||||
error.value = 'Failed to delete feed.'
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user