Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OIDC SSO #2449

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ pico-args = "0.5.0"
paste = "1.0.9"
governor = "0.5.0"

# OIDC SSo
openidconnect = "2.3.2"


# Capture CTRL+C
ctrlc = { version = "3.2.3", features = ["termination"] }

Expand Down
2 changes: 2 additions & 0 deletions migrations/mysql/2021-09-16-133000_add_sso/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TABLE sso_nonce;
DROP TABLE sso_config;
18 changes: 18 additions & 0 deletions migrations/mysql/2021-09-16-133000_add_sso/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
ALTER TABLE organizations ADD COLUMN identifier TEXT;

CREATE TABLE sso_nonce (
uuid CHAR(36) NOT NULL PRIMARY KEY,
org_uuid CHAR(36) NOT NULL REFERENCES organizations (uuid),
nonce CHAR(36) NOT NULL
);

CREATE TABLE sso_config (
uuid CHAR(36) NOT NULL PRIMARY KEY,
org_uuid CHAR(36) NOT NULL REFERENCES organizations(uuid),
use_sso BOOLEAN NOT NULL,
callback_path TEXT NOT NULL,
signed_out_callback_path TEXT NOT NULL,
authority TEXT,
client_id TEXT,
client_secret TEXT
);
2 changes: 2 additions & 0 deletions migrations/postgresql/2021-09-16-133000_add_sso/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TABLE sso_nonce;
DROP TABLE sso_config;
18 changes: 18 additions & 0 deletions migrations/postgresql/2021-09-16-133000_add_sso/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
ALTER TABLE organizations ADD COLUMN identifier TEXT;

CREATE TABLE sso_nonce (
uuid CHAR(36) NOT NULL PRIMARY KEY,
org_uuid CHAR(36) NOT NULL REFERENCES organizations (uuid),
nonce CHAR(36) NOT NULL
);

CREATE TABLE sso_config (
uuid CHAR(36) NOT NULL PRIMARY KEY,
org_uuid CHAR(36) NOT NULL REFERENCES organizations(uuid),
use_sso BOOLEAN NOT NULL,
callback_path TEXT NOT NULL,
signed_out_callback_path TEXT NOT NULL,
authority TEXT,
client_id TEXT,
client_secret TEXT
);
2 changes: 2 additions & 0 deletions migrations/sqlite/2021-09-16-133000_add_sso/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TABLE sso_nonce;
DROP TABLE sso_config;
18 changes: 18 additions & 0 deletions migrations/sqlite/2021-09-16-133000_add_sso/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
ALTER TABLE organizations ADD COLUMN identifier TEXT;

CREATE TABLE sso_nonce (
uuid CHAR(36) NOT NULL PRIMARY KEY,
org_uuid CHAR(36) NOT NULL REFERENCES organizations (uuid),
nonce CHAR(36) NOT NULL
);

