16 changed files with 228 additions and 81 deletions
			
			
		| @ -1 +0,0 @@ | |||
| DROP TABLE sso_nonce; | |||
| @ -1,13 +0,0 @@ | |||
| ALTER TABLE organizations ADD COLUMN identifier               TEXT; | |||
| ALTER TABLE organizations ADD COLUMN use_sso                  BOOLEAN NOT NULL; | |||
| ALTER TABLE organizations ADD COLUMN callback_path            TEXT NOT NULL; | |||
| ALTER TABLE organizations ADD COLUMN signed_out_callback_path TEXT NOT NULL; | |||
| ALTER TABLE organizations ADD COLUMN authority                TEXT; | |||
| ALTER TABLE organizations ADD COLUMN client_id                TEXT; | |||
| ALTER TABLE organizations ADD COLUMN client_secret            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 | |||
| ); | |||
| @ -0,0 +1,2 @@ | |||
| DROP TABLE sso_nonce; | |||
| DROP TABLE sso_config; | |||
| @ -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 | |||
| ); | |||
| @ -1 +1,2 @@ | |||
| DROP TABLE sso_nonce; | |||
| DROP TABLE sso_config; | |||
|  | |||
| @ -1,13 +1,18 @@ | |||
| ALTER TABLE organizations ADD COLUMN identifier               TEXT; | |||
| ALTER TABLE organizations ADD COLUMN use_sso                  BOOLEAN NOT NULL; | |||
| ALTER TABLE organizations ADD COLUMN callback_path            TEXT NOT NULL; | |||
| ALTER TABLE organizations ADD COLUMN signed_out_callback_path TEXT NOT NULL; | |||
| ALTER TABLE organizations ADD COLUMN authority                TEXT; | |||
| ALTER TABLE organizations ADD COLUMN client_id                TEXT; | |||
| ALTER TABLE organizations ADD COLUMN client_secret            TEXT; | |||
| ALTER TABLE organizations ADD COLUMN identifier TEXT; | |||
| 
 | |||
| CREATE TABLE sso_nonce ( | |||
|   uuid CHAR(36) NOT NULL PRIMARY KEY, | |||
|   uuid     CHAR(36) NOT NULL PRIMARY KEY, | |||
|   org_uuid CHAR(36) NOT NULL REFERENCES organizations (uuid), | |||
|   nonce CHAR(36) NOT NULL | |||
|   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 | |||
| ); | |||
|  | |||
| @ -1 +1,2 @@ | |||
| DROP TABLE sso_nonce; | |||
| DROP TABLE sso_config; | |||
|  | |||
| @ -1,13 +1,18 @@ | |||
| ALTER TABLE organizations ADD COLUMN identifier               TEXT; | |||
| ALTER TABLE organizations ADD COLUMN use_sso                  BOOLEAN NOT NULL; | |||
| ALTER TABLE organizations ADD COLUMN callback_path            TEXT NOT NULL; | |||
| ALTER TABLE organizations ADD COLUMN signed_out_callback_path TEXT NOT NULL; | |||
| ALTER TABLE organizations ADD COLUMN authority                TEXT; | |||
| ALTER TABLE organizations ADD COLUMN client_id                TEXT; | |||
| ALTER TABLE organizations ADD COLUMN client_secret            TEXT; | |||
| ALTER TABLE organizations ADD COLUMN identifier TEXT; | |||
| 
 | |||
| CREATE TABLE sso_nonce ( | |||
|   uuid CHAR(36) NOT NULL PRIMARY KEY, | |||
|   uuid     CHAR(36) NOT NULL PRIMARY KEY, | |||
|   org_uuid CHAR(36) NOT NULL REFERENCES organizations (uuid), | |||
|   nonce CHAR(36) NOT NULL | |||
|   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 | |||
| ); | |||
|  | |||
| @ -0,0 +1,104 @@ | |||
| use crate::api::EmptyResult; | |||
| use crate::db::DbConn; | |||
| use crate::error::MapResult; | |||
| use serde_json::Value; | |||
| 
 | |||
| use super::Organization; | |||
| 
 | |||
