summaryrefslogtreecommitdiffstats
path: root/sandstorm.js
diff options
context:
space:
mode:
authorDavid Renshaw <david@sandstorm.io>2016-09-26 11:40:43 -0400
committerDavid Renshaw <david@sandstorm.io>2016-11-03 23:42:00 -0400
commit1ad44de8c6774a7caee43eeecdafe1893234e890 (patch)
tree7bed0a308b9be4946f9a97e6efb3e42d31c4ebe7 /sandstorm.js
parent9f4300d38b248a147ac0fdf39bb607ffed5bae7e (diff)
downloadwekan-1ad44de8c6774a7caee43eeecdafe1893234e890.tar.gz
wekan-1ad44de8c6774a7caee43eeecdafe1893234e890.tar.bz2
wekan-1ad44de8c6774a7caee43eeecdafe1893234e890.zip
powerbox identity requests
Diffstat (limited to 'sandstorm.js')
-rw-r--r--sandstorm.js144
1 files changed, 144 insertions, 0 deletions
diff --git a/sandstorm.js b/sandstorm.js
index d1b7b300..40a80dc6 100644
--- a/sandstorm.js
+++ b/sandstorm.js
@@ -21,6 +21,79 @@ const sandstormBoard = {
};
if (isSandstorm && Meteor.isServer) {
+ const Capnp = require('capnp');
+ const Powerbox = Capnp.importSystem('sandstorm/powerbox.capnp');
+ const Identity = Capnp.importSystem('sandstorm/identity.capnp');
+ const SandstormHttpBridge =
+ Capnp.importSystem('sandstorm/sandstorm-http-bridge.capnp').SandstormHttpBridge;
+
+ let httpBridge = null;
+
+ function getHttpBridge() {
+ if (!httpBridge) {
+ const capnpConnection = Capnp.connect('unix:/tmp/sandstorm-api');
+ httpBridge = capnpConnection.restore(null, SandstormHttpBridge);
+ }
+ return httpBridge;
+ }
+
+ Meteor.methods({
+ sandstormClaimIdentityRequest(token, descriptor) {
+ check(token, String);
+ check(descriptor, String);
+
+ const parsedDescriptor = Capnp.parse(
+ Powerbox.PowerboxDescriptor,
+ new Buffer(descriptor, 'base64'),
+ { packed: true });
+
+ const tag = Capnp.parse(Identity.Identity.PowerboxTag, parsedDescriptor.tags[0].value);
+ const permissions = [];
+ if (tag.permissions[1]) {
+ permissions.push('configure');
+ }
+
+ if (tag.permissions[0]) {
+ permissions.push('participate');
+ }
+
+ const sessionId = this.connection.sandstormSessionId();
+ const httpBridge = getHttpBridge();
+ const session = httpBridge.getSessionContext(sessionId).context;
+ const api = httpBridge.getSandstormApi(sessionId).api;
+
+ Meteor.wrapAsync((done) => {
+ session.claimRequest(token).then((response) => {
+ const identity = response.cap.castAs(Identity.Identity);
+ const promises = [api.getIdentityId(identity), identity.getProfile()];
+ return Promise.all(promises).then((responses) => {
+ const identityId = responses[0].id.toString('hex').slice(0, 32);
+ const profile = responses[1].profile;
+ return profile.picture.getUrl().then((response) => {
+ const sandstormInfo = {
+ id: identityId,
+ name: profile.displayName.defaultText,
+ permissions,
+ picture: `${response.protocol}://${response.hostPath}`,
+ preferredHandle: profile.preferredHandle,
+ pronouns: profile.pronouns,
+ };
+
+ const login = Accounts.updateOrCreateUserFromExternalService(
+ 'sandstorm', sandstormInfo,
+ { profile: { name: sandstormInfo.name, fullname: sandstormInfo.name } });
+
+ updateUserPermissions(login.userId, permissions);
+ done();
+ });
+ });
+ }).catch((e) => {
+ done(e, null);
+ });
+ })();
+ },
+ });
+
function updateUserPermissions(userId, permissions) {
const isActive = permissions.indexOf('participate') > -1;
const isAdmin = permissions.indexOf('configure') > -1;
@@ -137,6 +210,77 @@ if (isSandstorm && Meteor.isServer) {
}
if (isSandstorm && Meteor.isClient) {
+ let rpcCounter = 0;
+ const rpcs = {};
+
+ window.addEventListener('message', (event) => {
+ if (event.source === window) {
+ // Meteor likes to postmessage itself.
+ return;
+ }
+
+ if ((event.source !== window.parent) ||
+ typeof event.data !== 'object' ||
+ typeof event.data.rpcId !== 'number') {
+ throw new Error(`got unexpected postMessage: ${event}`);
+ }
+
+ const handler = rpcs[event.data.rpcId];
+ if (!handler) {
+ throw new Error(`no such rpc ID for event ${event}`);
+ }
+
+ delete rpcs[event.data.rpcId];
+ handler(event.data);
+ });
+
+ function sendRpc(name, message) {
+ const id = rpcCounter++;
+ message.rpcId = id;
+ const obj = {};
+ obj[name] = message;
+ window.parent.postMessage(obj, '*');
+ return new Promise((resolve, reject) => {
+ rpcs[id] = (response) => {
+ if (response.error) {
+ reject(new Error(response.error));
+ } else {
+ resolve(response);
+ }
+ };
+ });
+ }
+
+ const powerboxDescriptors = {
+ identity: 'EAhQAQEAABEBF1EEAQH_GN1RqXqYhMAAQAERAREBAQ',
+ // Generated using the following code:
+ //
+ // Capnp.serializePacked(
+ // Powerbox.PowerboxDescriptor,
+ // { tags: [ {
+ // id: "13872380404802116888",
+ // value: Capnp.serialize(Identity.PowerboxTag, { permissions: [true, false] })
+ // }]}).toString('base64')
+ // .replace(/\//g, "_")
+ // .replace(/\+/g, "-");
+ };
+
+ function doRequest(serializedPowerboxDescriptor, onSuccess) {
+ return sendRpc('powerboxRequest', {
+ query: [serializedPowerboxDescriptor],
+ }).then((response) => {
+ if (!response.canceled) {
+ onSuccess(response);
+ }
+ });
+ }
+
+ window.sandstormRequestIdentity = function () {
+ doRequest(powerboxDescriptors.identity, (response) => {
+ Meteor.call('sandstormClaimIdentityRequest', response.token, response.descriptor);
+ });
+ };
+
// Since the Sandstorm grain is displayed in an iframe of the Sandstorm shell,
// we need to explicitly expose meta data like the page title or the URL path
// so that they could appear in the browser window.