mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-04 04:56:49 +00:00
feat(api): add internal auth
This commit is contained in:
parent
9c1acd84e1
commit
dd14e7daef
3 changed files with 30 additions and 3 deletions
|
|
@ -7,11 +7,16 @@ pub const INTERNAL_APPID_HEADER: &'static str = "x-pluralkit-appid";
|
||||||
pub struct AuthState {
|
pub struct AuthState {
|
||||||
system_id: Option<i32>,
|
system_id: Option<i32>,
|
||||||
app_id: Option<i32>,
|
app_id: Option<i32>,
|
||||||
|
internal: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AuthState {
|
impl AuthState {
|
||||||
pub fn new(system_id: Option<i32>, app_id: Option<i32>) -> Self {
|
pub fn new(system_id: Option<i32>, app_id: Option<i32>, internal: bool) -> Self {
|
||||||
Self { system_id, app_id }
|
Self {
|
||||||
|
system_id,
|
||||||
|
app_id,
|
||||||
|
internal,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn system_id(&self) -> Option<i32> {
|
pub fn system_id(&self) -> Option<i32> {
|
||||||
|
|
@ -22,6 +27,10 @@ impl AuthState {
|
||||||
self.app_id
|
self.app_id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn internal(&self) -> bool {
|
||||||
|
self.internal
|
||||||
|
}
|
||||||
|
|
||||||
pub fn access_level_for(&self, a: &impl Authable) -> PrivacyLevel {
|
pub fn access_level_for(&self, a: &impl Authable) -> PrivacyLevel {
|
||||||
if self
|
if self
|
||||||
.system_id
|
.system_id
|
||||||
|
|
|
||||||
|
|
@ -58,8 +58,23 @@ pub async fn auth(State(ctx): State<ApiContext>, mut req: Request, next: Next) -
|
||||||
authed_app_id = Some(1);
|
authed_app_id = Some(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo: fix syntax
|
||||||
|
let internal = if req.headers().get("x-pluralkit-client-ip").is_none()
|
||||||
|
&& let Some(auth_header) = req
|
||||||
|
.headers()
|
||||||
|
.get("x-pluralkit-internalauth")
|
||||||
|
.map(|h| h.to_str().ok())
|
||||||
|
.flatten()
|
||||||
|
&& let Some(real_token) = libpk::config.internal_auth.clone()
|
||||||
|
&& auth_header.as_bytes().ct_eq(real_token.as_bytes()).into()
|
||||||
|
{
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
};
|
||||||
|
|
||||||
req.extensions_mut()
|
req.extensions_mut()
|
||||||
.insert(AuthState::new(authed_system_id, authed_app_id));
|
.insert(AuthState::new(authed_system_id, authed_app_id, internal));
|
||||||
|
|
||||||
next.run(req).await
|
next.run(req).await
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -128,6 +128,9 @@ pub struct PKConfig {
|
||||||
|
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub sentry_url: Option<String>,
|
pub sentry_url: Option<String>,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub internal_auth: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PKConfig {
|
impl PKConfig {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue