1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-08-09 04:22:45 +03:00

data-model: simplify tokens

This commit is contained in:
Quentin Gliech
2022-12-06 15:36:39 +01:00
parent cff3125c19
commit dff2f98167
4 changed files with 25 additions and 53 deletions

View File

@@ -41,7 +41,7 @@ pub async fn add_access_token(
session: &Session<PostgresqlBackend>,
access_token: String,
expires_after: Duration,
) -> Result<AccessToken<PostgresqlBackend>, anyhow::Error> {
) -> Result<AccessToken, anyhow::Error> {
let created_at = clock.now();
let expires_at = created_at + expires_after;
let id = Ulid::from_datetime_with_source(created_at.into(), &mut rng);
@@ -66,7 +66,7 @@ pub async fn add_access_token(
.context("could not insert oauth2 access token")?;
Ok(AccessToken {
data: id,
id,
access_token,
jti: id.to_string(),
created_at,
@@ -113,7 +113,7 @@ impl LookupError for AccessTokenLookupError {
pub async fn lookup_active_access_token(
conn: &mut PgConnection,
token: &str,
) -> Result<(AccessToken<PostgresqlBackend>, Session<PostgresqlBackend>), AccessTokenLookupError> {
) -> Result<(AccessToken, Session<PostgresqlBackend>), AccessTokenLookupError> {
let res = sqlx::query_as!(
OAuth2AccessTokenLookup,
r#"
@@ -162,7 +162,7 @@ pub async fn lookup_active_access_token(
let id = Ulid::from(res.oauth2_access_token_id);
let access_token = AccessToken {
data: id,
id,
jti: id.to_string(),
access_token: res.oauth2_access_token,
created_at: res.oauth2_access_token_created_at,
@@ -228,13 +228,13 @@ pub async fn lookup_active_access_token(
#[tracing::instrument(
skip_all,
fields(access_token.id = %access_token.data),
fields(%access_token.id),
err(Debug),
)]
pub async fn revoke_access_token(
executor: impl PgExecutor<'_>,
clock: &Clock,
access_token: AccessToken<PostgresqlBackend>,
access_token: AccessToken,
) -> anyhow::Result<()> {
let revoked_at = clock.now();
let res = sqlx::query!(
@@ -243,7 +243,7 @@ pub async fn revoke_access_token(
SET revoked_at = $2
WHERE oauth2_access_token_id = $1
"#,
Uuid::from(access_token.data),
Uuid::from(access_token.id),
revoked_at,
)
.execute(executor)

View File

@@ -42,9 +42,9 @@ pub async fn add_refresh_token(
mut rng: impl Rng + Send,
clock: &Clock,
session: &Session<PostgresqlBackend>,
access_token: AccessToken<PostgresqlBackend>,
access_token: AccessToken,
refresh_token: String,
) -> anyhow::Result<RefreshToken<PostgresqlBackend>> {
) -> anyhow::Result<RefreshToken> {
let created_at = clock.now();
let id = Ulid::from_datetime_with_source(created_at.into(), &mut rng);
tracing::Span::current().record("refresh_token.id", tracing::field::display(id));
@@ -59,7 +59,7 @@ pub async fn add_refresh_token(
"#,
Uuid::from(id),
Uuid::from(session.data),
Uuid::from(access_token.data),
Uuid::from(access_token.id),
refresh_token,
created_at,
)
@@ -68,7 +68,7 @@ pub async fn add_refresh_token(
.context("could not insert oauth2 refresh token")?;
Ok(RefreshToken {
data: id,
id,
refresh_token,
access_token: Some(access_token),
created_at,
@@ -117,8 +117,7 @@ impl LookupError for RefreshTokenLookupError {
pub async fn lookup_active_refresh_token(
conn: &mut PgConnection,
token: &str,
) -> Result<(RefreshToken<PostgresqlBackend>, Session<PostgresqlBackend>), RefreshTokenLookupError>
{
) -> Result<(RefreshToken, Session<PostgresqlBackend>), RefreshTokenLookupError> {
let res = sqlx::query_as!(
OAuth2RefreshTokenLookup,
r#"
@@ -181,7 +180,7 @@ pub async fn lookup_active_refresh_token(
(Some(id), Some(access_token), Some(created_at), Some(expires_at)) => {
let id = Ulid::from(id);
Some(AccessToken {
data: id,
id,
jti: id.to_string(),
access_token,
created_at,
@@ -192,7 +191,7 @@ pub async fn lookup_active_refresh_token(
};
let refresh_token = RefreshToken {
data: res.oauth2_refresh_token_id.into(),
id: res.oauth2_refresh_token_id.into(),
refresh_token: res.oauth2_refresh_token,
created_at: res.oauth2_refresh_token_created_at,
access_token,
@@ -261,14 +260,14 @@ pub async fn lookup_active_refresh_token(
#[tracing::instrument(
skip_all,
fields(
refresh_token.id = %refresh_token.data,
%refresh_token.id,
),
err(Debug),
)]
pub async fn consume_refresh_token(
executor: impl PgExecutor<'_>,
clock: &Clock,
refresh_token: &RefreshToken<PostgresqlBackend>,
refresh_token: &RefreshToken,
) -> Result<(), anyhow::Error> {
let consumed_at = clock.now();
let res = sqlx::query!(
@@ -277,7 +276,7 @@ pub async fn consume_refresh_token(
SET consumed_at = $2
WHERE oauth2_refresh_token_id = $1
"#,
Uuid::from(refresh_token.data),
Uuid::from(refresh_token.id),
consumed_at,
)
.execute(executor)