Skip to content

Instantly share code, notes, and snippets.

@trevorfoskett
trevorfoskett / defaultClient.html
Created November 5, 2019 16:59
Default Virtru JS Client from quickstart.
<body>
<div id="virtru-auth-widget-mount"></div>
<script type="text/javascript">
async function afterAuth(email) {
// Run all client code from here.
// This will only be called when the user is successfully authenticated.
const client = new Virtru.Client({email});
const yourString = prompt('Type a sting to encrypt: ', 'Hello, world!');
const encryptParams = new Virtru.EncryptParamsBuilder()
.withStringSource(yourString)
@trevorfoskett
trevorfoskett / importSDK.html
Last active November 5, 2019 17:22
Import Virtru SDK and styleing
<head>
<link href="https://sdk.virtru.com/js/latest/auth-widget/index.css" rel="stylesheet"/>
<script src="https://sdk.virtru.com/js/latest/auth-widget/index.js"></script>
<script src="https://sdk.virtru.com/js/latest/virtru-sdk.min.js"></script>
</head>
@trevorfoskett
trevorfoskett / createPDF.gs
Last active November 5, 2019 17:39
Server-side Google Apps Script code to generate base 64-encoded PDF blob from Google Doc.
function createPDF() {
var docBlob = DocumentApp.getActiveDocument().getBlob();
docBlob.setName(doc.getName() + '.pdf');
var blobB64 = Utilities.base64Encode(docBlob.getBytes());
return blobB64;
}
@trevorfoskett
trevorfoskett / convertPDFData.js
Last active November 5, 2019 20:46
Convert the base-64 encoded blob generated on the server into an array buffer for encryption.
/*
* Handles client-side conversion of document
* data from server to a format we can use for encryption.
* Called in both download & email workflows.
*
* @return {arrayBuffer} Array Buffer of the document content.
*/
async function convertPDFData() {
// Gets document content; converts
// document content back into a usable
@trevorfoskett
trevorfoskett / buildPolicy.js
Last active November 5, 2019 20:47
Builds access control policy based on user checkbox selections.
/*
* Builds policy according to user inputs.
*
* @param {Array} authUsers The list of authorized users for this piece of content.
* @return {Policy} The policy for this piece of content.
*/
function buildPolicy() {
var policy = new Virtru.PolicyBuilder();
if ($('#watermark-toggle').is(":checked")) {
@trevorfoskett
trevorfoskett / customizeClient.js
Last active November 5, 2019 20:47
Customize the default Virtru client to accept arrayBuffer input and output pdf.tdf3.html
async function afterAuth(email) {
// Run all client code from here.
// This will only be called when the user is successfully authenticated.
const client = new Virtru.Client({email});
const yourString = prompt('Type a sting to encrypt: ', 'Hello, world!');
const encryptParams = new Virtru.EncryptParamsBuilder()
.withArrayBufferSource(arrayBuffer) // Change input to accept arrayBuffer
.withDisplayFilename('hello.pdf') // Change display filename to reflect PDF
.build();
const ct = await client.encrypt(encryptParams);
@trevorfoskett
trevorfoskett / pdfToClient.js
Last active November 5, 2019 20:48
Runs server-side function to return the contents of the document in base64 format.
/*
* Runs server-side function to return the contents
* of the document in base64 format.
*
* @return {string} Base 64'd document content.
*/
function genPDF() {
return new Promise(function(resolve, reject) {
google.script.run
.withSuccessHandler(function(blobB64) {
@trevorfoskett
trevorfoskett / addPolicy.js
Last active November 5, 2019 20:51
Adding a policy object to Virtru encryption client.
const encryptParams = new Virtru.EncryptParamsBuilder()
.withArrayBufferSource(arrayBuffer)
.withDisplayFilename(`${docTitle}.pdf`)
.withPolicy(policy)
.withUsersWithAccess(authorizedUsers)
.build();
@trevorfoskett
trevorfoskett / clientEmail.js
Created November 5, 2019 21:03
Updated Virtru encrypt client to allow the emailing of the protected file.
const client = new Virtru.Client({email});
const encryptParams = new Virtru.EncryptParamsBuilder()
.withArrayBufferSource(arrayBuffer)
.withDisplayFilename(`${docTitle}.pdf`)
.withPolicy(policy)
.withUsersWithAccess(authorizedUsers)
.build();
const ct = await client.encrypt(encryptParams);
var ctString = await ct.toString(); // Encrypt to string rather than to file
@trevorfoskett
trevorfoskett / sendEmail.gs
Last active November 5, 2019 21:12
Server-side function to generate and send an email with encrypted attachment content.
function sendEmail(cipherText, recipients, userMessage) {
// Get email address of file owner and assign attachment title.
var fileOwner = Session.getActiveUser().getEmail();
var fileName = DocumentApp.getActiveDocument().getName() + ".pdf.tdf3.html";
// Provide a basic email body for recipients who do not support HTML.
var emailBody = fileOwner + " has shared the encrypted file " + fileName +
" with you.\r\n\r\nIt\'s attached below; please download to open in" +
" Virtru\'s Secure Reader.";