Skip to content

Commit c5a2268

Browse files
committed
Update to oauth 5.0
1 parent 9ff136a commit c5a2268

File tree

5 files changed

+66
-53
lines changed

5 files changed

+66
-53
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ jobs:
1212
contents: write
1313
runs-on: ubuntu-latest
1414
steps:
15-
- uses: actions/checkout@v4
15+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
1616

1717
# build frontend
18-
- uses: actions/setup-node@v4
18+
- uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
1919
with:
2020
node-version: 22
2121
cache-dependency-path: frontend/package-lock.lock
@@ -24,7 +24,7 @@ jobs:
2424

2525
# build backend
2626
- uses: dtolnay/rust-toolchain@stable
27-
- uses: Swatinem/rust-cache@v2
27+
- uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1
2828
- run: cargo fmt --check
2929
- run: cargo clippy --all-features -- --deny "warnings"
3030
- run: cargo test
@@ -45,7 +45,7 @@ jobs:
4545
working-directory: release
4646
- run: git tag $TAG
4747
- run: git push origin $TAG
48-
- uses: softprops/action-gh-release@v2
48+
- uses: softprops/action-gh-release@153bb8e04406b158c6c84fc1615b65b24149a1fe # v2.6.1
4949
with:
5050
body: Automatic release, commit ${{ github.sha }}
5151
tag_name: ${{ env.TAG }}

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "etes"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
edition = "2024"
55

66
[dependencies]
@@ -17,7 +17,7 @@ cookie = "0.18"
1717
futures = "0.3"
1818
hyper = { version = "1.0", features = ["full"] }
1919
hyper-util = { version = "0.1", features = ["client-legacy"] }
20-
oauth2 = "4.4"
20+
oauth2 = "5.0"
2121
parking_lot = "0.12"
2222
rand = "0.10"
2323
reqwest = { version = "0.13", features = ["json"] }

src/auth.rs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use axum_extra::extract::{PrivateCookieJar, cookie::Cookie};
1010
use cookie::{Key, SameSite};
1111
use hyper::header::{ACCEPT, USER_AGENT};
1212
use oauth2::{
13-
AuthUrl, AuthorizationCode, ClientId, ClientSecret, CsrfToken, RedirectUrl, Scope,
14-
TokenResponse, TokenUrl, basic::BasicClient, reqwest::async_http_client,
13+
AuthUrl, AuthorizationCode, ClientId, ClientSecret, CsrfToken, EndpointNotSet, EndpointSet,
14+
RedirectUrl, Scope, TokenResponse, TokenUrl, basic::BasicClient, reqwest as oauth2_reqwest,
1515
};
1616
use serde::Deserialize;
1717
use std::fmt::Debug;
@@ -27,9 +27,13 @@ static GITHUB_TOKEN_URL: &str = "https://github.com/login/oauth/access_token";
2727
static GITHUB_USER_URL: &str = "https://api.github.com/user";
2828
static GITHUB_ACCEPT_TYPE: &str = "application/vnd.github+json";
2929

30+
type GithubClient =
31+
BasicClient<EndpointSet, EndpointNotSet, EndpointNotSet, EndpointNotSet, EndpointSet>;
32+
3033
#[derive(Clone)]
3134
pub struct GithubOauthService {
32-
oauth_client: BasicClient,
35+
oauth_client: GithubClient,
36+
http_client: oauth2_reqwest::Client,
3337
session_key: Key,
3438
}
3539

