import getPool from "@cocalc/database/pool" ; import { parseDomain , ParseResultType } from "parse-domain" ; export default async function isDomainExclusiveSSO ( email_address ) { if ( ! email_address ) { return ; } const raw_domain = email_address . split ( "@" ) [ 1 ] ?. trim ( ) . toLowerCase ( ) ; if ( ! raw_domain ) { return ; } const exclusiveDomains = await getExclusiveDomains ( ) ; if ( exclusiveDomains . length == 0 ) { return ; } const parsed = parseDomain ( raw_domain ) ; if ( parsed . type != ParseResultType . Listed ) { return ; } const { domain , topLevelDomains } = parsed ; const canonical = [ domain ?? "" , ... topLevelDomains ] . join ( "." ) ; if ( exclusiveDomains . includes ( canonical ) ) { return canonical ; } } async function getExclusiveDomains ( ) { const pool = getPool ( "minutes" ) ; const { rows } = await pool . query ( "SELECT conf#>'{exclusive_domains}' as exclusive_domains FROM passport_settings" ) ; const v = [ ] ; for ( const row of rows ) { const { exclusive_domains } = row ; if ( exclusive_domains ) { v . push ( ... exclusive_domains ) ; } } return v ; }