コード例 #1
0
ファイル: warden.js プロジェクト: rapid7/tokend
router.route('/v1/authenticate').post((req, res) => {
  vault.mounts().then((mounts) => {
    if (!mounts.hasOwnProperty('transit/')) {
      vault.mount({
        mount_point: 'transit',
        type: 'transit'
      });
    }
  }).then(() => {
    let token;

    vault.tokenCreate({
      ttl: '1m',
      renewable: true,
      no_parent: true
    }).then((data) => {
      token = data.auth;

      return vault.tokenLookupAccessor({accessor: data.auth.accessor});
    }).then((resp) => {
      const creation_time = resp.data.creation_time * 1000;
      const explicit_max_ttl = resp.data.explicit_max_ttl * 1000;

      if (explicit_max_ttl === 0) {
        return vault.request({
          path: '/sys/mounts/auth/token/tune',
          method: 'GET'
        }).then((resp) => ({
          creation_time,
          explicit_max_ttl: resp.max_lease_ttl * 1000
        }));
      }

      return {
        creation_time,
        explicit_max_ttl
      };
    }).then((data) => ({
      creation_time: new Date(data.creation_time).toISOString(),
      expiration_time: new Date(data.creation_time + data.explicit_max_ttl).toISOString()
    })).then((data) => {
      if (data.expiration_time <= data.creation_time) {
        throw new Error('Token has already expired');
      }

      const resp = Object.assign({}, token, data);

      console.log(resp);
      res.json(resp);
    });
  }).catch((err) => {
    console.log(err);
    res.status(err.statusCode).json(err.error);
  });
});
コード例 #2
0
ファイル: warden.js プロジェクト: rapid7/tokend
router.route('/mounts').get((req, res) => {
  vault.mounts()
    .then((mounts) => res.json(mounts))
    .catch((err) => res.status(err.statusCode).json(err.error));
});