Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/lib/sso/sso.ts
5837 views
1
/*
2
* This file is part of CoCalc: Copyright © 2022-2026 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { sortBy } from "lodash";
7
8
import getPool from "@cocalc/database/pool";
9
import { COLORS } from "@cocalc/database/settings/get-sso-strategies";
10
import { ssoDispayedName } from "@cocalc/util/auth";
11
import { ssoNormalizeExclusiveDomains } from "@cocalc/util/sso-normalize-domains";
12
import { SSO } from "./types";
13
14
const SQL_SELECT = `SELECT strategy, info,
15
COALESCE(info -> 'icon', conf -> 'icon') as icon,
16
COALESCE(info -> 'display', conf -> 'display') as display,
17
COALESCE(info -> 'exclusive_domains', conf -> 'exclusive_domains') as exclusive_domains`;
18
19
interface Row {
20
strategy: string;
21
display?: string;
22
exclusive_domains?: string[];
23
info?: any;
24
icon?: string;
25
}
26
27
function parseRow(row: Row): SSO {
28
const { strategy, display, info, icon } = row;
29
ssoNormalizeExclusiveDomains(row);
30
const domains = row.exclusive_domains ?? [];
31
return {
32
id: strategy,
33
display: ssoDispayedName({ display, name: strategy }),
34
domains,
35
descr: info?.description ?? null,
36
backgroundColor: COLORS[strategy] ?? "",
37
icon,
38
};
39
}
40
41
export async function getSSO(): Promise<SSO[]> {
42
const pool = getPool("long");
43
const { rows } = await pool.query(`
44
${SQL_SELECT}
45
FROM passport_settings
46
WHERE coalesce(info -> 'public', conf -> 'public', 'true'::JSONB)::BOOL IS FALSE`);
47
const data = rows.map((row) => parseRow(row));
48
return sortBy(data, (sso) => sso.display);
49
}
50
51
export async function getOneSSO(id: string): Promise<SSO | undefined> {
52
const pool = getPool("long");
53
const { rows } = await pool.query(
54
`${SQL_SELECT}
55
FROM passport_settings
56
WHERE strategy=$1`,
57
[id],
58
);
59
if (rows.length === 0) return;
60
return parseRow(rows[0]);
61
}
62
63