Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ jobs:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v2
with:
version: latest
- run: pnpm install
version: 8
run_install: true
- run: pnpm tsc

check-formatting:
Expand All @@ -19,8 +19,8 @@ jobs:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v2
with:
version: latest
- run: pnpm install
version: 8
run_install: true
- run: pnpx prettier --check .

tests:
Expand All @@ -34,6 +34,6 @@ jobs:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v2
with:
version: latest
- run: pnpm install
version: 8
run_install: true
- run: pnpm test
2 changes: 1 addition & 1 deletion .github/workflows/tauri-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:

- uses: pnpm/action-setup@v3
with:
version: latest
version: 8

- name: install Rust stable
uses: dtolnay/rust-toolchain@stable
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/dist
/src-tauri/target
/src-tauri/templates

/.pnpm-store
/pnpm-lock.yaml
104 changes: 104 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ edition = "2021"
tauri-build = { version = "1.5", features = [] }

[dependencies]
askama = "0.12.1"
oauth2 = "4.4.2"
open = "5.0.1"
reqwest = { version = "0.11.23", features = ["blocking", "json"] }
Expand Down
41 changes: 32 additions & 9 deletions src-tauri/src/oauth.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
use askama::Template;
use oauth2::{
basic::BasicClient, reqwest::http_client, AuthUrl, AuthorizationCode, ClientId, CsrfToken,
PkceCodeChallenge, RedirectUrl, Scope, TokenUrl,
};
use tauri::{AppHandle, Manager};
use tiny_http::Server;

#[derive(Template, Clone, Debug)]
#[template(path = "redirect.html")]
struct RedirectTemplate {
error: Option<String>,
message: Option<String>,
}

#[allow(clippy::needless_pass_by_value)]
#[tauri::command]
pub fn start_oauth_flow<R: tauri::Runtime>(
Expand Down Expand Up @@ -40,12 +48,19 @@ pub fn start_oauth_flow<R: tauri::Runtime>(
if let Some(request) = server.incoming_requests().next() {
let path = request.url().to_string();
let parts = serde_urlencoded::from_str::<Vec<(String, String)>>(&path[2..]).unwrap();
let code = parts
.iter()
.find(|(key, _)| key == "code")
.unwrap()
.1
.to_string();
let code = if let Some((_, value)) = parts.iter().find(|(key, _)| key == "code") {
value.to_string()
} else {
let html = RedirectTemplate {
error: Some("Login cancelled".to_string()),
message: Some("Failed to get code from the request".to_string()),
}
.render()
.unwrap();
let header = tiny_http::Header::from_bytes("Content-Type", "text/html").unwrap();
let _ = request.respond(tiny_http::Response::from_string(html).with_header(header));
return Err("Failed to get code from the request".to_string());
};

let access_token = client
.exchange_code(AuthorizationCode::new(code.to_string()))
Expand All @@ -57,9 +72,17 @@ pub fn start_oauth_flow<R: tauri::Runtime>(
.emit_all("event::update_access_token", access_token)
.expect("failed to emit event");

let _ = request.respond(tiny_http::Response::from_string(
"Thanks! You may now close this window and return to the app.",
));
let html = RedirectTemplate {
error: None,
message: Some(
"Thanks! You may now close this window and return to the app.".to_string(),
),
}
.render()
.unwrap();
let header = tiny_http::Header::from_bytes("Content-Type", "text/html").unwrap();
let _ = request.respond(tiny_http::Response::from_string(html).with_header(header));
}
Ok(())
});
}
42 changes: 42 additions & 0 deletions src-tauri/templates/redirect.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!doctype html>
<html lang="en">
<head>
<title>Lichess Broadcaster</title>
<style>
* {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
}
body {
background-color: rgb(31 41 55);
padding: 1rem;
text-align: center;
margin-top: 5rem;
font-size: 2rem;
font-weight: 600;
color: rgb(229 231 235);
}
.error {
background-color: #fca5a5;
color: #991b1b;
margin: 1rem;
padding: 1rem;
}
</style>
</head>

<body>
{% match self.error %}
{% when Some with (error) %}
<div class="error">{{ error }}</div>
{% else %}
{% endmatch %}

{% match self.message %}
{% when Some with (message) %}
<div class="message">{{ message }}</div>
{% else %}
{% endmatch %}
</body>
</html>