| db_object! { | |||
|     #[derive(Identifiable, Queryable, Insertable, Associations, AsChangeset)] | |||
|     #[table_name = "sso_config"] | |||
|     #[belongs_to(Organization, foreign_key = "org_uuid")] | |||
|     #[primary_key(uuid)] | |||
|     pub struct SsoConfig { | |||
|         pub uuid: String, | |||
|         pub org_uuid: String, | |||
|         pub use_sso: bool, | |||
|         pub callback_path: String, | |||
|         pub signed_out_callback_path: String, | |||
|         pub authority: Option<String>, | |||
|         pub client_id: Option<String>, | |||
|         pub client_secret: Option<String>, | |||
|     } | |||
| } | |||
| 
 | |||
| /// Local methods
 | |||
| impl SsoConfig { | |||
|     pub fn new(org_uuid: String) -> Self { | |||
|         Self { | |||
|             uuid: crate::util::get_uuid(), | |||
|             org_uuid, | |||
|             use_sso: false, | |||
|             callback_path: String::from("http://localhost/#/sso/"), | |||
|             signed_out_callback_path: String::from("http://localhost/#/sso/"), | |||
|             authority: None, | |||
|             client_id: None, | |||
|             client_secret: None, | |||
|         } | |||
|     } | |||
| 
 | |||
|     pub fn to_json(&self) -> Value { | |||
|         json!({ | |||
|             "Id": self.uuid, | |||
|             "UseSso": self.use_sso, | |||
|             "CallbackPath": self.callback_path, | |||
|             "SignedOutCallbackPath": self.signed_out_callback_path, | |||
|             "Authority": self.authority, | |||
|             "ClientId": self.client_id, | |||
|             "ClientSecret": self.client_secret, | |||
|         }) | |||
|     } | |||
| } | |||
| 
 | |||
| /// Database methods
 | |||
| impl SsoConfig { | |||
|     pub fn save(&self, conn: &DbConn) -> EmptyResult { | |||
|         db_run! { conn: | |||
|             sqlite, mysql { | |||
|                 match diesel::replace_into(sso_config::table) | |||
|                     .values(SsoConfigDb::to_db(self)) | |||
|                     .execute(conn) | |||
|                 { | |||
|                     Ok(_) => Ok(()), | |||
|                     // Record already exists and causes a Foreign Key Violation because replace_into() wants to delete the record first.
 | |||
|                     Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => { | |||
|                         diesel::update(sso_config::table) | |||
|                             .filter(sso_config::uuid.eq(&self.uuid)) | |||
|                             .set(SsoConfigDb::to_db(self)) | |||
|                             .execute(conn) | |||
|                             .map_res("Error adding sso config to organization") | |||
|                     } | |||
|                     Err(e) => Err(e.into()), | |||
|                 }.map_res("Error adding sso config to organization") | |||
|             } | |||
|             postgresql { | |||
|                 let value = SsoConfigDb::to_db(self); | |||
|                 diesel::insert_into(sso_config::table) | |||
|                     .values(&value) | |||
|                     .on_conflict(sso_config::uuid) | |||
|                     .do_update() | |||
|                     .set(&value) | |||
|                     .execute(conn) | |||
|                     .map_res("Error adding sso config to organization") | |||
|             } | |||
|         } | |||
|     } | |||
| 
 | |||
|     pub fn delete(self, conn: &DbConn) -> EmptyResult { | |||
|         db_run! { conn: { | |||
|             diesel::delete(sso_config::table.filter(sso_config::uuid.eq(self.uuid))) | |||
|                 .execute(conn) | |||
|                 .map_res("Error deleting SSO Config") | |||
|         }} | |||
|     } | |||
| 
 | |||
|     pub fn find_by_org(org_uuid: &str, conn: &DbConn) -> Option<Self> { | |||
|         db_run! { conn: { | |||
|             sso_config::table | |||
|                 .filter(sso_config::org_uuid.eq(org_uuid)) | |||
|                 .first::<SsoConfigDb>(conn) | |||
|                 .ok() | |||
|                 .from_db() | |||
|         }} | |||
|     } | |||
| } | |||
					Loading…
					
					
				
		Reference in new issue