Skip to content

Commit

Permalink
Add urlencoding crate and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Almaju committed Feb 7, 2024
1 parent a3e0541 commit f89f0f7
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 6 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions auth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ regex = "1.10.3"
serde = "1.0.196"
serde_json = "1.0.113"
sha2 = "0.10.8"
urlencoding = { version = "2.1.3", optional = true }
uuid = { version = "1.7.0", features = ["serde", "v4"] }
wasm-bindgen = { version = "0.2.90", optional = true }

Expand All @@ -27,7 +28,7 @@ serde_json = "1.0.113"
tokio = { version = "1.0", features = ["full"] }

[features]
default = ["axum", "mongodb", "wasm"]
# default = ["axum", "mongodb", "wasm"]
axum = ["dep:axum"]
mongodb = ["dep:futures", "dep:mongodb"]
wasm = ["dep:wasm-bindgen"]
wasm = ["dep:urlencoding", "dep:wasm-bindgen"]
19 changes: 16 additions & 3 deletions auth/src/clients/wasm/api.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
use wasm_bindgen::prelude::*;

use super::linkedin;

#[wasm_bindgen]
struct AuthApi {
linkedin: LinkedInApi,
#[derive(Clone, Debug, PartialEq)]
pub struct AuthApi {
linkedin: Option<linkedin::Api>,
}

#[wasm_bindgen]
struct LinkedInApi;
impl AuthApi {
#[wasm_bindgen(constructor)]
pub fn new(linkedin: Option<linkedin::Api>) -> Self {
Self { linkedin }
}

#[wasm_bindgen]
pub fn linkedin(self) -> linkedin::Api {
self.linkedin.expect("LinkedIn API is not available")
}
}
83 changes: 83 additions & 0 deletions auth/src/clients/wasm/linkedin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
#[derive(Clone, Debug, PartialEq)]
pub struct Api {
client_id: String,
redirect_uri: String,
}

#[wasm_bindgen]
impl Api {
#[wasm_bindgen(constructor)]
pub fn new(client_id: &str, redirect_uri: &str) -> Self {
Self {
client_id: client_id.to_string(),
redirect_uri: redirect_uri.to_string(),
}
}

#[wasm_bindgen]
pub fn sign_in(&self) -> String {
self.sign_in_with_params(SignInParams::new())
}

#[wasm_bindgen]
pub fn sign_in_with_params(&self, params: SignInParams) -> String {
let scopes = params
.scopes
.iter()
.map(|scope| scope.to_string())
.collect::<Vec<String>>()
.join(" ");

format!("https://www.linkedin.com/oauth/v2/authorization?response_type={}&client_id={}&redirect_uri={}&scope={}", &params.response_type, &self.client_id, urlencoding::encode(&self.redirect_uri), urlencoding::encode(&scopes))
}
}

#[wasm_bindgen]
pub struct SignInParams {
response_type: Responsetype,
scopes: Vec<Scope>,
}

#[wasm_bindgen]
impl SignInParams {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self {
response_type: Responsetype::Code,
scopes: vec![Scope::Openid, Scope::Profile, Scope::Email],
}
}
}

#[wasm_bindgen]
pub enum Responsetype {
Code,
}

impl std::fmt::Display for Responsetype {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Responsetype::Code => write!(f, "code"),
}
}
}

#[wasm_bindgen]
pub enum Scope {
Openid,
Profile,
Email,
}

impl std::fmt::Display for Scope {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Scope::Openid => write!(f, "openid"),
Scope::Profile => write!(f, "profile"),
Scope::Email => write!(f, "email"),
}
}
}
5 changes: 4 additions & 1 deletion auth/src/clients/wasm/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
pub mod api;
mod api;
pub mod linkedin;

pub use api::AuthApi;

0 comments on commit f89f0f7

Please sign in to comment.