Can get current user identity
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
use crate::user::{APIClient, APIClientID, UserConfig, UserID};
|
||||
use crate::server::HttpFailure;
|
||||
use crate::user::{APIClient, APIClientID, RumaClient, UserConfig, UserID};
|
||||
use crate::utils::curr_time;
|
||||
use actix_remote_ip::RemoteIP;
|
||||
use actix_web::dev::Payload;
|
||||
@ -6,13 +7,14 @@ use actix_web::{FromRequest, HttpRequest};
|
||||
use bytes::Bytes;
|
||||
use jwt_simple::common::VerificationOptions;
|
||||
use jwt_simple::prelude::{Duration, HS256Key, MACLike};
|
||||
use ruma::api::{IncomingResponse, OutgoingRequest};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::net::IpAddr;
|
||||
use std::str::FromStr;
|
||||
|
||||
pub struct APIClientAuth {
|
||||
pub user: UserConfig,
|
||||
client: APIClient,
|
||||
pub client: APIClient,
|
||||
pub payload: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
@ -94,7 +96,7 @@ impl APIClientAuth {
|
||||
// Decode JWT
|
||||
let key = HS256Key::from_bytes(client.secret.as_bytes());
|
||||
let mut verif = VerificationOptions::default();
|
||||
verif.max_validity = Some(Duration::from_mins(15));
|
||||
verif.max_validity = Some(Duration::from_mins(20));
|
||||
let claims = match key.verify_token::<TokenClaims>(jwt_token, Some(verif)) {
|
||||
Ok(t) => t,
|
||||
Err(e) => {
|
||||
@ -178,6 +180,22 @@ impl APIClientAuth {
|
||||
user,
|
||||
})
|
||||
}
|
||||
|
||||
/// Get an instance of Matrix client
|
||||
pub async fn client(&self) -> anyhow::Result<RumaClient> {
|
||||
self.user.matrix_client().await
|
||||
}
|
||||
|
||||
/// Send request to matrix server
|
||||
pub async fn send_request<R: OutgoingRequest<IncomingResponse = E>, E: IncomingResponse>(
|
||||
&self,
|
||||
request: R,
|
||||
) -> anyhow::Result<E, HttpFailure> {
|
||||
match self.client().await?.send_request(request).await {
|
||||
Ok(e) => Ok(e),
|
||||
Err(e) => Err(HttpFailure::MatrixClientError(e.to_string())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromRequest for APIClientAuth {
|
||||
|
Reference in New Issue
Block a user