class OpenProject::Token

Constants

VERSION

Attributes

extractor[R]
key[R]
block_changes_at[RW]
company[RW]
domain[RW]
expires_at[RW]
issued_at[RW]
mail[RW]
notify_admins_at[RW]
notify_users_at[RW]
restrictions[RW]
starts_at[RW]
subscriber[RW]
version[R]

Public Class Methods

import(data) click to toggle source
# File lib/open_project/token.rb, line 31
def import(data)
  raise ImportError, "Missing key." if key.nil?
  raise ImportError, "No token data." if data.nil?

  data = Armor.decode(data)
  json = extractor.read(data)
  attributes = JSON.parse(json)

  new(attributes)
rescue Extractor::Error
  raise ImportError, "Token value could not be read."
rescue JSON::ParserError
  raise ImportError, "Token value is invalid JSON."
rescue Armor::ParseError
  raise ImportError, "Token value could not be parsed."
end
key=(key) click to toggle source
# File lib/open_project/token.rb, line 22
def key=(key)
  if key && !key.is_a?(OpenSSL::PKey::RSA)
    raise ArgumentError, "Key is missing."
  end

  @key = key
  @extractor = Extractor.new(self.key)
end
new(attributes = {}) click to toggle source
# File lib/open_project/token.rb, line 73
def initialize(attributes = {})
  load_attributes(attributes)
end

Public Instance Methods

attributes() click to toggle source
# File lib/open_project/token.rb, line 122
def attributes
  hash = {}

  hash["version"]          = self.version
  hash["subscriber"]       = self.subscriber
  hash["mail"]             = self.mail
  hash["company"]          = self.company
  hash["domain"]           = self.domain

  hash["issued_at"]        = self.issued_at
  hash["starts_at"]        = self.starts_at
  hash["expires_at"]       = self.expires_at       if self.will_expire?

  hash["notify_admins_at"] = self.notify_admins_at if self.will_notify_admins?
  hash["notify_users_at"]  = self.notify_users_at  if self.will_notify_users?
  hash["block_changes_at"] = self.block_changes_at if self.will_block_changes?

  hash["restrictions"]     = self.restrictions     if self.restricted?

  hash
end
block_changes?() click to toggle source
# File lib/open_project/token.rb, line 105
def block_changes?
  will_block_changes? && Date.today >= self.block_changes_at
end
expired?() click to toggle source
# File lib/open_project/token.rb, line 93
def expired?
  will_expire? && Date.today >= self.expires_at
end
from_json(json) click to toggle source
# File lib/open_project/token.rb, line 148
def from_json(json)
  load_attributes(JSON.parse(json))
rescue => e
  raise ParseError, "Failed to load from json: #{e}"
end
notify_admins?() click to toggle source
# File lib/open_project/token.rb, line 97
def notify_admins?
  will_notify_admins? && Date.today >= self.notify_admins_at
end
notify_users?() click to toggle source
# File lib/open_project/token.rb, line 101
def notify_users?
  will_notify_users? && Date.today >= self.notify_users_at
end
restricted?(key = nil) click to toggle source
# File lib/open_project/token.rb, line 114
def restricted?(key = nil)
  if key
    restricted? && restrictions.has_key?(key)
  else
    restrictions && restrictions.length >= 1
  end
end
to_json() click to toggle source
# File lib/open_project/token.rb, line 144
def to_json
  JSON.dump(self.attributes)
end
validate_domain?() click to toggle source

tokens with no version or a version lower than 2.0 don't have the attributes company or domain

# File lib/open_project/token.rb, line 110
def validate_domain?
  version && Gem::Version.new(version) >= domain_required_from_version
end
will_block_changes?() click to toggle source
# File lib/open_project/token.rb, line 89
def will_block_changes?
  self.block_changes_at
end
will_expire?() click to toggle source
# File lib/open_project/token.rb, line 77
def will_expire?
  self.expires_at
end
will_notify_admins?() click to toggle source
# File lib/open_project/token.rb, line 81
def will_notify_admins?
  self.notify_admins_at
end
will_notify_users?() click to toggle source
# File lib/open_project/token.rb, line 85
def will_notify_users?
  self.notify_users_at
end

Private Instance Methods

current_gem_version() click to toggle source
# File lib/open_project/token.rb, line 217
def current_gem_version
  @current_gem_version ||= Gem::Version.new(OpenProject::Token::VERSION.to_s)
end
date_attribute_keys() click to toggle source
# File lib/open_project/token.rb, line 213
def date_attribute_keys
  %w(starts_at issued_at expires_at notify_admins_at notify_users_at block_changes_at)
end
domain_required_from_version() click to toggle source
# File lib/open_project/token.rb, line 221
def domain_required_from_version
  @domain_required_from_version ||= Gem::Version.new('2.0')
end
load_attributes(attributes) click to toggle source
# File lib/open_project/token.rb, line 156
def load_attributes(attributes)
  attributes = Hash[attributes.map { |k, v| [k.to_s, v] }]

  @version = read_version attributes
  @subscriber = attributes["subscriber"]
  @mail = attributes["mail"]
  @company = attributes["company"]
  @domain = attributes["domain"]

  date_attribute_keys.each do |attr|
    value = attributes[attr]
    value = Date.parse(value) rescue nil if value.is_a?(String)

    next unless value

    send("#{attr}=", value)
  end

  restrictions = attributes["restrictions"]

  if restrictions && restrictions.is_a?(Hash)
    restrictions = Hash[restrictions.map { |k, v| [k.to_sym, v] }]
    @restrictions = restrictions
  end
end
read_version(attr) click to toggle source

Reads the version from the given attributes hash. Besides the usual values it allows for a special value `-1`. This is then results in the version being `nil` specifically rather than the current gem version by default.

This way the generated token will get what ever version the importing party has. This is important due to a bug in openproject-token 1.x where any version other than `nil` (or the integer literal 1) results in a “Version is too new” error. This affects all OpenProject installations with a version older than 10.6 which will run into internal server errors trying to activate their Enterprise tokens due to this.

Generating tokens with version `-1` prevents that.

@param attr [Hash] Parsed token attributes.

# File lib/open_project/token.rb, line 198
def read_version(attr)
  value = attr.include?("version") ? attr["version"] : current_gem_version.to_s
  version = nil

  if value.present? && value.to_s != "-1"
    version = Gem::Version.new value

    if version > current_gem_version
      raise ArgumentError, "Version is too new"
    end
  end

  version
end