Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
mpiorowski committed Jan 8, 2024
1 parent ebf9bf2 commit 200cfaa
Showing 1 changed file with 0 additions and 16 deletions.
16 changes: 0 additions & 16 deletions service-auth/src/auth_oauth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use anyhow::Result;
use oauth2::{basic::BasicClient, AuthUrl, ClientId, ClientSecret, RedirectUrl, TokenUrl};
use rusve_auth::Env;
use serde::{Deserialize, Serialize};
use tonic::metadata::{Ascii, MetadataValue};

pub enum OAuthProvider {
Google,
Expand All @@ -23,7 +22,6 @@ pub trait OAuth {
Self: Sized;
fn build_oauth_client(&self) -> BasicClient;
async fn get_user_info(&self, token: &str) -> Result<OAuthUser>;
async fn generate_jwt(&self, user: OAuthUser) -> Result<MetadataValue<Ascii>>;
}

pub struct OAuthConfig {
Expand All @@ -35,7 +33,6 @@ pub struct OAuthConfig {
redirect_url: String,
pub scopes: Vec<String>,
user_info_url: String,
jwt_secret: String,
}

#[derive(Debug, serde::Deserialize, serde::Serialize)]
Expand Down Expand Up @@ -65,7 +62,6 @@ impl OAuth for OAuthConfig {
redirect_url: format!("{}/oauth-callback/google", env.auth_url),
user_info_url: "https://www.googleapis.com/oauth2/v3/userinfo".to_string(),
scopes: vec!["email".to_string(), "openid".to_string()],
jwt_secret: env.jwt_secret,
}),
"github" => Ok(Self {
provider: OAuthProvider::Github,
Expand All @@ -76,7 +72,6 @@ impl OAuth for OAuthConfig {
redirect_url: format!("{}/oauth-callback/github", env.auth_url),
user_info_url: "https://api.github.com/user".to_string(),
scopes: vec!["user:email".to_string()],
jwt_secret: env.jwt_secret,
}),
_ => Err(anyhow::anyhow!(format!(
"Invalid OAuth provider: {}",
Expand Down Expand Up @@ -140,15 +135,4 @@ impl OAuth for OAuthConfig {
}
}
}

async fn generate_jwt(&self, user: OAuthUser) -> Result<MetadataValue<Ascii>> {
let token = jsonwebtoken::encode(
&jsonwebtoken::Header::default(),
&user,
&jsonwebtoken::EncodingKey::from_secret(self.jwt_secret.as_bytes()),
)?;
let token = format!("bearer {}", token);
let token: MetadataValue<Ascii> = token.parse()?;
Ok(token)
}
}

0 comments on commit 200cfaa

Please sign in to comment.