Show auth success
This commit is contained in:
parent
1403961a09
commit
7a8cca8fe7
4 changed files with 34 additions and 7 deletions
|
|
@ -56,6 +56,7 @@ impl From<StoredDocumentVersion> for DocumentVersionWithoutContent {
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct PingResponse {
|
pub struct PingResponse {
|
||||||
pub server_version: String,
|
pub server_version: String,
|
||||||
|
pub is_authenticated: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, JsonSchema)]
|
#[derive(Debug, Clone, Serialize, JsonSchema)]
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,23 @@
|
||||||
use axum::Json;
|
use axum::{extract::State, Json};
|
||||||
|
use axum_extra::{
|
||||||
|
headers::{authorization::Bearer, Authorization},
|
||||||
|
TypedHeader,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{database::models::PingResponse, errors::SyncServerError};
|
use super::auth::{self, auth};
|
||||||
|
use crate::{app_state::AppState, database::models::PingResponse, errors::SyncServerError};
|
||||||
|
|
||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
pub async fn ping() -> Result<Json<PingResponse>, SyncServerError> {
|
pub async fn ping(
|
||||||
|
maybe_auth_header: Option<TypedHeader<Authorization<Bearer>>>,
|
||||||
|
State(state): State<AppState>,
|
||||||
|
) -> Result<Json<PingResponse>, SyncServerError> {
|
||||||
|
let is_authenticated = maybe_auth_header
|
||||||
|
.map(|auth_header| auth(&state, auth_header.token()).is_ok())
|
||||||
|
.unwrap_or(false);
|
||||||
|
|
||||||
Ok(Json(PingResponse {
|
Ok(Json(PingResponse {
|
||||||
server_version: env!("CARGO_PKG_VERSION").to_string(),
|
server_version: env!("CARGO_PKG_VERSION").to_string(),
|
||||||
|
is_authenticated,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,14 @@ export class SyncServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async ping(): Promise<components["schemas"]["PingResponse"]> {
|
public async ping(): Promise<components["schemas"]["PingResponse"]> {
|
||||||
const response = await this.client.GET("/ping");
|
const response = await this.client.GET("/ping", {
|
||||||
|
params: {
|
||||||
|
header: {
|
||||||
|
authorization:
|
||||||
|
"Bearer " + this.database.getSettings().token,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
Logger.getInstance().info(
|
Logger.getInstance().info(
|
||||||
"Ping response: " + JSON.stringify(response.data)
|
"Ping response: " + JSON.stringify(response.data)
|
||||||
|
|
|
||||||
|
|
@ -46,9 +46,15 @@ export class SyncSettingsTab extends PluginSettingTab {
|
||||||
button.setButtonText("Test Connection").onClick(async () => {
|
button.setButtonText("Test Connection").onClick(async () => {
|
||||||
try {
|
try {
|
||||||
const result = await this.syncServer.ping();
|
const result = await this.syncServer.ping();
|
||||||
new Notice(
|
if (result.isAuthenticated) {
|
||||||
`Successfully connected to server! (server version: ${result.serverVersion})`
|
new Notice(
|
||||||
);
|
`Successfully authenticated with the server (version: ${result.serverVersion})!`
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
new Notice(
|
||||||
|
`Successfully connected to server (version: ${result.serverVersion}) but failed to authenticate.`
|
||||||
|
);
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
new Notice("Failed to connect to server: " + e);
|
new Notice("Failed to connect to server: " + e);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue