feat: get in the friends server
This commit is contained in:
parent
9f9393cfe5
commit
2417c109e4
9 changed files with 473 additions and 21 deletions
83
src/nnid/mapped_ids.rs
Normal file
83
src/nnid/mapped_ids.rs
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
use rocket::{get, State};
|
||||
use serde::Serialize;
|
||||
use crate::Pool;
|
||||
use crate::xml::Xml;
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename = "mapped_id")]
|
||||
struct MappedId {
|
||||
in_id: String,
|
||||
out_id: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename = "mapped_ids")]
|
||||
struct MappedIds {
|
||||
mapped_id: Vec<MappedId>,
|
||||
}
|
||||
|
||||
struct UserIdAndName {
|
||||
pid: i32,
|
||||
username: String,
|
||||
}
|
||||
|
||||
#[get("/v1/api/admin/mapped_ids?<input_type>&<output_type>&<input>")]
|
||||
pub async fn mapped_ids(pool: &State<Pool>, input_type: String, output_type: String, input: String) -> Option<Xml<MappedIds>> {
|
||||
let pool = pool.inner();
|
||||
|
||||
let is_input_pid = input_type == "pid";
|
||||
let is_output_pid = output_type == "pid";
|
||||
|
||||
let mut outputs = Vec::new();
|
||||
|
||||
for input in input.split(',') {
|
||||
if input == ""{
|
||||
continue;
|
||||
}
|
||||
let Some(user) =
|
||||
(if is_input_pid {
|
||||
let id: i32 = input.parse().ok()?;
|
||||
|
||||
sqlx::query_as!(
|
||||
UserIdAndName,
|
||||
"select pid, username from users where pid = $1",
|
||||
id
|
||||
).fetch_one(pool)
|
||||
.await.ok()
|
||||
} else {
|
||||
sqlx::query_as!(
|
||||
UserIdAndName,
|
||||
"select pid, username from users where username = $1",
|
||||
input
|
||||
).fetch_one(pool)
|
||||
.await.ok()
|
||||
}) else {
|
||||
outputs.push(MappedId{
|
||||
in_id: input.to_string(),
|
||||
out_id: None,
|
||||
});
|
||||
|
||||
continue
|
||||
};
|
||||
|
||||
|
||||
if is_output_pid{
|
||||
outputs.push(MappedId{
|
||||
in_id: input.to_string(),
|
||||
out_id: Some(user.pid.to_string()),
|
||||
})
|
||||
} else {
|
||||
outputs.push(MappedId{
|
||||
in_id: input.to_string(),
|
||||
out_id: Some(user.username),
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Some(Xml(
|
||||
MappedIds{
|
||||
mapped_id: outputs
|
||||
}
|
||||
))
|
||||
}
|
||||
|
|
@ -7,3 +7,4 @@ pub mod oauth;
|
|||
mod pid_distribution;
|
||||
pub mod people;
|
||||
pub mod provider;
|
||||
pub mod mapped_ids;
|
||||
|
|
|
|||
|
|
@ -6,12 +6,19 @@ use sqlx::types::ipnetwork::IpNetwork::V4;
|
|||
use crate::account::account::Auth;
|
||||
use crate::nnid::oauth::generate_token::create_token;
|
||||
use crate::nnid::oauth::generate_token::token_type::NEX_TOKEN;
|
||||
use crate::nnid::provider::Test::{A, B};
|
||||
use crate::Pool;
|
||||
use crate::xml::Xml;
|
||||
|
||||
enum Test{
|
||||
A(String),
|
||||
B(i32)
|
||||
}
|
||||
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename = "nex_token")]
|
||||
struct NexToken{
|
||||
pub struct NexToken{
|
||||
host: Ipv4Addr,
|
||||
nex_password: String,
|
||||
pid: i32,
|
||||
|
|
@ -19,20 +26,51 @@ struct NexToken{
|
|||
token: String
|
||||
}
|
||||
|
||||
#[get("/v1/api/provider/nex_token/@me?<game_server_id>")]
|
||||
pub async fn get_nex_token(pool: &State<Pool>, auth: Auth<true>, game_server_id: String) -> Option<Xml<NexToken>>{
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename = "service_token")]
|
||||
pub struct ServiceToken{
|
||||
token: String
|
||||
}
|
||||
|
||||
#[get("/v1/api/provider/service_token/@me")]
|
||||
pub async fn get_service_token(pool: &State<Pool>, auth: Auth<true>) -> Option<Xml<ServiceToken>>{
|
||||
// just gonna put this here as a side note for the future:
|
||||
// we could also be using key derivation to derive the nex token as if it were a key
|
||||
// that way we could reduce the data the database needs to store and also reduce the transfer
|
||||
// cost of sending an entire row from the user table (which is required for the auth code unless
|
||||
// we change the way we read in data to essentially having the user object be a proxy for its
|
||||
// table row)
|
||||
|
||||
let pool = pool.inner();
|
||||
|
||||
let token = create_token(pool, auth.pid, NEX_TOKEN, None).await;
|
||||
|
||||
|
||||
|
||||
Some(
|
||||
Xml(
|
||||
ServiceToken{
|
||||
token
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
#[get("/v1/api/provider/nex_token/@me?<game_server_id>")]
|
||||
pub async fn get_nex_token(pool: &State<Pool>, auth: Auth<true>, game_server_id: &str) -> Option<Xml<NexToken>>{
|
||||
// just gonna put this here as a side note for the future:
|
||||
// we could also be using key derivation to derive the nex token as if it were a key
|
||||
// that way we could reduce the data the database needs to store and also reduce the transfer
|
||||
// cost of sending an entire row from the user table (which is required for the auth code unless
|
||||
// we change the way we read in data to essentially having the user object be a proxy for its
|
||||
// table row)
|
||||
|
||||
let pool = pool.inner();
|
||||
|
||||
let server = sqlx::query!(
|
||||
"select * from nex_servers where game_server_id = $1",
|
||||
"select address, port from nex_servers where game_server_id = $1",
|
||||
game_server_id
|
||||
) .fetch_one(pool).await.ok()?;
|
||||
) .fetch_one(pool).await.unwrap();
|
||||
|
||||
let token = create_token(pool, auth.pid, NEX_TOKEN, None).await;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue