Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/scripts/auth/gen-sso.py
Views: 687
#!/usr/bin/env python312# This is 100% for development only. None of these configurations are real!3# This is a quick and simple script to generate PostgreSQL commands to re-create the passport_strategy table.4# As a first command, it deletes everything you have there -- hence this is 100% only for development!5# Usage:6# python3 cocalc-db-sso.py [dump]7# which issues commands to psql directly, or add "dump" to see what it would do.8#9# The conf/info fields are described in src/packages/server/auth/sso/types.ts1011from typing_extensions import TypedDict12from typing import Dict1314Entry = TypedDict("Entry", {"strategy": str, "conf": Dict, "info": Dict})1516# the first 4 ones have no functional purpose – good for checking the UI, etc.1718food: Entry = {19"strategy": "food",20"conf": {21"icon": "https://img.icons8.com/glyph-neue/344/food-and-wine.png",22"type": "oauth2next",23"scope": ["email", "profile"],24"display": "Food University",25"clientID": "CoCalc_Client",26"tokenURL": "https://localhost/oauth2/wowtech/access_token",27"login_info": {28"emails": "emails[0].value"29},30"userinfoURL": "https://localhost/oauth2/userinfo",31"clientSecret": "sEcRet1234",32"authorizationURL": "https://localhost/oauth2/authorize"33},34"info": {35"public": False,36"description":37"This is the SSO mechanism for anyone associated with Food University",38"exclusive_domains": ["food.edu"]39}40}4142flight: Entry = {43"strategy": "flight",44"conf": {45"type": "oauth2next",46"scope": ["email", "profile"],47"clientID": "CoCalc_Client",48"tokenURL": "https://localhost/oauth2/wowtech/access_token",49"login_info": {50"emails": "emails[0].value"51},52"userinfoURL": "https://localhost/oauth2/userinfo",53"clientSecret": "sEcRet1234",54"authorizationURL": "https://localhost/oauth2/authorize"55},56"info": {57"icon":58"https://img.icons8.com/external-kiranshastry-solid-kiranshastry/344/external-flight-interface-kiranshastry-solid-kiranshastry.png",59"public": False,60"display": "Flight Research",61"description":62"This is to sign up with CoCalc as a student of **Flight Research International, Inc.**\n\nMore information:\n\n- [airplane.edu](http://airplane.edu/)\n\n- [yet another link](http://nowhere.com)",63"exclusive_domains": ["airplane.edu", "aircraft.com"]64}65}6667minimal: Entry = {68"strategy": "minimal",69"conf": {70"type": "oauth2next",71"display": "Minimal",72"clientID": "CoCalc_Client",73"tokenURL": "https://localhost/oauth2/wowtech/access_token",74"login_info": {75"emails": "emails[0].value"76},77"userinfoURL": "https://localhost/oauth2/userinfo",78"clientSecret": "sEcRet1234",79"authorizationURL": "https://localhost/oauth2/authorize"80},81"info": {82"public": False,83"do_not_hide": True,84"exclusive_domains": ["minimal.edu"]85}86}8788abacus2: Entry = {89"strategy": "abacus2",90"conf": {91"type": "oauth2next",92"scope": ["email", "profile"],93"clientID": "CoCalc_Client",94"tokenURL": "https://localhost/oauth2/wowtech/access_token",95"login_info": {96"emails": "emails[0].value"97},98"userinfoURL": "https://localhost/oauth2/userinfo",99"clientSecret": "sEcRet1234",100"authorizationURL": "https://localhost/oauth2/authorize"101},102"info": {103"icon":104"https://img.icons8.com/external-smashingstocks-outline-color-smashing-stocks/344/external-abacus-online-education-smashingstocks-outline-color-smashing-stocks.png",105"public":106False,107"display":108"Abacus 2",109"description":110"This is the SSO mechanism for anyone associated with Abacus Inc",111"exclusive_domains":112["abacus.edu", "dadacus.edu", "nadacus.edu", "blablacus.edu"]113}114}115116oidc1: Entry = {117"strategy": "oidc1",118"conf": {119"type": "oidc",120"issuer": "http://localhost:5300",121"authorizationURL": "http://localhost:5300/auth",122"tokenURL": "http://localhost:5300/token",123"userInfoURL": "http://localhost:5300/me",124"clientID": "cocalc",125"clientSecret": "s3cr3t",126"callbackURL": "http://localhost:5000/auth/oidc1/return",127},128"info": {129"display": "OIDC Test",130"description":131"This is the SSO mechanism for anyone associated with OIDC Test",132"public": False,133}134}135136strats = [food, flight, minimal, abacus2, oidc1]137138# read content of file saml-idp-local.pem139# curdir is the directory of this file140from os.path import join, dirname, realpath, exists, abspath141142curdir = dirname(realpath(__file__))143saml20fn = join(curdir, "saml-idp-local.pem")144145if exists(saml20fn):146print("Generating SAML 2.0 SSO strategy")147saml20cert: str = open(saml20fn, "r").read()148saml20: Entry = {149"strategy": "saml20",150"conf": {151"type": "saml",152"name": "saml20",153"entryPoint": "http://localhost:7000/saml/sso",154"path": "/auth/saml20/return",155#"audience": False, # "https://localhost:5000/", # false is set as default156"login_info": {157"first_name": "firstName",158"last_name": "lastName",159"full_name": "displayName",160"emails": "email",161"id": "email",162},163"issuer": "https://cocalc.com",164"cert": saml20cert165},166"info": {167"icon":168"https://b.thumbs.redditmedia.com/EQ1HS4MFeamF4Yw6ufKYWkSkmcsikv4VvQ4dYzfsmGw.png",169"public": False,170"display": "Saml20",171"description": "Testing my SAML 2.0 IdP",172"exclusive_domains": ["example.com"],173"update_on_login": True,174"cookie_ttl_s": 24 * 60 * 60, # 24 hours175}176}177strats.append(saml20)178else:179print(180f"WARNING: no SAML 2.0 generated. Setup saml-idp and copy the pem file certificate to exactly {saml20fn}"181)182183# Setting up a test OAuth2 server is hard, or I don't know how to do it.184# In any case, this test was using gerges-beshay/oauth2orize-examples185# with small modifications: db/users has a given_name and family_name,186# and routes/user returns them in the json response.187# $ PORT=5555 node app.js188oauth2server = 'http://localhost:5555'189oauth2: Entry = {190"strategy": "myOauth2",191"conf": {192"type": "oauth2",193"scope": ["email", "profile"],194"authorizationURL": f'{oauth2server}/dialog/authorize',195"tokenURL": f'{oauth2server}/oauth/token',196"userinfoURL": f'{oauth2server}/api/userinfo',197"clientID": 'abc123',198"clientSecret": 'ssh-secret',199#"login_info": {200# "id": "_raw.user_id",201#}202},203"info": {204"public": False,205"display": "My OAuth2",206"description": "My OAuth2",207"update_on_login": False,208}209}210strats.append(oauth2)211212# fake public213214twitter: Entry = {215"strategy": "twitter",216"conf": {217"clientID": "123",218"clientSecret": "123123"219},220}221strats.append(twitter)222223github: Entry = {224"strategy": "github",225"conf": {226"clientID": "123",227"clientSecret": "123123"228},229}230strats.append(github)231232##############233234sql_commands = []235236from json import dumps237238sql_commands.append("DELETE FROM passport_settings;")239240insertPattern = "INSERT INTO passport_settings (strategy, conf, info) VALUES ('{strategy}', '{conf}'::JSONB, '{info}'::JSONB);"241242for strat in strats:243print("Inserting", strat["strategy"])244sql_commands.append(245insertPattern.format(strategy=strat["strategy"],246conf=dumps(strat["conf"]),247info=dumps(strat.get("info"))))248249import sys250if len(sys.argv) > 1 and sys.argv[1] == 'dump':251print()252print('commands:')253print()254for sql in sql_commands:255print(sql)256exit()257258from subprocess import run259260# this needs all env variables to set properly, e.g. source an "postgres-env" file first261run(["psql"], check=True, input="\n".join(sql_commands).encode('utf8'))262263264