<commit_before>if _G.js then
  error "Not available"
end

local Configuration = require "cosy.configuration"
local Digest        = require "cosy.digest"
local Random        = require "cosy.random"
local Time          = require "cosy.time"
local Jwt           = require "luajwt"
local App           = require "cosy.configuration-layers".app

Configuration.load "cosy.token"

if Configuration.token.secret == nil then
  App.token = {
    secret = Digest (Random ())
  }
end

local Token = {}

function Token.encode (token)
  local secret        = Configuration.token.secret
  local algorithm     = Configuration.token.algorithm
  local result, err   = Jwt.encode (token, secret, algorithm)
  if not result then
    error (err)
  end
  return result
end

function Token.decode (s)
  local key           = Configuration.token.secret
  local algorithm     = Configuration.token.algorithm
  local result, err   = Jwt.decode (s, key, algorithm)
  if not result then
    error (err)
  end
  return result
end

function Token.iteration ()
  local now    = Time ()
  local result = {
    iat      = now,
    nbf      = now - 1,
    exp      = now + Configuration.expiration.administration,
    iss      = Configuration.server.name,
    aud      = nil,
    sub      = "cosy:iteration",
    jti      = Digest (tostring (now + Random ())),
    contents = {
      type = "iteration",
    },
  }
  return Token.encode (result)
end<commit_msg>Add iteration token type.<commit_after>if _G.js then
  error "Not available"
end

local Configuration = require "cosy.configuration"
local Digest        = require "cosy.digest"
local Random        = require "cosy.random"
local Time          = require "cosy.time"
local Jwt           = require "luajwt"
local App           = require "cosy.configuration-layers".app

Configuration.load "cosy.token"

if Configuration.token.secret == nil then
  App.token = {
    secret = Digest (Random ())
  }
end

local Token = {}

function Token.encode (token)
  local secret        = Configuration.token.secret
  local algorithm     = Configuration.token.algorithm
  local result, err   = Jwt.encode (token, secret, algorithm)
  if not result then
    error (err)
  end
  return result
end

function Token.decode (s)
  local key           = Configuration.token.secret
  local algorithm     = Configuration.token.algorithm
  local result, err   = Jwt.decode (s, key, algorithm)
  if not result then
    error (err)
  end
  return result
end

function Token.iteration ()
  local now    = Time ()
  local result = {
    iat      = now,
    nbf      = now - 1,
    exp      = now + Configuration.expiration.iteration,
    iss      = Configuration.server.name,
    aud      = nil,
    sub      = "cosy:iteration",
    jti      = Digest (tostring (now + Random ())),
    contents = {
      type = "iteration",
    },
  }
  return Token.encode (result)
end
{'fraction_non_alphanumeric': 0.09612023767913316, 'fraction_numerical': 0.0006990562740300594, 'mean_word_length': 2.4690909090909092, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3469105', 'n_tokens_mistral': 858, 'n_tokens_neox': 850, 'n_words': 260}