Skip to content

Instantly share code, notes, and snippets.

@RobinDaugherty
Last active December 10, 2019 20:04
Show Gist options
  • Save RobinDaugherty/f306c1ca75dffd5a69e7640eab00f398 to your computer and use it in GitHub Desktop.
Save RobinDaugherty/f306c1ca75dffd5a69e7640eab00f398 to your computer and use it in GitHub Desktop.
Bitium export to 1Password
// Paste this into the console on the Manage Apps page of Bitium.
//
// To make the process easier, it may be best to show as many items on the page as possible. By default, it shows 20,
// and it can be changed to 200.
//
// The items that are shown on the current page will be exported as a CSV, which will be downloaded as a file once
// all of the passwords have been fetched. (It fetches them slowly so that Bitium does not see it as suspicious activity.)
//
// If a password cannot be fetched, the row will be changed to a red background and it will have a "Fix admin visibility"
// button. This button will open the Credentials page in a new browser tab where you can use "Add Policy" to make
// the password visible. Set permissions to "Assign and View". Some of the items may not have a password, as some of the
// "apps" in Bitium do not represent a specific login, so there's no username or password stored.
//
// Once all of the apps have been changed to provide password visibility, you can refresh the Manage Apps page and paste
// this code into the console again.
//
// Once the file has downloaded, import it into 1Password using File -> Import, choosing "Other", then selecting
// "Import a CSV File".
//
// Each time you import into 1Password, it will tag all of the imported items with a name like "csvname-import-yyyy-mm-dd"
// so you can identify them later. You can find each of these tags in the navigation area on the left.
function BitiumExporterRow(bitiumExporter, tr) {
this.bitiumExporter = bitiumExporter;
this.tr = tr;
this.bitiumInstallationId = jQuery(tr).attr('id').match(/[0-9]+$/)[0];
var appLink = jQuery(tr).find('td.dt_apps .installation_tag strong a');
this.name = appLink.text();
this.bitiumUrl = appLink.attr('href');
var usernamePatternMatch = jQuery(tr).find('td.dt_apps .installation_tag span').text().match(/^\((.+)\)$/);
this.username = usernamePatternMatch ? usernamePatternMatch[1] : null;
this.url = jQuery(tr).find('td.dt_apps small').text();
this.bitiumAppType = jQuery(tr).find('td.dt_provider div').text();
}
BitiumExporterRow.prototype = {
bitiumOrganizationId: Newton.current_contact.org_id,
passwordCannotBeFetched() {
jQuery(this.tr).css({ backgroundColor: "red" });
var link = jQuery(this.tr).find('.dt_actions').html(`<a class="btn btn-bitium-inverse" href="${this.bitiumUrl}/credentials" target="_blank">Fix admin visibility</a>`)
},
passwordWasFetched() {
jQuery(this.tr).css({ backgroundColor: "green" });
},
fetchPassword() {
return new Promise((fetchPasswordResolve, fetchPasswordReject) => {
if(this.password) {
return fetchPasswordResolve();
}
console.log(`Fetching password for ${this.bitiumUrl}`);
if(this.bitiumExporter.debugMode) {
window.setTimeout(() => {
this.password = "password";
fetchPasswordResolve();
}, 200)
return;
}
jQuery.ajax({
url: `https://www.bitium.com/api/v2/organizations/${this.bitiumOrganizationId}/admin/installations/${this.bitiumInstallationId}/password?` + jQuery.param({
"return_to": "null",
"password": "true",
}),
type: "POST",
headers: {
"Accept": "*/*;q=0.5, text/javascript, application/javascript, application/ecmascript, application/x-ecmascript",
},
})
.done((data, textStatus, jqXHR) => {
this.password = data.password;
this.passwordWasFetched();
fetchPasswordResolve();
})
.fail((jqXHR, textStatus, errorThrown) => {
this.passwordCannotBeFetched();
fetchPasswordResolve();
jQuery('.modal-backdrop.in').hide();
jQuery('.modal.in.modalerror').hide();
});
})
}
};
function BitiumExporter() {
var bitiumExporter = this;
this.rows = jQuery('table#table_apps_overview tbody tr').
map(function() { return new BitiumExporterRow(bitiumExporter, this); });
}
function prepCsvField(data) {
if(data) {
return `"${data.replace(/"/g, '""')}"`;
} else {
return '""';
}
}
BitiumExporter.prototype = {
debugMode: false,
csvRecords() {
return this.rows.map(function() {
return [
prepCsvField(this.name),
prepCsvField(this.url),
prepCsvField(this.username),
prepCsvField(this.password),
prepCsvField(""),
prepCsvField(this.bitiumUrl),
prepCsvField(this.bitiumAppType),
].join();
});
},
fetchPasswords() {
return new Promise((fetchPasswordsResolve, fetchPasswordsReject) => {
var rowIndex = 0;
var next = () => {
if (rowIndex < this.rows.length) {
this.rows[rowIndex++].fetchPassword().then(next);
} else {
fetchPasswordsResolve();
}
};
next();
});
},
asCsv() {
return new Promise((asCsvResolve, asCsvReject) => {
this.fetchPasswords().then(() => {
var lines = this.csvRecords().toArray();
lines.unshift("title,URL,username,password,notes,bitiumUrl,bitiumAppType");
asCsvResolve(lines.join("\n") + "\n");
});
});
},
downloadCsv() {
this.asCsv().then((csv)=> location.href = 'data:text/csv;' + (window.btoa ? 'base64,' + btoa(csv) : csv));
}
};
var bitiumExporter = new BitiumExporter();
// bitiumExporter.debugMode = true;
bitiumExporter.downloadCsv();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment