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"
|
dotenvy = "0.15"
|
||||||
bcrypt = "0.19"
|
bcrypt = "0.19"
|
||||||
uuid = {version = "1.23", features=["serde", "v4"]}
|
uuid = {version = "1.23", features=["serde", "v4"]}
|
||||||
jwt = "0.16.0"
|
jsonwebtoken = { version = "11", default-features = false, features = ["aws_lc_rs"] }
|
||||||
hmac = "0.12"
|
|
||||||
sha2 = "0.10"
|
|
||||||
log = "0.4.32"
|
log = "0.4.32"
|
||||||
env_logger = "0.11"
|
env_logger = "0.11"
|
||||||
scraper = "0.27"
|
scraper = "0.29"
|
||||||
actix-cors = "0.7"
|
actix-cors = "0.7"
|
||||||
chrono = { version = "0.4.45", features = ["serde"] }
|
chrono = { version = "0.4.45", features = ["serde"] }
|
||||||
dateparser = "0.3"
|
dateparser = "0.3"
|
||||||
|
|||||||
+3
-3
@@ -1,8 +1,8 @@
|
|||||||
# --- builder ---
|
# --- 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 \
|
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/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
@@ -12,7 +12,7 @@ RUN cargo build --release && \
|
|||||||
rm -rf target
|
rm -rf target
|
||||||
|
|
||||||
# --- runtime ---
|
# --- runtime ---
|
||||||
FROM debian:bookworm-slim
|
FROM debian:trixie-slim
|
||||||
|
|
||||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||||
libpq5 ca-certificates \
|
libpq5 ca-certificates \
|
||||||
|
|||||||
+49
-38
@@ -1,16 +1,12 @@
|
|||||||
extern crate hmac;
|
|
||||||
extern crate jwt;
|
|
||||||
extern crate sha2;
|
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
use actix_web::HttpRequest;
|
use actix_web::HttpRequest;
|
||||||
use chrono::{Duration, Utc};
|
use chrono::{Duration, Utc};
|
||||||
use dotenvy::dotenv;
|
use dotenvy::dotenv;
|
||||||
use hmac::{Hmac, Mac};
|
use jsonwebtoken::{
|
||||||
use jwt::{Header, SignWithKey, Token, VerifyWithKey};
|
decode, encode, errors::ErrorKind, Algorithm, DecodingKey, EncodingKey, Header, Validation,
|
||||||
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sha2::Sha256;
|
|
||||||
|
|
||||||
/// How long a freshly issued token remains valid for.
|
/// How long a freshly issued token remains valid for.
|
||||||
const TOKEN_LIFETIME_HOURS: i64 = 730;
|
const TOKEN_LIFETIME_HOURS: i64 = 730;
|
||||||
@@ -30,45 +26,42 @@ struct Claims {
|
|||||||
exp: i64,
|
exp: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
type HmacSha256 = Hmac<Sha256>;
|
fn jwt_secret() -> String {
|
||||||
|
|
||||||
fn signing_key() -> HmacSha256 {
|
|
||||||
dotenv().ok();
|
dotenv().ok();
|
||||||
let secret = env::var("JWT_SECRET").expect("JWT_SECRET must be set");
|
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")
|
|
||||||
|
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 {
|
impl JwtToken {
|
||||||
pub fn encode(user_id: i32, token_version: i32) -> String {
|
pub fn encode(user_id: i32, token_version: i32) -> String {
|
||||||
let key: HmacSha256 = signing_key();
|
|
||||||
let claims = Claims {
|
let claims = Claims {
|
||||||
user_id,
|
user_id,
|
||||||
tv: token_version,
|
tv: token_version,
|
||||||
exp: (Utc::now() + Duration::hours(TOKEN_LIFETIME_HOURS)).timestamp(),
|
exp: (Utc::now() + Duration::hours(TOKEN_LIFETIME_HOURS)).timestamp(),
|
||||||
};
|
};
|
||||||
// Signing claims with a valid HMAC key cannot fail.
|
encode(&Header::new(Algorithm::HS256), &claims, &encoding_key())
|
||||||
claims
|
|
||||||
.sign_with_key(&key)
|
|
||||||
.expect("signing claims with a valid HMAC key cannot fail")
|
.expect("signing claims with a valid HMAC key cannot fail")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn decode(encoded_token: String) -> Result<JwtToken, &'static str> {
|
pub fn decode(encoded_token: String) -> Result<JwtToken, &'static str> {
|
||||||
let key: HmacSha256 = signing_key();
|
let key = DecodingKey::from_secret(jwt_secret().as_bytes());
|
||||||
let token_str: &str = encoded_token.as_str();
|
match decode::<Claims>(&encoded_token, &key, &validation()) {
|
||||||
let token: Result<Token<Header, Claims, jwt::Verified>, jwt::Error> =
|
Ok(token) => Ok(JwtToken {
|
||||||
VerifyWithKey::verify_with_key(token_str, &key);
|
user_id: token.claims.user_id,
|
||||||
|
token_version: token.claims.tv,
|
||||||
match token {
|
}),
|
||||||
Ok(token) => {
|
Err(err) if matches!(err.kind(), ErrorKind::ExpiredSignature) => {
|
||||||
let claims = token.claims();
|
Err("token has expired")
|
||||||
if claims.exp < Utc::now().timestamp() {
|
|
||||||
return Err("token has expired");
|
|
||||||
}
|
|
||||||
Ok(JwtToken {
|
|
||||||
user_id: claims.user_id,
|
|
||||||
token_version: claims.tv,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
Err(_err) => Err("could not decode token"),
|
Err(_err) => Err("could not decode token"),
|
||||||
}
|
}
|
||||||
@@ -90,11 +83,9 @@ impl JwtToken {
|
|||||||
mod jwt_test {
|
mod jwt_test {
|
||||||
use actix_web::{http::header, test};
|
use actix_web::{http::header, test};
|
||||||
use chrono::{Duration, Utc};
|
use chrono::{Duration, Utc};
|
||||||
use hmac::Hmac;
|
use jsonwebtoken::{encode, Algorithm, EncodingKey, Header};
|
||||||
use jwt::SignWithKey;
|
|
||||||
use sha2::Sha256;
|
|
||||||
|
|
||||||
use super::{Claims, JwtToken};
|
use super::{encoding_key, Claims, JwtToken};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
async fn encode_decode() {
|
async fn encode_decode() {
|
||||||
@@ -116,13 +107,13 @@ mod jwt_test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
async fn decode_expired_token() {
|
async fn decode_expired_token() {
|
||||||
let key: Hmac<Sha256> = super::signing_key();
|
|
||||||
let claims = Claims {
|
let claims = Claims {
|
||||||
user_id: 32,
|
user_id: 32,
|
||||||
tv: 0,
|
tv: 0,
|
||||||
exp: (Utc::now() - Duration::hours(1)).timestamp(),
|
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) {
|
match JwtToken::decode(expired_token) {
|
||||||
Err(message) => assert_eq!(message, "token has expired"),
|
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]
|
#[actix_web::test]
|
||||||
async fn decode_from_request_with_correct_token() {
|
async fn decode_from_request_with_correct_token() {
|
||||||
let encoded_token: String = JwtToken::encode(32, 0);
|
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",
|
"build": "vite build",
|
||||||
"preview": "vite preview",
|
"preview": "vite preview",
|
||||||
"test": "vitest run",
|
"test": "vitest run",
|
||||||
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs --fix --ignore-path .gitignore",
|
"lint": "eslint . --fix",
|
||||||
"format": "prettier --write src/"
|
"format": "prettier --write src/"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@@ -18,16 +18,21 @@
|
|||||||
"vue-router": "^5.1.0"
|
"vue-router": "^5.1.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@rushstack/eslint-patch": "^1.16.1",
|
"@eslint/js": "^10.0.1",
|
||||||
"@vitejs/plugin-vue": "^6.0.7",
|
"@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",
|
"@vue/test-utils": "^2.4.11",
|
||||||
"eslint": "^8.57.0",
|
"eslint": "^10.14.0",
|
||||||
"eslint-plugin-vue": "^10.11.1",
|
"eslint-plugin-vue": "^10.11.1",
|
||||||
"jsdom": "^29.1.1",
|
"globals": "^17.14.0",
|
||||||
|
"jsdom": "^30.1.3",
|
||||||
"prettier": "^3.8.3",
|
"prettier": "^3.8.3",
|
||||||
"vite": "^8.0.16",
|
"vite": "^8.0.16",
|
||||||
"vitest": "^5.0.2",
|
"vitest": "^5.0.2",
|
||||||
"vue-eslint-parser": "^10.4.1"
|
"vue-eslint-parser": "^10.4.1"
|
||||||
|
},
|
||||||
|
"overrides": {
|
||||||
|
"glob": "^14.0.3",
|
||||||
|
"minimatch": "^11.0.4"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -21,7 +21,7 @@ async function loadFeeds() {
|
|||||||
try {
|
try {
|
||||||
const response = await axios.get(`/api/v1/article/feeds/${userId}`, authHeaders())
|
const response = await axios.get(`/api/v1/article/feeds/${userId}`, authHeaders())
|
||||||
feeds.value = response.data.feeds
|
feeds.value = response.data.feeds
|
||||||
} catch (e) {
|
} catch {
|
||||||
error.value = 'Failed to load feeds.'
|
error.value = 'Failed to load feeds.'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -31,7 +31,7 @@ async function deleteFeed(feedId) {
|
|||||||
try {
|
try {
|
||||||
await axios.delete(`/api/v1/article/feed/${feedId}`, authHeaders())
|
await axios.delete(`/api/v1/article/feed/${feedId}`, authHeaders())
|
||||||
feeds.value = feeds.value.filter(f => f.id !== feedId)
|
feeds.value = feeds.value.filter(f => f.id !== feedId)
|
||||||
} catch (e) {
|
} catch {
|
||||||
error.value = 'Failed to delete feed.'
|
error.value = 'Failed to delete feed.'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user