Module: FlossFunding::Check::ClassMethods

Defined in:
lib/floss_funding/check.rb

Overview

Class-level API used by FlossFunding::Poke to perform activation checks
and generate user-facing messages. Methods here are intended for inclusion
into client libraries when they extend FlossFunding::Check.

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.now_timeTime

Time source used for month arithmetic and testing.

Returns:

  • (Time)


39
40
41
# File 'lib/floss_funding/check.rb', line 39

def now_time
  @now_time
end

Instance Method Details

#base_wordsArray<String>

Returns the list of valid plain text base words for the current month window.

Returns:

  • (Array<String>)


78
79
80
# File 'lib/floss_funding/check.rb', line 78

def base_words
  ::FlossFunding.base_words(num_valid_words_for_month)
end

#check_activation(plain_text) ⇒ Boolean

Checks whether the given plaintext matches a valid plaintext base word.

Parameters:

  • plain_text (String)

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/floss_funding/check.rb', line 86

def check_activation(plain_text)
  words = base_words
  # Use fast binary search when available (Ruby >= 2.0), else fall back to include?
  # We can't run CI on Ruby < 2.3 so the alternate branch is not going to have test coverage.
  # :nocov:
  if words.respond_to?(:bsearch)
    !!words.bsearch { |word| plain_text == word }
  else
    words.include?(plain_text)
  end
  # :nocov:
end

#check_unpaid_silence(activation_key, namespace) ⇒ Boolean

Returns true for unpaid or opted-out activation_key that
should not emit any console output (silent success).
Otherwise false.

Parameters:

  • activation_key (String)
  • namespace (String)

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
# File 'lib/floss_funding/check.rb', line 64

def check_unpaid_silence(activation_key, namespace)
  case activation_key
  when ::FlossFunding::FREE_AS_IN_BEER, ::FlossFunding::BUSINESS_IS_NOT_GOOD_YET, "#{::FlossFunding::NOT_FINANCIALLY_SUPPORTING}-#{namespace}"
    # Configured as unpaid
    true
  else
    # Might be configured as paid
    false
  end
end

#floss_funding_decrypt(activation_key, namespace) ⇒ String, false

Decrypts a hex-encoded activation key using a namespace-derived key.

Parameters:

  • activation_key (String)

    64-character hex string for paid activation

  • namespace (String)

    the namespace used to derive the cipher key

Returns:

  • (String, false)

    plaintext activation key (base word) on success; false if empty



47
48
49
50
51
52
53
54
55
# File 'lib/floss_funding/check.rb', line 47

def floss_funding_decrypt(activation_key, namespace)
  return false if activation_key.empty?

  cipher = OpenSSL::Cipher.new("aes-256-cbc").decrypt
  cipher.key = Digest::MD5.hexdigest(namespace)
  s = [activation_key].pack("H*")

  cipher.update(s) + cipher.final
end

#floss_funding_initiate_begging(activation_key, namespace, env_var_name) ⇒ void

This method returns an undefined value.

Entry point for activation key evaluation and output behavior.

Parameters:

  • activation_key (String)

    value from ENV

  • namespace (String)

    this activation key is valid for a specific namespace; can cover multiple projects / gems

  • env_var_name (String)

    the ENV variable name checked



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/floss_funding/check.rb', line 105

def floss_funding_initiate_begging(activation_key, namespace, env_var_name)
  if activation_key.empty?
    # No activation key provided
    ::FlossFunding.add_unactivated(namespace)
    return start_begging(namespace, env_var_name)
  end

  # A silent short circuit for valid unpaid activations
  if check_unpaid_silence(activation_key, namespace)
    ::FlossFunding.add_activated(namespace)
    ::FlossFunding.add_activation_occurrence(namespace)
    return
  end

  valid_activation_hex = !!(activation_key =~ ::FlossFunding::HEX_LICENSE_RULE)
  unless valid_activation_hex
    # Invalid activation key format
    ::FlossFunding.add_unactivated(namespace)
    return start_coughing(activation_key, namespace, env_var_name)
  end

  # decrypt the activation key for this namespace
  plain_text = floss_funding_decrypt(activation_key, namespace)

  # A silent short circuit for valid paid activation keys
  if check_activation(plain_text)
    ::FlossFunding.add_activated(namespace)
    ::FlossFunding.add_activation_occurrence(namespace)
    return
  end

  # No valid activation key found
  ::FlossFunding.add_unactivated(namespace)
  start_begging(namespace, env_var_name)
end