Commit cfb5d936 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Improve interface to makeToken.

It now takes a template that allows setting all options.
parent 3228147e
...@@ -2122,7 +2122,7 @@ function userMenu(elt) { ...@@ -2122,7 +2122,7 @@ function userMenu(elt) {
if(serverConnection.version !== "1" && if(serverConnection.version !== "1" &&
serverConnection.permissions.indexOf('token') >= 0) { serverConnection.permissions.indexOf('token') >= 0) {
items.push({label: 'Invite user', onClick: () => { items.push({label: 'Invite user', onClick: () => {
makeToken(null); makeToken();
}}); }});
} }
if(serverConnection.permissions.indexOf('present') >= 0 && canFile()) if(serverConnection.permissions.indexOf('present') >= 0 && canFile())
...@@ -3047,11 +3047,11 @@ const units = { ...@@ -3047,11 +3047,11 @@ const units = {
/** /**
* @param {string} s * @param {string} s
* @returns {string|number} * @returns {Date|number}
*/ */
function parseExpiration(s) { function parseExpiration(s) {
if(!s) if(!s)
return units.d; return null;
let re = /^([0-9]+)(s|min|h|d|yr)$/ let re = /^([0-9]+)(s|min|h|d|yr)$/
let e = re.exec(s) let e = re.exec(s)
if(e) { if(e) {
...@@ -3063,7 +3063,7 @@ function parseExpiration(s) { ...@@ -3063,7 +3063,7 @@ function parseExpiration(s) {
let d = new Date(s); let d = new Date(s);
if(d.toString() === 'Invalid Date') if(d.toString() === 'Invalid Date')
throw new Error("Couldn't parse expiration date"); throw new Error("Couldn't parse expiration date");
return d.toISOString(); return d;
} }
function protocol2Predicate() { function protocol2Predicate() {
...@@ -3086,20 +3086,25 @@ function editTokenPredicate() { ...@@ -3086,20 +3086,25 @@ function editTokenPredicate() {
} }
/** /**
* @param {string} username * @param {Object} [template]
* @param {number|string} [expires]
*/ */
function makeToken(username, expires) { function makeToken(template) {
if(!template)
template = {};
let v = { let v = {
group: group, group: group,
}; }
if(username) if('username' in template)
v.username = username; v.username = template.username;
if(expires) if('expires' in template)
v.expires = expires; v.expires = template.expires;
else else
v.expires = units.d; v.expires = units.d;
if(serverConnection.permissions.indexOf('present') >= 0) if('not-before' in template)
v["not-before"] = template["not-before"];
if('permissions' in template)
v.permissions = template.permissions;
else if(serverConnection.permissions.indexOf('present') >= 0)
v.permissions = ['present']; v.permissions = ['present'];
else else
v.permissions = []; v.permissions = [];
...@@ -3112,7 +3117,13 @@ commands.invite = { ...@@ -3112,7 +3117,13 @@ commands.invite = {
parameters: "[username] [expiration]", parameters: "[username] [expiration]",
f: (c, r) => { f: (c, r) => {
let p = parseCommand(r); let p = parseCommand(r);
makeToken(p[0], parseExpiration(p[1])); let template = {};
if(p[0])
template.username = p[0];
let expires = parseExpiration(p[1]);
if(expires)
template.expires = expires;
makeToken(template);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment