validate-jwt-query-fix
This commit is contained in:
parent
d8375f39d3
commit
d15633af0f
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -455,7 +455,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "discoursio-presence"
|
name = "discoursio-presence"
|
||||||
version = "0.2.14"
|
version = "0.2.15"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"actix-web",
|
"actix-web",
|
||||||
"futures",
|
"futures",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "discoursio-presence"
|
name = "discoursio-presence"
|
||||||
version = "0.2.14"
|
version = "0.2.15"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
29
src/data.rs
29
src/data.rs
|
@ -1,4 +1,4 @@
|
||||||
use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION, CONTENT_TYPE};
|
use reqwest::header::{HeaderMap, HeaderValue, CONTENT_TYPE};
|
||||||
use reqwest::Client as HTTPClient;
|
use reqwest::Client as HTTPClient;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
@ -9,19 +9,22 @@ use crate::SSEMessageData;
|
||||||
|
|
||||||
pub async fn get_auth_id(token: &str) -> Result<i32, Box<dyn Error>> {
|
pub async fn get_auth_id(token: &str) -> Result<i32, Box<dyn Error>> {
|
||||||
let auth_api_base = env::var("AUTH_URL")?;
|
let auth_api_base = env::var("AUTH_URL")?;
|
||||||
let (query_name, query_type) = match auth_api_base.contains("auth.discours.io") {
|
|
||||||
true => ("session", "query"), // authorizer
|
|
||||||
_ => ("getSession", "mutation"), // v2
|
|
||||||
};
|
|
||||||
let operation = "GetUserId";
|
|
||||||
let mut headers = HeaderMap::new();
|
let mut headers = HeaderMap::new();
|
||||||
headers.insert(AUTHORIZATION, HeaderValue::from_str(token)?);
|
|
||||||
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
|
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
|
||||||
|
|
||||||
let gql = json!({
|
let gql = json!({
|
||||||
"query": format!("{} {} {{ {} {{ user {{ id }} }} }}", query_type, operation, query_name),
|
"query": r#"query ValidateJWTToken($params: ValidateJWTTokenInput) {
|
||||||
"operationName": operation,
|
validate_jwt_token(params: $params) {
|
||||||
"variables": HashMap::<String, String>::new()
|
is_valid
|
||||||
|
claims
|
||||||
|
}
|
||||||
|
}"#,
|
||||||
|
"variables": {
|
||||||
|
"params": {
|
||||||
|
"token_type": "access_token",
|
||||||
|
"token": token,
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let client = HTTPClient::new();
|
let client = HTTPClient::new();
|
||||||
|
@ -36,9 +39,9 @@ pub async fn get_auth_id(token: &str) -> Result<i32, Box<dyn Error>> {
|
||||||
let r: HashMap<String, serde_json::Value> = response.json().await?;
|
let r: HashMap<String, serde_json::Value> = response.json().await?;
|
||||||
let user_id = r
|
let user_id = r
|
||||||
.get("data")
|
.get("data")
|
||||||
.and_then(|data| data.get(query_name))
|
.and_then(|data| data.get("validate_jwt_token"))
|
||||||
.and_then(|query| query.get("user"))
|
.and_then(|query| query.get("clams"))
|
||||||
.and_then(|user| user.get("id"))
|
.and_then(|claims| claims.get("sub"))
|
||||||
.and_then(|id| id.as_i64());
|
.and_then(|id| id.as_i64());
|
||||||
|
|
||||||
match user_id {
|
match user_id {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user