Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.

Commit 9c97a0c

Browse files
committed
storage: make the access token expiration optional
1 parent e6b91c1 commit 9c97a0c

11 files changed

+36
-41
lines changed

crates/graphql/src/mutations/oauth2_session.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,9 @@ impl OAuth2SessionMutations {
177177
}
178178

179179
let ttl = if permanent {
180-
// XXX: that's lazy
181-
Duration::days(365 * 50)
180+
None
182181
} else {
183-
Duration::minutes(5)
182+
Some(Duration::minutes(5))
184183
};
185184
let access_token = repo
186185
.oauth2_access_token()

crates/handlers/src/graphql/tests.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414

1515
use axum::http::Request;
16-
use chrono::Duration;
1716
use hyper::StatusCode;
1817
use mas_data_model::{AccessToken, Client, TokenType, User};
1918
use mas_router::SimpleRoute;
@@ -106,13 +105,7 @@ async fn start_oauth_session(
106105

107106
let access_token = repo
108107
.oauth2_access_token()
109-
.add(
110-
&mut rng,
111-
&state.clock,
112-
&session,
113-
access_token_str,
114-
Duration::minutes(5),
115-
)
108+
.add(&mut rng, &state.clock, &session, access_token_str, None)
116109
.await
117110
.unwrap();
118111

crates/handlers/src/oauth2/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub(crate) async fn generate_token_pair<R: RepositoryAccess>(
115115

116116
let access_token = repo
117117
.oauth2_access_token()
118-
.add(rng, clock, session, access_token_str, ttl)
118+
.add(rng, clock, session, access_token_str, Some(ttl))
119119
.await?;
120120

121121
let refresh_token = repo

crates/handlers/src/oauth2/token.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ async fn client_credentials_grant(
529529

530530
let access_token = repo
531531
.oauth2_access_token()
532-
.add(rng, clock, &session, access_token_str, ttl)
532+
.add(rng, clock, &session, access_token_str, Some(ttl))
533533
.await?;
534534

535535
let mut params = AccessTokenResponse::new(access_token.access_token).with_expires_in(ttl);

crates/storage-pg/.sqlx/query-477f79556e5777b38feb85013b4f04dbb8230e4b0b0bcc45f669d7b8d0b91db4.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/storage-pg/.sqlx/query-6554d3620a5f7fb0e85af44e8a21c2f2f3ebe4b805ec67aca4a2278a8ae16693.json

Lines changed: 0 additions & 17 deletions
This file was deleted.

crates/storage-pg/.sqlx/query-dd16942318bf38d9a245b2c86fedd3cbd6b65e7a13465552d79cd3c022122fd4.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- Copyright 2023 The Matrix.org Foundation C.I.C.
2+
--
3+
-- Licensed under the Apache License, Version 2.0 (the "License");
4+
-- you may not use this file except in compliance with the License.
5+
-- You may obtain a copy of the License at
6+
--
7+
-- http://www.apache.org/licenses/LICENSE-2.0
8+
--
9+
-- Unless required by applicable law or agreed to in writing, software
10+
-- distributed under the License is distributed on an "AS IS" BASIS,
11+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
-- See the License for the specific language governing permissions and
13+
-- limitations under the License.
14+
15+
-- This makes the `expires_at` column nullable on the `oauth2_access_tokens`.
16+
-- This is to allow permanent tokens to be created via the admin API.
17+
ALTER TABLE oauth2_access_tokens
18+
ALTER COLUMN expires_at DROP NOT NULL;
19+

crates/storage-pg/src/oauth2/access_token.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct OAuth2AccessTokenLookup {
4242
oauth2_session_id: Uuid,
4343
access_token: String,
4444
created_at: DateTime<Utc>,
45-
expires_at: DateTime<Utc>,
45+
expires_at: Option<DateTime<Utc>>,
4646
revoked_at: Option<DateTime<Utc>>,
4747
}
4848

@@ -59,7 +59,7 @@ impl From<OAuth2AccessTokenLookup> for AccessToken {
5959
session_id: value.oauth2_session_id.into(),
6060
access_token: value.access_token,
6161
created_at: value.created_at,
62-
expires_at: Some(value.expires_at),
62+
expires_at: value.expires_at,
6363
}
6464
}
6565
}
@@ -146,10 +146,10 @@ impl<'c> OAuth2AccessTokenRepository for PgOAuth2AccessTokenRepository<'c> {
146146
clock: &dyn Clock,
147147
session: &Session,
148148
access_token: String,
149-
expires_after: Duration,
149+
expires_after: Option<Duration>,
150150
) -> Result<AccessToken, Self::Error> {
151151
let created_at = clock.now();
152-
let expires_at = created_at + expires_after;
152+
let expires_at = expires_after.map(|d| created_at + d);
153153
let id = Ulid::from_datetime_with_source(created_at.into(), rng);
154154

155155
tracing::Span::current().record("access_token.id", tracing::field::display(id));
@@ -177,7 +177,7 @@ impl<'c> OAuth2AccessTokenRepository for PgOAuth2AccessTokenRepository<'c> {
177177
access_token,
178178
session_id: session.id,
179179
created_at,
180-
expires_at: Some(expires_at),
180+
expires_at,
181181
})
182182
}
183183

crates/storage-pg/src/oauth2/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ mod tests {
270270
&clock,
271271
&session,
272272
"aabbcc".to_owned(),
273-
Duration::minutes(5),
273+
Some(Duration::minutes(5)),
274274
)
275275
.await
276276
.unwrap();

0 commit comments

Comments
 (0)