refactoring

This commit is contained in:
병준 박 2022-08-27 06:03:12 +00:00
parent ee123f4e38
commit e67ee6426f
7 changed files with 120 additions and 3 deletions

View File

@ -10,9 +10,12 @@ name = "beteran_common_rust"
path = "./src/lib.rs" path = "./src/lib.rs"
[dependencies] [dependencies]
jsonwebtoken = { version = "8" }
lazy_static = { version = "1" }
prost = { version = "0" }
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = { version = "1" } serde_json = { version = "1" }
prost = { version = "0" } time = { version = "0" }
beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.74-snapshot" } beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.74-snapshot" }

27
resources/private.pem Normal file
View File

@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAw/GvSFLy8K5uT79REy5lJtIS+Y9uAP1SJEyLvAqWfdPh9YO5
QCnYPp1uCqL3xWozkhrQWUbaYkO+65vjmYcGBMlzO+7KvZcHh4wr8ybA03M6Yy6J
SbnXRQvVm2Bvp2Z1B0dkT11CtZxiaNywYFvMhh5+zEbsrbFkzN5zI+hNpyg4uaaQ
Y5Oy2jGW3zuOIy/i0qzs3pCHWDAk0d5hpGvflUIj7BOQtl7CcFoB8tMp6F2fi8t9
m/Qa0yvQz8CNf7pdDqprUlm509wKzB4HgX8aG5SMwMlfuQjvT3jQNLRnHJFDghdZ
uCaop8IKf4PVGjuLo0SEebYzVEYfzLQrHemoRwIDAQABAoIBAA7y+XSDH5XNOQ1M
g1r7RyxXCquPjcz1hrbOQDrwc9rVt3+LtS54As+EI0kcddrR521WLl8hULf+gypm
kk68RnFHDUudREqCo0hsIU5DvqJjQCkg+8hsLUhPRtS73sjewD7162lCrDw+2iM1
8x7m4gu5d+MN5jC6P/6BWjd7GbGO9Yvnz1avKLwwNgLoivZn1S88FAXE8wEgAG4j
evlHX6BaAaOkMy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-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw/GvSFLy8K5uT79REy5l
JtIS+Y9uAP1SJEyLvAqWfdPh9YO5QCnYPp1uCqL3xWozkhrQWUbaYkO+65vjmYcG
BMlzO+7KvZcHh4wr8ybA03M6Yy6JSbnXRQvVm2Bvp2Z1B0dkT11CtZxiaNywYFvM
hh5+zEbsrbFkzN5zI+hNpyg4uaaQY5Oy2jGW3zuOIy/i0qzs3pCHWDAk0d5hpGvf
lUIj7BOQtl7CcFoB8tMp6F2fi8t9m/Qa0yvQz8CNf7pdDqprUlm509wKzB4HgX8a
G5SMwMlfuQjvT3jQNLRnHJFDghdZuCaop8IKf4PVGjuLo0SEebYzVEYfzLQrHemo
RwIDAQAB
-----END PUBLIC KEY-----

75
src/jwt/mod.rs Normal file
View File

@ -0,0 +1,75 @@
use serde::{Deserialize, Serialize};
use std::fmt;
lazy_static! {
static ref ENCODING_KEY: jsonwebtoken::EncodingKey =
jsonwebtoken::EncodingKey::from_rsa_pem(include_bytes!("../../resources/private.pem"))
.expect("ENCODING_KEY");
static ref DECODING_KEY: jsonwebtoken::DecodingKey =
jsonwebtoken::DecodingKey::from_rsa_pem(include_bytes!("../../resources/public.pem"))
.expect("DECODING_KEY");
}
#[derive(Debug)]
pub enum Error {
Encoding(String),
Decoding(String),
}
impl std::error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Encoding(e) => write!(f, "Encoding Error: {}", e),
Error::Decoding(e) => write!(f, "Decoding Error: {}", e),
}
}
}
// #[derive(Debug, Serialize, Deserialize)]
// struct Claims {
// aud: String, // Optional. Audience
// exp: usize, // Required (validate_exp defaults to true in validation). Expiration time (as UTC timestamp)
// iat: usize, // Optional. Issued at (as UTC timestamp)
// iss: String, // Optional. Issuer
// nbf: usize, // Optional. Not Before (as UTC timestamp)
// sub: String, // Optional. Subject (whom token refers to)
// }
#[derive(Debug, Serialize, Deserialize)]
pub struct Claims {
pub iss: String,
pub iat: i64,
pub exp: i64,
pub session_id: String,
}
pub fn encode(issuer: &str, session_id: &str) -> Result<String, Error> {
use jsonwebtoken::{encode, Algorithm, Header};
let header = Header::new(Algorithm::RS256);
let issued_at = time::OffsetDateTime::now_utc();
let expiration_at = issued_at + time::Duration::days(1);
let claims = Claims {
iss: issuer.to_string(),
iat: issued_at.unix_timestamp(),
exp: expiration_at.unix_timestamp(),
session_id: session_id.to_string(),
};
let token =
encode(&header, &claims, &ENCODING_KEY).map_err(|e| Error::Encoding(e.to_string()))?;
Ok(token)
}
pub fn decode(token: &str) -> Result<Claims, Error> {
use jsonwebtoken::{decode, Algorithm, Validation};
let token_data = decode::<Claims>(token, &DECODING_KEY, &Validation::new(Algorithm::RS256))
.map_err(|e| Error::Decoding(e.to_string()))?;
Ok(token_data.claims)
}

View File

@ -1,2 +1,6 @@
#[macro_use]
extern crate lazy_static;
pub mod error; pub mod error;
pub mod models; pub mod jwt;
pub mod pagination;

View File

@ -1 +0,0 @@
pub mod pagination;