CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

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