-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rework /auth into /token and read from db
- Loading branch information
Showing
12 changed files
with
229 additions
and
49 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE session; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CREATE TABLE session ( | ||
id uuid NOT NULL PRIMARY KEY, | ||
uid character varying(255) NOT NULL, | ||
name character varying NOT NULL, | ||
email character varying NOT NULL, | ||
groups character varying[], | ||
expire timestamp with time zone NOT NULL | ||
); | ||
|
||
CREATE INDEX session_expire_idx ON session (expire); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env sh | ||
|
||
pack() { | ||
while read -r line; do | ||
printf '%s\\n' "$line" | ||
done | ||
echo "$line" | ||
} | ||
|
||
SECRET_KEY=$(openssl genpkey -algorithm ed25519) | ||
PUBLIC_KEY=$(echo "$SECRET_KEY" | openssl pkey -pubout) | ||
|
||
echo "POSER_AUTH_SECRET_KEY=\"$(echo "$SECRET_KEY" | pack)\"" | ||
echo "POSER_AUTH_PUBLIC_KEY=\"$(echo "$PUBLIC_KEY" | pack)\"" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env sh | ||
|
||
DATABASE_URI=${POSER_AUTH_DATABASE_URI:-postgresql://poser@localhost/poser} | ||
|
||
psql "$DATABASE_URI" < "$1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
//! A route for requesting tokens from a user session. | ||
use std::collections::HashMap; | ||
|
||
use crate::token::{self, UserToken}; | ||
use crate::ServerState; | ||
|
||
use axum::{ | ||
extract::{Form, State}, | ||
http::StatusCode, | ||
response::{IntoResponse, Json, Response}, | ||
}; | ||
use pasetors::{keys::AsymmetricSecretKey, version4::V4}; | ||
use serde_json::json; | ||
use thiserror::Error; | ||
use time::OffsetDateTime; | ||
use tokio_postgres::Client; | ||
use tracing::error; | ||
use uuid::Uuid; | ||
|
||
/// Errors returned by a the handler. | ||
#[derive(Error, Clone, Debug)] | ||
pub enum TokenError { | ||
#[error("invalid session token")] | ||
InvalidSessionToken, | ||
#[error("invalid session")] | ||
InvalidSession, | ||
#[error("missing session token")] | ||
MissingSessionToken, | ||
#[error("error generating id token")] | ||
PasetoError(#[from] token::TokenError), | ||
} | ||
|
||
/// A handler to generate a short-lived id token from a user's session token. | ||
/// | ||
/// Given a session token (stored in the auth cookie), this route either | ||
/// returns 200 OK with a short-lived Paseto of the user or 400 Bad Request | ||
/// with an error message and possibly the configured login URL for | ||
/// redirecting the user. | ||
#[axum::debug_handler(state = ServerState)] | ||
pub async fn token_handler( | ||
State(state): State<ServerState>, | ||
Form(params): Form<HashMap<String, String>>, | ||
) -> Result<Response, TokenError> { | ||
let session_token = params.get("code").ok_or_else(|| { | ||
error!("missing session token parameter"); | ||
TokenError::MissingSessionToken | ||
})?; | ||
|
||
let session_id = Uuid::try_parse(session_token).map_err(|_| { | ||
error!("failed to parse session token as uuid"); | ||
TokenError::InvalidSessionToken | ||
})?; | ||
|
||
let token = build_token(&session_id, &state.db, &state.config.key).await?; | ||
|
||
Ok(Json(json!({ "expires_in": 3600, "id_token": token })).into_response()) | ||
} | ||
|
||
async fn build_token( | ||
session_id: &Uuid, | ||
db: &Client, | ||
key: &AsymmetricSecretKey<V4>, | ||
) -> Result<String, TokenError> { | ||
let session = db | ||
.query_one("SELECT * from session WHERE id = $1::UUID", &[&session_id]) | ||
.await | ||
.map_err(|e| { | ||
error!("database error: {}", e); | ||
TokenError::InvalidSession | ||
})?; | ||
|
||
let expiration: OffsetDateTime = session.get("expire"); | ||
if expiration < OffsetDateTime::now_utc() { | ||
error!("session is expired"); | ||
return Err(TokenError::InvalidSession); | ||
} | ||
|
||
let token = UserToken { | ||
id: session.get("uid"), | ||
name: session.get("name"), | ||
email: session.get("email"), | ||
groups: session.get("groups"), | ||
}; | ||
|
||
token.sign(key).map_err(|e| { | ||
error!("error generating token: {}", e); | ||
TokenError::PasetoError(e) | ||
}) | ||
} | ||
|
||
impl IntoResponse for TokenError { | ||
fn into_response(self) -> Response { | ||
let response = match self { | ||
TokenError::InvalidSessionToken | TokenError::MissingSessionToken => { | ||
json!({ "error": "invalid request" }) | ||
} | ||
TokenError::InvalidSession => json!({ "error": "bad session" }), | ||
TokenError::PasetoError(_) => json!({ "error": "internal error" }), | ||
}; | ||
|
||
(StatusCode::BAD_REQUEST, Json(response)).into_response() | ||
} | ||
} |
Oops, something went wrong.