CREATE TABLE sso_config (
uuid CHAR(36) NOT NULL PRIMARY KEY,
org_uuid CHAR(36) NOT NULL REFERENCES organizations(uuid),
use_sso BOOLEAN NOT NULL,
callback_path TEXT NOT NULL,
signed_out_callback_path TEXT NOT NULL,
authority TEXT,
client_id TEXT,
client_secret TEXT
);
94 changes: 94 additions & 0 deletions src/api/core/organizations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub fn routes() -> Vec<Route> {
put_collection_users,
put_organization,
post_organization,
get_organization_sso,
put_organization_sso,
post_organization_collections,
delete_organization_collection_user,
post_organization_collection_delete_user,
Expand Down Expand Up @@ -92,6 +94,14 @@ struct OrgData {
struct OrganizationUpdateData {
BillingEmail: String,
Name: String,
Identifier: Option<String>,
}

#[derive(Deserialize, Debug)]
#[allow(non_snake_case)]
struct OrganizationSsoUpdateData {
Enabled: Option<bool>,
Data: Option<SsoOrganizationData>,
}

#[derive(Deserialize, Debug)]
Expand All @@ -100,6 +110,45 @@ struct NewCollectionData {
Name: String,
}

#[derive(Deserialize, Debug)]
#[allow(non_snake_case)]
struct SsoOrganizationData {
// authority: Option<String>,
// clientId: Option<String>,
// clientSecret: Option<String>,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented out properties. Can probably be removed, can't it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depends, if those items are sent by the web-client or other clients then having those as an Option would be a better choice.
If they are not needed at all, then removing them is the better option.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They were added in newer versions of the web-client. They are not used/needed anywhere, I assume they are for other kinds of SSO configuration. The older web-client had a lot less options and inputs than the current version.

AcrValues: Option<String>,
AdditionalEmailClaimTypes: Option<String>,
AdditionalNameClaimTypes: Option<String>,
AdditionalScopes: Option<String>,
AdditionalUserIdClaimTypes: Option<String>,
Authority: Option<String>,
ClientId: Option<String>,
ClientSecret: Option<String>,
ConfigType: Option<String>,
ExpectedReturnAcrValue: Option<String>,
GetClaimsFromUserInfoEndpoint: Option<bool>,
IdpAllowUnsolicitedAuthnResponse: Option<bool>,
IdpArtifactResolutionServiceUrl: Option<String>,
IdpBindingType: Option<u8>,
IdpDisableOutboundLogoutRequests: Option<bool>,
IdpEntityId: Option<String>,
IdpOutboundSigningAlgorithm: Option<String>,
IdpSingleLogoutServiceUrl: Option<String>,
IdpSingleSignOnServiceUrl: Option<String>,
IdpWantAuthnRequestsSigned: Option<bool>,
IdpX509PublicCert: Option<String>,
KeyConnectorUrlY: Option<String>,
KeyConnectorEnabled: Option<bool>,
MetadataAddress: Option<String>,
RedirectBehavior: Option<String>,
SpMinIncomingSigningAlgorithm: Option<String>,
SpNameIdFormat: Option<u8>,
SpOutboundSigningAlgorithm: Option<String>,
SpSigningBehavior: Option<u8>,
SpValidateCertificates: Option<bool>,
SpWantAssertionsSigned: Option<bool>,
}

#[derive(Deserialize)]
#[allow(non_snake_case)]
struct OrgKeyData {
Expand Down Expand Up @@ -134,6 +183,7 @@ async fn create_organization(headers: Headers, data: JsonUpcase<OrgData>, conn:

let org = Organization::new(data.Name, data.BillingEmail, private_key, public_key);
let mut user_org = UserOrganization::new(headers.user.uuid, org.uuid.clone());
let sso_config = SsoConfig::new(org.uuid.clone());
let collection = Collection::new(org.uuid.clone(), data.CollectionName);

user_org.akey = data.Key;
Expand All @@ -143,6 +193,7 @@ async fn create_organization(headers: Headers, data: JsonUpcase<OrgData>, conn:

org.save(&conn).await?;
user_org.save(&conn).await?;
sso_config.save(&conn).await?;
collection.save(&conn).await?;

Ok(Json(org.to_json()))
Expand Down Expand Up @@ -228,11 +279,54 @@ async fn post_organization(

org.name = data.Name;
org.billing_email = data.BillingEmail;
org.identifier = data.Identifier;

org.save(&conn).await?;
Ok(Json(org.to_json()))
}

#[get("/organizations/<org_id>/sso")]
async fn get_organization_sso(org_id: String, _headers: OwnerHeaders, conn: DbConn) -> JsonResult {
match SsoConfig::find_by_org(&org_id, &conn).await {
Some(sso_config) => {
let config_json = Json(sso_config.to_json());
Ok(config_json)
}
None => err!("Can't find organization sso config"),
}
}

#[post("/organizations/<org_id>/sso", data = "<data>")]
async fn put_organization_sso(
org_id: String,
_headers: OwnerHeaders,
data: JsonUpcase<OrganizationSsoUpdateData>,
conn: DbConn,
) -> JsonResult {
let p: OrganizationSsoUpdateData = data.into_inner().data;
let d: SsoOrganizationData = p.Data.unwrap();

let mut sso_config = match SsoConfig::find_by_org(&org_id, &conn).await {
Some(sso_config) => sso_config,
None => SsoConfig::new(org_id),
};

sso_config.use_sso = p.Enabled.unwrap_or_default();

// let sso_config_data = data.Data.unwrap();

// TODO use real values
sso_config.callback_path = "http://localhost:8000/#/sso".to_string(); //data.CallbackPath;
sso_config.signed_out_callback_path = "http://localhost:8000/#/sso".to_string(); //data2.Data.unwrap().call

sso_config.authority = d.Authority;
sso_config.client_id = d.ClientId;
sso_config.client_secret = d.ClientSecret;

sso_config.save(&conn).await?;
Ok(Json(sso_config.to_json()))
}

// GET /api/collections?writeOnly=false
#[get("/collections")]
async fn get_user_collections(headers: Headers, conn: DbConn) -> Json<Value> {
Expand Down
Loading