Files
novem/src/api/auth.rs

88 lines
2.1 KiB
Rust
Raw Normal View History

2026-04-24 19:22:25 +01:00
use std::collections::HashMap;
2026-04-21 20:30:41 +01:00
use serde::Deserialize;
2026-04-24 19:22:25 +01:00
use crate::{
api,
query::{self, use_query},
};
pub(crate) const DEVICE_LOGIN_FLOW_URL: &str = "https://github.com/login/device";
2026-04-21 20:30:41 +01:00
#[derive(Clone)]
2026-04-22 12:41:33 +01:00
pub struct CreateDeviceCode;
2026-04-21 20:30:41 +01:00
#[derive(Deserialize)]
2026-04-24 19:22:25 +01:00
pub(crate) struct DeviceCodeResponse {
pub device_code: String,
pub user_code: String,
pub vertification_uri: Option<String>,
pub expires_in: u16,
pub interval: u16,
2026-04-21 20:30:41 +01:00
}
impl query::QueryFn for CreateDeviceCode {
type Data = DeviceCodeResponse;
type Error = api::Error;
type Context = api::QueryContext;
fn key(&self) -> &'static str {
2026-04-22 12:41:33 +01:00
"auth.device_code"
2026-04-21 20:30:41 +01:00
}
async fn run(&self, c: &Self::Context) -> Result<Self::Data, Self::Error> {
2026-04-24 19:22:25 +01:00
let res = c
2026-04-21 20:30:41 +01:00
.http
.post(format!(
"https://github.com/login/device/code?client_id={}",
c.github.client_id
))
2026-04-24 19:22:25 +01:00
.header("Accept", "application/json")
.send()
.await?;
api::parse_response(res).await
}
}
#[derive(Clone)]
pub struct RequestAccessToken {
pub device_code: String,
}
#[derive(Deserialize)]
pub struct RequestAccessTokenResponse {
pub access_token: String,
pub token_type: String,
pub scope: String,
}
impl query::QueryFn for RequestAccessToken {
type Data = RequestAccessTokenResponse;
type Error = api::Error;
type Context = api::QueryContext;
fn key(&self) -> &'static str {
"auth.access_token"
}
async fn run(&self, c: &Self::Context) -> Result<Self::Data, Self::Error> {
let mut params = HashMap::new();
params.insert("client_id", c.github.client_id);
params.insert("device_code", &self.device_code);
params.insert("grant_type", "urn:ietf:params:oauth:grant-type:device_code");
let res = c
.http
.post(format!(
"https://github.com/login/oauth/access_token?client_id={}&device_code={}",
c.github.client_id, self.device_code
))
.form(&params)
2026-04-21 20:30:41 +01:00
.send()
.await?;
2026-04-24 19:22:25 +01:00
api::parse_response(res).await
2026-04-21 20:30:41 +01:00
}
}