-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #318 from vscheuber/main
- Loading branch information
Showing
14 changed files
with
349 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { frodo } from '@rockcarver/frodo-lib'; | ||
import { Option } from 'commander'; | ||
|
||
import { describeAuthenticationSettings } from '../../ops/AuthenticationSettingsOps'; | ||
import { verboseMessage } from '../../utils/Console'; | ||
import { FrodoCommand } from '../FrodoCommand'; | ||
|
||
const { getTokens } = frodo.login; | ||
|
||
const program = new FrodoCommand('frodo authn describe'); | ||
|
||
program | ||
.description('Describe authentication settings.') | ||
.addOption(new Option('--json', 'Output in JSON format.')) | ||
.action( | ||
// implement command logic inside action handler | ||
async (host, realm, user, password, options, command) => { | ||
command.handleDefaultArgsAndOpts( | ||
host, | ||
realm, | ||
user, | ||
password, | ||
options, | ||
command | ||
); | ||
if (await getTokens()) { | ||
verboseMessage(`Describing authentication settings...`); | ||
const outcome = await describeAuthenticationSettings(options.json); | ||
if (!outcome) process.exitCode = 1; | ||
} | ||
// unrecognized combination of options or no options | ||
else { | ||
verboseMessage('Unrecognized combination of options or no options...'); | ||
program.help(); | ||
process.exitCode = 1; | ||
} | ||
} | ||
// end command logic inside action handler | ||
); | ||
|
||
program.parse(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { frodo } from '@rockcarver/frodo-lib'; | ||
import { Option } from 'commander'; | ||
|
||
import { exportAuthenticationSettingsToFile } from '../../ops/AuthenticationSettingsOps'; | ||
import { verboseMessage } from '../../utils/Console'; | ||
import { FrodoCommand } from '../FrodoCommand'; | ||
|
||
const { getTokens } = frodo.login; | ||
|
||
const program = new FrodoCommand('frodo authn export'); | ||
|
||
program | ||
.description('Export authentication settings.') | ||
.addOption(new Option('-f, --file <file>', 'Name of the export file.')) | ||
.action( | ||
// implement command logic inside action handler | ||
async (host, realm, user, password, options, command) => { | ||
command.handleDefaultArgsAndOpts( | ||
host, | ||
realm, | ||
user, | ||
password, | ||
options, | ||
command | ||
); | ||
if (await getTokens()) { | ||
verboseMessage('Exporting authentication settings to file...'); | ||
const outcome = exportAuthenticationSettingsToFile(options.file); | ||
if (!outcome) process.exitCode = 1; | ||
} else { | ||
process.exitCode = 1; | ||
} | ||
} | ||
// end command logic inside action handler | ||
); | ||
|
||
program.parse(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { frodo } from '@rockcarver/frodo-lib'; | ||
import { Option } from 'commander'; | ||
|
||
import { importAuthenticationSettingsFromFile } from '../../ops/AuthenticationSettingsOps'; | ||
import { verboseMessage } from '../../utils/Console'; | ||
import { FrodoCommand } from '../FrodoCommand'; | ||
|
||
const { getTokens } = frodo.login; | ||
|
||
const program = new FrodoCommand('frodo authn import'); | ||
|
||
program | ||
.description('Import authentication settings.') | ||
.addOption(new Option('-f, --file <file>', 'Name of the file to import.')) | ||
.action( | ||
// implement command logic inside action handler | ||
async (host, realm, user, password, options, command) => { | ||
command.handleDefaultArgsAndOpts( | ||
host, | ||
realm, | ||
user, | ||
password, | ||
options, | ||
command | ||
); | ||
if (await getTokens()) { | ||
verboseMessage('Importing authentication settings from file...'); | ||
const outcome = importAuthenticationSettingsFromFile(options.file); | ||
if (!outcome) process.exitCode = 1; | ||
} else { | ||
process.exitCode = 1; | ||
} | ||
} | ||
// end command logic inside action handler | ||
); | ||
|
||
program.parse(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import path from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
|
||
import { FrodoStubCommand } from '../FrodoCommand'; | ||
|
||
const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
|
||
export default function setup() { | ||
const program = new FrodoStubCommand('authn') | ||
.description('Manage authentication settings.') | ||
.executableDir(__dirname); | ||
|
||
program.command('describe', 'Describe authentication settings.'); | ||
|
||
program.command('export', 'Export authentication settings.'); | ||
|
||
program.command('import', 'Import authentication settings.'); | ||
|
||
return program; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import { frodo, state } from '@rockcarver/frodo-lib'; | ||
import fs from 'fs'; | ||
|
||
import { | ||
createObjectTable, | ||
debugMessage, | ||
failSpinner, | ||
printMessage, | ||
showSpinner, | ||
succeedSpinner, | ||
} from '../utils/Console'; | ||
import { saveJsonToFile } from '../utils/ExportImportUtils'; | ||
|
||
const { getTypedFilename, getFilePath } = frodo.utils; | ||
const { | ||
exportAuthenticationSettings: _exportAuthenticationSettings, | ||
importAuthenticationSettings: _importAuthenticationSettings, | ||
readAuthenticationSettings: _readAuthenticationSettings, | ||
} = frodo.authn.settings; | ||
|
||
/** | ||
* Export authentication settings to file | ||
* @param {string} file file name | ||
* @returns {Promise<boolean>} true if successful, false otherwise | ||
*/ | ||
export async function exportAuthenticationSettingsToFile( | ||
file: string | ||
): Promise<boolean> { | ||
let outcome = false; | ||
debugMessage( | ||
`cli.AuthenticationSettingsOps.exportAuthenticationSettingsToFile: begin` | ||
); | ||
showSpinner( | ||
`Exporting ${frodo.utils.getRealmName( | ||
state.getRealm() | ||
)} realm authentication settings...` | ||
); | ||
try { | ||
let fileName = getTypedFilename( | ||
`${frodo.utils.getRealmName(state.getRealm())}Realm`, | ||
'authentication.settings' | ||
); | ||
if (file) { | ||
fileName = file; | ||
} | ||
const filePath = getFilePath(fileName, true); | ||
const exportData = await _exportAuthenticationSettings(); | ||
saveJsonToFile(exportData, filePath); | ||
succeedSpinner( | ||
`Exported ${frodo.utils.getRealmName( | ||
state.getRealm() | ||
)} realm authentication settings to ${filePath}.` | ||
); | ||
outcome = true; | ||
} catch (error) { | ||
failSpinner( | ||
`Error exporting ${frodo.utils.getRealmName( | ||
state.getRealm() | ||
)} realm authentication settings: ${error.message}` | ||
); | ||
} | ||
debugMessage( | ||
`cli.AuthenticationSettingsOps.exportAuthenticationSettingsToFile: end` | ||
); | ||
return outcome; | ||
} | ||
|
||
/** | ||
* Import authentication settings from file | ||
* @param {string} file file name | ||
* @returns {Promise<boolean>} true if successful, false otherwise | ||
*/ | ||
export async function importAuthenticationSettingsFromFile( | ||
file: string | ||
): Promise<boolean> { | ||
let outcome = false; | ||
debugMessage( | ||
`cli.AuthenticationSettingsOps.importAuthenticationSettingsFromFile: begin` | ||
); | ||
showSpinner( | ||
`Importing ${frodo.utils.getRealmName( | ||
state.getRealm() | ||
)} realm authentication settings...` | ||
); | ||
try { | ||
const data = fs.readFileSync(getFilePath(file), 'utf8'); | ||
const fileData = JSON.parse(data); | ||
await _importAuthenticationSettings(fileData); | ||
outcome = true; | ||
succeedSpinner( | ||
`Imported ${frodo.utils.getRealmName( | ||
state.getRealm() | ||
)} realm authentication settings.` | ||
); | ||
} catch (error) { | ||
failSpinner( | ||
`Error importing ${frodo.utils.getRealmName( | ||
state.getRealm() | ||
)} realm authentication settings.` | ||
); | ||
printMessage(error, 'error'); | ||
} | ||
debugMessage( | ||
`cli.AuthenticationSettingsOps.importAuthenticationSettingsFromFile: end` | ||
); | ||
return outcome; | ||
} | ||
|
||
/** | ||
* Describe authentication settings | ||
* @param {boolean} json JSON output | ||
* @returns {Promise<boolean>} true if successful, false otherwise | ||
*/ | ||
export async function describeAuthenticationSettings( | ||
json = false | ||
): Promise<boolean> { | ||
let outcome = false; | ||
try { | ||
const settings = await _readAuthenticationSettings(); | ||
delete settings._id; | ||
delete settings._rev; | ||
delete settings._type; | ||
if (json) { | ||
printMessage(settings, 'data'); | ||
outcome = true; | ||
} else { | ||
const table = createObjectTable(settings); | ||
printMessage(table.toString(), 'data'); | ||
outcome = true; | ||
} | ||
} catch (error) { | ||
printMessage(error.message, 'error'); | ||
} | ||
return outcome; | ||
} |
Oops, something went wrong.