Skip to content

Commit

Permalink
refactor: suit module auth to new api generator
Browse files Browse the repository at this point in the history
  • Loading branch information
ActivePeter committed Jun 14, 2024
1 parent 88b1228 commit 89541fb
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 262 deletions.
132 changes: 90 additions & 42 deletions pa_note_front/src/logic/authority.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,100 @@
import {AppFuncTs} from "@/logic/app_func";
import {LoginArg, VerifyTokenArg} from "@/logic/commu/api_caller";
import {_PaUtilTs} from "@/3rd/pa_util_ts";
import {ElNotification} from "element-plus";

export class AuthorityMan{
private verified=false;
verify_token_when_first_load(){
let token=window.localStorage["panote_token"]
if(token&&_PaUtilTs._JudgeType.is_string(token)){
this.ctx.api_caller
.verify_token(new VerifyTokenArg(token),(r)=>{
if(r.if_success){
this.verified=true
this.ctx.app.showlogbtn=false
window.localStorage["panote_token"]=r.new_token
ElNotification({
title: 'Success',
message: 'token 已更新',
type: 'success',
})
}else{
delete window.localStorage["panote_token"]
}
})
}
}
is_logged_in(){
return this.verified
}
login(id:string,pw:string,cb:(succ:boolean)=>void){
this.ctx.api_caller
.login(new LoginArg(id,pw),(r)=>{
if(r.if_success==1){
this.verified=true
this.ctx.app.showlogbtn=false
window.localStorage["panote_token"]=r.token
import { AppFuncTs } from "@/logic/app_func";
import { LoginArg, VerifyTokenArg } from "@/logic/commu/api_caller";
import { _PaUtilTs } from "@/3rd/pa_util_ts";
import { ElNotification } from "element-plus";
import { LoginReq, VerifyTokenReq, apis } from "./gen_api_auth";

export class AuthorityMan {
private verified = false;
verify_token_when_first_load() {
let token = window.localStorage["panote_token"]
if (token && _PaUtilTs._JudgeType.is_string(token)) {
apis.verify_token(new VerifyTokenReq(token)).then(r => {
let succ = r.succ()
let fail = r.fail()
if (succ) {
this.verified = true
this.ctx.app.showlogbtn = false
window.localStorage["panote_token"] = succ.new_token
ElNotification({
title: 'Success',
message: '登录成功',
message: 'token 已更新',
type: 'success',
})
cb(true)
}else{
cb(false)
} else {
console.warn("token 已过期", fail)
ElNotification({
title: 'Fail',
message: 'token 已过期',
type: 'fail',
})
delete window.localStorage["panote_token"]
}
})
// this.ctx.api_caller
// .verify_token(new VerifyTokenArg(token), (r) => {
// if (r.if_success) {
// this.verified = true
// this.ctx.app.showlogbtn = false
// window.localStorage["panote_token"] = r.new_token
// ElNotification({
// title: 'Success',
// message: 'token 已更新',
// type: 'success',
// })
// } else {
// delete window.localStorage["panote_token"]
// }
// })
}
}
is_logged_in() {
return this.verified
}
login(id: string, pw: string, cb: (succ: boolean) => void) {
apis.login(new LoginReq(id, pw)).then(r => {
let succ = r.succ()
let fail = r.fail()
if (succ) {
this.verified = true
this.ctx.app.showlogbtn = false
window.localStorage["panote_token"] = succ.token
ElNotification({
title: 'Success',
message: '登录成功',
type: 'success',
})
cb(true)
} else {
ElNotification({
title: 'Fail',
message: '登录失败',
type: 'error',
})
cb(false)
}
}).catch(e => {
console.log(e)
cb(false)
})
// this.ctx.api_caller
// .login(new LoginArg(id,pw),(r)=>{
// if(r.if_success==1){
// this.verified=true
// this.ctx.app.showlogbtn=false
// window.localStorage["panote_token"]=r.token

// ElNotification({
// title: 'Success',
// message: '登录成功',
// type: 'success',
// })
// cb(true)
// }else{
// cb(false)
// }
// })
}
constructor(private ctx:AppFuncTs.Context) {
constructor(private ctx: AppFuncTs.Context) {
}
}
4 changes: 2 additions & 2 deletions pa_note_front/src/logic/gen_api_auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class LoginReq {

export namespace apis {
export async function login(req:LoginReq):Promise<LoginResp>{
let res:any = await axios.post("/api/login", req)
let res:any = await axios.post("/api/panote_auth/login", req)
return new LoginResp(res.data.kernel,res.data.id)
}
}
Expand Down Expand Up @@ -97,7 +97,7 @@ export class VerifyTokenReq {

export namespace apis {
export async function verify_token(req:VerifyTokenReq):Promise<VerifyTokenResp>{
let res:any = await axios.post("/api/verify_token", req)
let res:any = await axios.post("/api/panote_auth/verify_token", req)
return new VerifyTokenResp(res.data.kernel,res.data.id)
}
}
Expand Down
5 changes: 0 additions & 5 deletions pa_note_server.wasm/files

This file was deleted.

2 changes: 1 addition & 1 deletion pa_note_server.wasm/panote_auth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ wasmedge-wasi-helper = "=0.2.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
chrono = "0.4.38"
jsonwebtoken = "9.1.0"
jsonwebtoken = "9.1.0"
30 changes: 6 additions & 24 deletions pa_note_server.wasm/panote_auth/src/gen_api.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

use serde_json::{json,Value};
use serde::{Serialize, Deserialize};
use axum::{http::StatusCode, routing::post, Json, Router};
use async_trait::async_trait;
use crate::general::network::http_handler::ApiHandlerImpl;




#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -78,32 +77,15 @@ pub struct VerifyTokenReq {
}


#[async_trait]

pub trait ApiHandler {

async fn handle_login(&self, req:LoginReq)->LoginResp;
fn handle_login(&self, req:LoginReq)->LoginResp;

async fn handle_verify_token(&self, req:VerifyTokenReq)->VerifyTokenResp;
fn handle_verify_token(&self, req:VerifyTokenReq)->VerifyTokenResp;

}


pub fn add_routers(mut router:Router)->Router
{

async fn login(Json(req):Json<LoginReq>)-> (StatusCode, Json<Value>){
(StatusCode::OK, Json(ApiHandlerImpl.handle_login(req).await.serialize()))
}
router=router
.route("/login", post(login));

async fn verify_token(Json(req):Json<VerifyTokenReq>)-> (StatusCode, Json<Value>){
(StatusCode::OK, Json(ApiHandlerImpl.handle_verify_token(req).await.serialize()))
}
router=router
.route("/verify_token", post(verify_token));


router
}


37 changes: 0 additions & 37 deletions pa_note_server.wasm/panote_auth/src/impl.rs

This file was deleted.

28 changes: 28 additions & 0 deletions pa_note_server.wasm/panote_auth/src/impl/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use self::authority::AuthorityMan;
use crate::gen_api::{ApiHandler, LoginReq, LoginResp, VerifyTokenReq, VerifyTokenResp};
use crate::*;

mod authority;

impl ApiHandler for Impl {
fn handle_login(&self, req: LoginReq) -> LoginResp {
let auth_man = AuthorityMan::new();
if let Ok(_) = auth_man.check_id_and_pw(&*(req.id), &*(req.pw)) {
LoginResp::Succ {
token: auth_man.gen_token(),
}
} else {
LoginResp::Fail { msg: "".to_owned() }
}
}
fn handle_verify_token(&self, req: VerifyTokenReq) -> VerifyTokenResp {
let auth_man = AuthorityMan::new();
if auth_man.check_token(req.token).is_ok() {
VerifyTokenResp::Succ {
new_token: auth_man.gen_token(),
}
} else {
VerifyTokenResp::Fail { msg: "".to_owned() }
}
}
}
24 changes: 13 additions & 11 deletions pa_note_server.wasm/panote_auth/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
mod gen_api;
mod r#impl;

use crate::gen_api::VerifyTokenReq;
use gen_api::{ApiHandler, LoginReq};
use serde_json;
use serde_json::Value;
use std::mem::ManuallyDrop;
use wasm_serverless_lib::*;
mod r#impl;
mod r#struct;
use r#struct::*;

trait AuthApi {
fn login(&self, req: LoginReq) -> LoginResp;
fn verify_token(&self, req: VerifyTokenReq) -> VerifyTokenResp;
}
// trait AuthApi {
// fn login(&self, req: LoginReq) -> LoginResp;
// fn verify_token(&self, req: VerifyTokenReq) -> VerifyTokenResp;
// }

pub struct Impl;

Expand All @@ -23,8 +25,8 @@ fn login(http_json_ptr: i32, http_json_len: i32) {
)
};
if let Ok(req) = serde_json::from_str::<LoginReq>(&json_str) {
let resp = Impl.login(req);
let resp_str = serde_json::to_string(&resp).unwrap();
let resp = Impl.handle_login(req);
let resp_str = serde_json::to_string(&resp.serialize()).unwrap();
unsafe { write_result(resp_str.as_ptr(), resp_str.len() as i32) }
}
}
Expand All @@ -38,8 +40,8 @@ fn verify_token(http_json_ptr: i32, http_json_len: i32) {
)
};
if let Ok(req) = serde_json::from_str::<VerifyTokenReq>(&json_str) {
let resp = Impl.verify_token(req);
let resp_str = serde_json::to_string(&resp).unwrap();
let resp = Impl.handle_verify_token(req);
let resp_str = serde_json::to_string(&resp.serialize()).unwrap();
unsafe { write_result(resp_str.as_ptr(), resp_str.len() as i32) }
}
println!("verify_token done");
Expand Down
12 changes: 0 additions & 12 deletions pa_note_server.wasm/panote_auth/src/struct.rs

This file was deleted.

Loading

0 comments on commit 89541fb

Please sign in to comment.