Skip to content

Commit a33a218

Browse files
committed
Merge branch 'feature/smbcloud-oauth' into development
2 parents c3690a6 + e7a84a1 commit a33a218

File tree

8 files changed

+89
-1
lines changed

8 files changed

+89
-1
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/smbcloud-model/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod app_auth;
33
pub mod error_codes;
44
pub mod forgot;
55
pub mod login;
6+
pub mod oauth;
67
pub mod project;
78
pub mod repository;
89
pub mod runner;

crates/smbcloud-model/src/oauth.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use {
2+
serde::{Deserialize, Serialize},
3+
tsync::tsync,
4+
};
5+
6+
#[derive(Serialize, Deserialize, Debug, Clone)]
7+
#[tsync]
8+
pub struct TokenResponse {
9+
pub access_token: String,
10+
pub expires_in: i32,
11+
pub refresh_token: Option<String>,
12+
pub refresh_token_expires_in: Option<String>,
13+
pub scope: String,
14+
pub token_type: String,
15+
}
16+
17+
#[derive(Serialize, Deserialize, Debug, Clone)]
18+
#[tsync]
19+
pub struct OauthRedirect {
20+
pub code: String,
21+
pub scope: String,
22+
pub authuser: i32,
23+
pub prompt: String,
24+
}
25+
26+
#[derive(Serialize, Deserialize, Debug, Clone)]
27+
#[tsync]
28+
pub struct UserInfo {
29+
pub id: String,
30+
pub email: String,
31+
pub verified_email: bool,
32+
pub name: String,
33+
pub given_name: String,
34+
pub family_name: String,
35+
pub picture: String,
36+
}

crates/smbcloud-networking-account/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ categories = [
1818
readme = "README.md"
1919

2020
[dependencies]
21-
reqwest = { workspace = true, features = ["json"] }
21+
log = { workspace = true }
22+
reqwest = { workspace = true, features = ["json", "rustls-tls-native-roots"] }
23+
openssl = { version = "0.10", features = ["vendored"] }
24+
serde_json = { workspace = true }
2225
smbcloud-model = { workspace = true }
2326
smbcloud-network = { workspace = true }
2427
smbcloud-networking = { workspace = true }

crates/smbcloud-networking-account/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ pub mod check_email;
22
pub mod login;
33
pub mod logout;
44
pub mod me;
5+
pub mod oauth;
56
pub mod remove;
67
pub mod signup;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use {
2+
log::debug,
3+
reqwest::Client,
4+
smbcloud_model::{error_codes::ErrorResponse, oauth::UserInfo},
5+
smbcloud_network::network::{self},
6+
};
7+
8+
pub async fn get_profile(access_token: String) -> Result<UserInfo, ErrorResponse> {
9+
let base_url = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json";
10+
debug!("Get profile with token: {}", access_token);
11+
let builder = Client::new()
12+
.get(base_url)
13+
.header("Authorization", format!("Bearer {}", access_token));
14+
network::request(builder).await
15+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use {
2+
log::debug,
3+
reqwest::Client,
4+
serde_json::json,
5+
smbcloud_model::{
6+
error_codes::ErrorResponse,
7+
oauth::{OauthRedirect, TokenResponse},
8+
},
9+
smbcloud_network::network::{self},
10+
};
11+
12+
pub async fn get_token(
13+
oauth_redirect: OauthRedirect,
14+
client_id: String,
15+
client_secret: String,
16+
) -> Result<TokenResponse, ErrorResponse> {
17+
let base_url = format!("https://oauth2.googleapis.com/token");
18+
debug!("Exchange code with token: {}", oauth_redirect.code);
19+
let paylod = json!({
20+
"client_id": client_id,
21+
"client_secret": client_secret,
22+
"code": oauth_redirect.code,
23+
"grant_type": "authorization_code",
24+
"redirect_uri": "http://localhost:8000"
25+
});
26+
let builder = Client::new().post(base_url).json(&paylod);
27+
network::request(builder).await
28+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod get_profile;
2+
pub mod get_token;

0 commit comments

Comments
 (0)