declare local var.secret STRING;
set var.secret = "iqFPeN2uZ0Lm5IrsKaOFKR";
declare local var.token STRING;
if (:token) {
set var.token = :token;
} else if (subfield(req.url.qs, "token", "&")) {
set var.token = subfield(req.url.qs, "token", "&");
} else {
log "syslog :: Missing 'token' query param or cookie";
error 403;
}
declare local var.expiryTime STRING;
declare local var.suppliedSig STRING;
declare local var.expectedSig STRING;
if (var.token ~ "^(\d+)_(\w+)$") {
set var.expiryTime = re.group.1;
set var.suppliedSig = re.group.2;
set var.expectedSig = digest.hmac_sha1(
var.secret,
req.url.path var.expiryTime
);
if (var.suppliedSig == var.expectedSig) {
if (time.is_after(now, std.integer2time(std.atoi(var.expiryTime)))) {
log "syslog :: Token has expired";
error 410;
}
} else {
log "syslog :: Token is incorrect, expected " + var.expectedSig;
error 410;
}
} else {
log "syslog :: Token format is invalid";
error 403;
}
set req.url = regsuball(req.url, "([\?|&])token=[^&\s]*&?", "\1");
log "syslog :: Token is good";
set req.url = regsuball(req.url, "[\?|&]+$", "");