@@ -43,18 +47,22 @@ impl GithubOauthService {
4347
/// Creates a new instance of `GithubOauthService`.
4448
/// Returns a `Result` containing the `GithubOauthService` instance or an `Error` if there was an error creating the service.
4549
pub fn new(config: &Config) -> anyhow::Result<Self> {
46-
let oauth_client = BasicClient::new(
47-
ClientId::new(config.github_client_id.clone()),
48-
Some(ClientSecret::new(config.github_client_secret.clone())),
49-
AuthUrl::from_url(GITHUB_AUTH_URL.parse()?),
50-
Some(TokenUrl::from_url(GITHUB_TOKEN_URL.parse()?)),
51-
)
52-
.set_redirect_uri(RedirectUrl::from_url(config.authorize_url.parse()?));
50+
let oauth_client: GithubClient =
51+
BasicClient::new(ClientId::new(config.github_client_id.clone()))
52+
.set_client_secret(ClientSecret::new(config.github_client_secret.clone()))
53+
.set_auth_uri(AuthUrl::from_url(GITHUB_AUTH_URL.parse()?))
54+
.set_token_uri(TokenUrl::from_url(GITHUB_TOKEN_URL.parse()?))
55+
.set_redirect_uri(RedirectUrl::from_url(config.authorize_url.parse()?));
56+
57+
let http_client = oauth2_reqwest::Client::builder()
58+
.redirect(oauth2_reqwest::redirect::Policy::none())
59+
.build()?;
5360

5461
let session_key: Key = Key::from(&sha512(&config.session_key));
5562

5663
Ok(Self {
5764
oauth_client,
65+
http_client,
5866
session_key,
5967
})
6068
}
@@ -111,7 +119,8 @@ pub(super) async fn login(
111119
let updated_jar = jar.add(csrf_cookie);
112120

113121
// Return the updated cookie jar and a redirect response to the authorization URL
114-
Ok((updated_jar, Redirect::to(auth_url.to_string().as_str())))
122+
let auth_url = auth_url.to_string();
123+
Ok((updated_jar, Redirect::to(&auth_url)))
115124
}
116125

117126
/// Handles the logout request.
@@ -174,7 +183,7 @@ pub(super) async fn authorize(
174183
let token = service
175184
.oauth_client
176185
.exchange_code(AuthorizationCode::new(query.code.clone()))
177-
.request_async(async_http_client)
186+
.request_async(&service.http_client)
178187
.await
179188
.context("Invalid token provided")?;
180189

@@ -198,21 +207,23 @@ pub(super) async fn authorize(
198207
return Err(AppError::Client(anyhow!("Invalid CSRF token")));
199208
}
200209

201-
// Create a new HTTP client
202-
let client = reqwest::Client::new();
203-
204210
// Fetch user data from the GitHub API
205-
let user: GitHubUser = client
211+
let user_response = service
212+
.http_client
206213
.get(GITHUB_USER_URL)
207214
.header(ACCEPT, HeaderValue::from_static(GITHUB_ACCEPT_TYPE))
208215
.header(USER_AGENT, HeaderValue::from_static(USER_AGENT_VALUE))
209216
.bearer_auth(token.access_token().secret())
210217
.send()
211218
.await
212-
.context("Failed to fetch user data")?
213-
.json()
214-
.await
215-
.context("Failed te deserialize GitHub user data")?;
219+
.context("Failed to fetch user data")?;
220+
let user: GitHubUser = serde_json::from_slice(
221+
&user_response
222+
.bytes()
223+
.await
224+
.context("Failed to read GitHub user response body")?,
225+
)
226+
.context("Failed te deserialize GitHub user data")?;
216227

217228
// Serialize the user data as a string
218229
let session_cookie_value = serde_json::to_string(&user)?;

src/github.rs

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -177,31 +177,34 @@ impl GitHubState {
177177

178178
for edge in root.data.repository.pull_requests.edges {
179179
let node = edge.node;
180-
let commit = node.status_check_rollup.commit;
181180

182-
let assignees = node
183-
.assignees
184-
.edges
185-
.into_iter()
186-
.map(|edge| edge.node)
187-
.collect();
188-
189-
let pull = Pull {
190-
number: node.number,
191-
created_at: node.created_at,
192-
is_draft: node.is_draft,
193-
title: node.title,
194-
status: node.status_check_rollup.state,
195-
assignees,
196-
commit: Commit {
197-
date: commit.authored_date,
198-
hash: commit.oid,
199-
message: None,
200-
url: None,
201-
},
202-
};
181+
if let Some(status_check_rollup) = node.status_check_rollup {
182+
let commit = status_check_rollup.commit;
183+
184+
let assignees = node
185+
.assignees
186+
.edges
187+
.into_iter()
188+
.map(|edge| edge.node)
189+
.collect();
190+
191+
let pull = Pull {
192+
number: node.number,
193+
created_at: node.created_at,
194+
is_draft: node.is_draft,
195+
title: node.title,
196+
status: status_check_rollup.state,
197+
assignees,
198+
commit: Commit {
199+
date: commit.authored_date,
200+
hash: commit.oid,
201+
message: None,
202+
url: None,
203+
},
204+
};
203205

204-
pulls.push(pull);
206+
pulls.push(pull);
207+
}
205208
}
206209

207210
Ok(GitHubState {
@@ -260,13 +263,13 @@ structstruck::strike! {
260263
node: Assignee,
261264
}>,
262265
},
263-
status_check_rollup: pub struct StatusCheckRollup {
266+
status_check_rollup: Option<pub struct StatusCheckRollup {
264267
pub commit: struct CheckCommit {
265268
pub authored_date: DateTime<Utc>,
266269
pub oid: CommitHash,
267270
},
268271
pub state: WorkflowStatus,
269-
},
272+
}>,
270273
}
271274
}>,
272275
}

src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use auth::GithubOauthService;
33
use axum::{
44
Router,
55
body::Body,
6-
extract::FromRef,
7-
extract::State,
6+
extract::{FromRef, State},
87
http::{HeaderValue, header, header::CONTENT_SECURITY_POLICY},
98
middleware::{self, Next},
109
routing::{any, get, put},

0 commit comments

Comments
 (0)