diff --git a/src/app/tabs/settings.component.ts b/src/app/tabs/settings.component.ts index 548d1308..eb1e9001 100644 --- a/src/app/tabs/settings.component.ts +++ b/src/app/tabs/settings.component.ts @@ -21,6 +21,8 @@ import { LdapConfiguration } from '../../models/ldapConfiguration'; import { OktaConfiguration } from '../../models/oktaConfiguration'; import { SyncConfiguration } from '../../models/syncConfiguration'; +import { ConnectorUtils } from '../../utils'; + @Component({ selector: 'app-settings', templateUrl: 'settings.component.html', @@ -76,32 +78,7 @@ export class SettingsComponent implements OnInit, OnDestroy { } async submit() { - if (this.ldap.ad) { - this.sync.creationDateAttribute = 'whenCreated'; - this.sync.revisionDateAttribute = 'whenChanged'; - this.sync.emailPrefixAttribute = 'sAMAccountName'; - this.sync.memberAttribute = 'member'; - this.sync.userObjectClass = 'person'; - this.sync.groupObjectClass = 'group'; - this.sync.userEmailAttribute = 'mail'; - this.sync.groupNameAttribute = 'name'; - - if (this.sync.groupPath == null) { - this.sync.groupPath = 'CN=Users'; - } - if (this.sync.userPath == null) { - this.sync.userPath = 'CN=Users'; - } - } - - if (this.sync.interval != null) { - if (this.sync.interval <= 0) { - this.sync.interval = null; - } else if (this.sync.interval < 5) { - this.sync.interval = 5; - } - } - + ConnectorUtils.adjustConfigForSave(this.ldap, this.sync); await this.configurationService.saveOrganizationId(this.organizationId); await this.configurationService.saveDirectoryType(this.directory); await this.configurationService.saveDirectory(DirectoryType.Ldap, this.ldap); diff --git a/src/commands/config.command.ts b/src/commands/config.command.ts index 48980a21..acaf0c0b 100644 --- a/src/commands/config.command.ts +++ b/src/commands/config.command.ts @@ -3,22 +3,60 @@ import * as program from 'commander'; import { EnvironmentService } from 'jslib/abstractions/environment.service'; import { I18nService } from 'jslib/abstractions/i18n.service'; +import { ConfigurationService } from '../services/configuration.service'; + +import { DirectoryType } from '../enums/directoryType'; + import { Response } from 'jslib/cli/models/response'; import { MessageResponse } from 'jslib/cli/models/response/messageResponse'; +import { AzureConfiguration } from '../models/azureConfiguration'; +import { GSuiteConfiguration } from '../models/gsuiteConfiguration'; +import { LdapConfiguration } from '../models/ldapConfiguration'; +import { OktaConfiguration } from '../models/oktaConfiguration'; +import { SyncConfiguration } from '../models/syncConfiguration'; + +import { ConnectorUtils } from '../utils'; + export class ConfigCommand { - constructor(private environmentService: EnvironmentService, private i18nService: I18nService) { } + private directory: DirectoryType; + private ldap = new LdapConfiguration(); + private gsuite = new GSuiteConfiguration(); + private azure = new AzureConfiguration(); + private okta = new OktaConfiguration(); + private sync = new SyncConfiguration(); + + constructor(private environmentService: EnvironmentService, private i18nService: I18nService, + private configurationService: ConfigurationService) { } async run(setting: string, value: string, cmd: program.Command): Promise { setting = setting.toLowerCase(); - switch (setting) { - case 'server': - await this.setServer(value); - break; - default: - return Response.badRequest('Unknown setting.'); + try { + switch (setting) { + case 'server': + await this.setServer(value); + break; + case 'directory': + await this.setDirectory(value); + break; + case 'ldap.password': + await this.setLdapPassword(value); + break; + case 'gsuite.key': + await this.setGSuiteKey(value); + break; + case 'azure.key': + await this.setAzureKey(value); + break; + case 'okta.token': + await this.setOktaToken(value); + break; + default: + return Response.badRequest('Unknown setting.'); + } + } catch (e) { + return Response.error(e); } - const res = new MessageResponse(this.i18nService.t('savedSetting', setting), null); return Response.success(res); } @@ -29,4 +67,61 @@ export class ConfigCommand { base: url, }); } + + private async setDirectory(type: string) { + const dir = parseInt(type, null); + if (dir < DirectoryType.Ldap || dir > DirectoryType.Okta) { + throw new Error('Invalid directory type value.'); + } + await this.loadConfig(); + this.directory = dir; + await this.saveConfig(); + } + + private async setLdapPassword(password: string) { + await this.loadConfig(); + this.ldap.password = password; + await this.saveConfig(); + } + + private async setGSuiteKey(key: string) { + await this.loadConfig(); + this.gsuite.privateKey = key; + await this.saveConfig(); + } + + private async setAzureKey(key: string) { + await this.loadConfig(); + this.azure.key = key; + await this.saveConfig(); + } + + private async setOktaToken(token: string) { + await this.loadConfig(); + this.okta.token = token; + await this.saveConfig(); + } + + private async loadConfig() { + this.directory = await this.configurationService.getDirectoryType(); + this.ldap = (await this.configurationService.getDirectory(DirectoryType.Ldap)) || + this.ldap; + this.gsuite = (await this.configurationService.getDirectory(DirectoryType.GSuite)) || + this.gsuite; + this.azure = (await this.configurationService.getDirectory( + DirectoryType.AzureActiveDirectory)) || this.azure; + this.okta = (await this.configurationService.getDirectory( + DirectoryType.Okta)) || this.okta; + this.sync = (await this.configurationService.getSync()) || this.sync; + } + + private async saveConfig() { + ConnectorUtils.adjustConfigForSave(this.ldap, this.sync); + await this.configurationService.saveDirectoryType(this.directory); + await this.configurationService.saveDirectory(DirectoryType.Ldap, this.ldap); + await this.configurationService.saveDirectory(DirectoryType.GSuite, this.gsuite); + await this.configurationService.saveDirectory(DirectoryType.AzureActiveDirectory, this.azure); + await this.configurationService.saveDirectory(DirectoryType.Okta, this.okta); + await this.configurationService.saveSync(this.sync); + } } diff --git a/src/program.ts b/src/program.ts index ff03717e..9869d672 100644 --- a/src/program.ts +++ b/src/program.ts @@ -150,15 +150,26 @@ export class Program extends BaseProgram { writeLn('\n Settings:'); writeLn(''); writeLn(' server - On-premise hosted installation URL.'); + writeLn(' directory - The type of directory to use.'); + writeLn(' ldap.password - The password for connection to this LDAP server.'); + writeLn(' azure.key - The Azure AD secret key.'); + writeLn(' gsuite.key - The G Suite private key.'); + writeLn(' okta.token - The Okta token.'); writeLn(''); writeLn(' Examples:'); writeLn(''); writeLn(' bwdc config server https://bw.company.com'); writeLn(' bwdc config server bitwarden.com'); + writeLn(' bwdc config directory 1'); + writeLn(' bwdc config ldap.password '); + writeLn(' bwdc config azure.key '); + writeLn(' bwdc config gsuite.key '); + writeLn(' bwdc config okta.token '); writeLn('', true); }) .action(async (setting, value, cmd) => { - const command = new ConfigCommand(this.main.environmentService, this.main.i18nService); + const command = new ConfigCommand(this.main.environmentService, this.main.i18nService, + this.main.configurationService); const response = await command.run(setting, value, cmd); this.processResponse(response); }); diff --git a/src/utils.ts b/src/utils.ts index 2fddf12c..56fd90d2 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -3,7 +3,9 @@ import { I18nService } from 'jslib/abstractions/i18n.service'; import { SyncService } from './services/sync.service'; import { Entry } from './models/entry'; +import { LdapConfiguration } from './models/ldapConfiguration'; import { SimResult } from './models/simResult'; +import { SyncConfiguration } from './models/syncConfiguration'; import { UserEntry } from './models/userEntry'; export class ConnectorUtils { @@ -61,6 +63,34 @@ export class ConnectorUtils { }); } + static adjustConfigForSave(ldap: LdapConfiguration, sync: SyncConfiguration) { + if (ldap.ad) { + sync.creationDateAttribute = 'whenCreated'; + sync.revisionDateAttribute = 'whenChanged'; + sync.emailPrefixAttribute = 'sAMAccountName'; + sync.memberAttribute = 'member'; + sync.userObjectClass = 'person'; + sync.groupObjectClass = 'group'; + sync.userEmailAttribute = 'mail'; + sync.groupNameAttribute = 'name'; + + if (sync.groupPath == null) { + sync.groupPath = 'CN=Users'; + } + if (sync.userPath == null) { + sync.userPath = 'CN=Users'; + } + } + + if (sync.interval != null) { + if (sync.interval <= 0) { + sync.interval = null; + } else if (sync.interval < 5) { + sync.interval = 5; + } + } + } + private static sortEntries(arr: Entry[], i18nService: I18nService) { arr.sort((a, b) => { return i18nService.collator ? i18nService.collator.compare(a.displayName, b.displayName) :