Skip to content

Commit

Permalink
feat: rename auth0 to cognito
Browse files Browse the repository at this point in the history
  • Loading branch information
joel@joellee.org committed Jan 8, 2024
1 parent cf182e2 commit 214b30a
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
use crate::fdw::auth0_fdw::auth0_client::rows_iterator::RowsIterator;
use crate::fdw::auth0_fdw::auth0_client::Auth0Client;
use crate::fdw::auth0_fdw::auth0_client::CognitoClient;

use crate::stats;
use pgrx::pg_sys;
use std::collections::HashMap;
use supabase_wrappers::prelude::*;

use crate::fdw::auth0_fdw::auth0_client::Auth0ClientError;
use crate::fdw::cognito_fdw::cognito_client::CognitoClientError;
use pgrx::pg_sys::panic::ErrorReport;
use pgrx::PgSqlErrorCode;
use thiserror::Error;

#[wrappers_fdw(
version = "0.1.1",
author = "Joel",
website = "https://github.com/supabase/wrappers/tree/main/wrappers/src/fdw/auth0_fdw",
error_type = "Auth0FdwError"
website = "https://github.com/supabase/wrappers/tree/main/wrappers/src/fdw/cognito_fdw",
error_type = "CognitoFdwError"
)]
pub(crate) struct Auth0Fdw {
pub(crate) struct CognitoFdw {
// row counter
url: String,
api_key: String,
rows_iterator: Option<RowsIterator>,
}

#[derive(Error, Debug)]
enum Auth0FdwError {
enum CognitoFdwError {
#[error("{0}")]
Auth0ClientError(#[from] Auth0ClientError),
CognitoClientError(#[from] CognitoClientError),

#[error("{0}")]
CreateRuntimeError(#[from] CreateRuntimeError),
Expand Down Expand Up @@ -59,27 +59,27 @@ enum Auth0FdwError {
SetOneOfApiKeyAndApiKeyIdSet,
}

impl From<Auth0FdwError> for ErrorReport {
fn from(value: Auth0FdwError) -> Self {
impl From<CognitoFdwError> for ErrorReport {
fn from(value: CognitoFdwError) -> Self {
match value {
Auth0FdwError::CreateRuntimeError(e) => e.into(),
Auth0FdwError::OptionsError(e) => e.into(),
Auth0FdwError::Auth0ClientError(e) => e.into(),
Auth0FdwError::SecretNotFound(_) => {
CognitoFdwError::CreateRuntimeError(e) => e.into(),
CognitoFdwError::OptionsError(e) => e.into(),
CognitoFdwError::CognitoClientError(e) => e.into(),
CognitoFdwError::SecretNotFound(_) => {
ErrorReport::new(PgSqlErrorCode::ERRCODE_FDW_ERROR, format!("{value}"), "")
}
_ => ErrorReport::new(PgSqlErrorCode::ERRCODE_FDW_ERROR, format!("{value}"), ""),
}
}
}

type Auth0FdwResult<T> = Result<T, Auth0FdwError>;
type CognitoFdwResult<T> = Result<T, CognitoFdwError>;

impl Auth0Fdw {
const FDW_NAME: &'static str = "Auth0Fdw";
impl CognitoFdw {
const FDW_NAME: &'static str = "CognitoFdw";
}

impl ForeignDataWrapper<Auth0FdwError> for Auth0Fdw {
impl ForeignDataWrapper<CognitoFdwError> for CognitoFdw {
// 'options' is the key-value pairs defined in `CREATE SERVER` SQL, for example,
//
// create server my_auth0_server
Expand All @@ -94,15 +94,15 @@ impl ForeignDataWrapper<Auth0FdwError> for Auth0Fdw {
// info or API url in an variable, but don't do any heavy works like making a
// database connection or API call.

fn new(options: &HashMap<String, String>) -> Result<Self, Auth0FdwError> {
fn new(options: &HashMap<String, String>) -> Result<Self, CognitoFdwError> {
let url = require_option("url", options)?.to_string();
let api_key = if let Some(api_key) = options.get("api_key") {
api_key.clone()
} else {
let api_key_id = options
.get("api_key_id")
.expect("`api_key_id` must be set if `api_key` is not");
get_vault_secret(api_key_id).ok_or(Auth0FdwError::SecretNotFound(api_key_id.clone()))?
get_vault_secret(api_key_id).ok_or(CognitoFdwError::SecretNotFound(api_key_id.clone()))?
};

stats::inc_stats(Self::FDW_NAME, stats::Metric::CreateTimes, 1);
Expand All @@ -120,14 +120,14 @@ impl ForeignDataWrapper<Auth0FdwError> for Auth0Fdw {
_sorts: &[Sort],
_limit: &Option<Limit>,
_options: &HashMap<String, String>,
) -> Auth0FdwResult<()> {
let auth0_client = Auth0Client::new(&self.url, &self.api_key)?;
) -> CognitoFdwResult<()> {
let auth0_client = CognitoClient::new(&self.url, &self.api_key)?;
self.rows_iterator = Some(RowsIterator::new(columns.to_vec(), 1000, auth0_client));

Ok(())
}

fn iter_scan(&mut self, row: &mut Row) -> Result<Option<()>, Auth0FdwError> {
fn iter_scan(&mut self, row: &mut Row) -> Result<Option<()>, CognitoFdwError> {
let rows_iterator = self
.rows_iterator
.as_mut()
Expand All @@ -141,22 +141,22 @@ impl ForeignDataWrapper<Auth0FdwError> for Auth0Fdw {
}
}

fn end_scan(&mut self) -> Auth0FdwResult<()> {
fn end_scan(&mut self) -> CognitoFdwResult<()> {
Ok(())
}

fn validator(options: Vec<Option<String>>, catalog: Option<pg_sys::Oid>) -> Auth0FdwResult<()> {
fn validator(options: Vec<Option<String>>, catalog: Option<pg_sys::Oid>) -> CognitoFdwResult<()> {
if let Some(oid) = catalog {
if oid == FOREIGN_SERVER_RELATION_ID {
let api_key_exists = check_options_contain(&options, "api_key").is_ok();
let api_key_id_exists = check_options_contain(&options, "api_key_id").is_ok();
let url_exists = check_options_contain(&options, "url").is_ok();
if (api_key_exists && api_key_id_exists) || (!api_key_exists && !api_key_id_exists)
{
return Err(Auth0FdwError::SetOneOfApiKeyAndApiKeyIdSet);
return Err(CognitoFdwError::SetOneOfApiKeyAndApiKeyIdSet);
}
if !url_exists {
return Err(Auth0FdwError::URLOptionMissing);
return Err(CognitoFdwError::URLOptionMissing);
}
} else if oid == FOREIGN_TABLE_RELATION_ID {
check_options_contain(&options, "object")?;
Expand Down
4 changes: 2 additions & 2 deletions wrappers/src/fdw/cognito_fdw/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(clippy::module_inception)]
mod auth0_client;
mod auth0_fdw;
mod cognito_client;
mod cognito_fdw;
mod tests;
14 changes: 7 additions & 7 deletions wrappers/src/fdw/cognito_fdw/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ mod tests {
fn auth0_smoketest() {
Spi::connect(|mut c| {
c.update(
r#"create foreign data wrapper auth0_wrapper
handler auth0_fdw_handler validator auth0_fdw_validator"#,
r#"create foreign data wrapper cognito_wrapper
handler cognito_fdw_handler validator cognito_fdw_validator"#,
None,
None,
)
.expect("Failed to create foreign data wrapper");
c.update(
r#"CREATE SERVER auth0_server
FOREIGN DATA WRAPPER auth0_wrapper
r#"CREATE SERVER cognito_server
FOREIGN DATA WRAPPER cognito_wrapper
OPTIONS (
url 'http://localhost:3796',
api_key 'apiKey'
Expand All @@ -27,13 +27,13 @@ mod tests {
.unwrap();
c.update(
r#"
CREATE FOREIGN TABLE auth0_view (
CREATE FOREIGN TABLE cognito_view (
created_at text,
email text,
email_verified bool,
identities jsonb
)
SERVER auth0_server
SERVER cognito_server
options (
object 'users'
)
Expand All @@ -47,7 +47,7 @@ mod tests {
*/
let results = c
.select(
"SELECT * FROM auth0_view WHERE email = 'example@gmail.com'",
"SELECT * FROM cognito_view WHERE email = 'example@gmail.com'",
None,
None,
)
Expand Down

0 comments on commit 214b30a

Please sign in to comment.