Class: OpenC3::AuthModel

Inherits:
Object show all
Defined in:
lib/openc3/models/auth_model.rb

Constant Summary collapse

PRIMARY_KEY =
'OPENC3__TOKEN'
TOKEN_CACHE_TIMEOUT =
5
@@token_cache =
nil
@@token_cache_time =
nil

Class Method Summary collapse

Class Method Details

.hash(token) ⇒ Object



65
66
67
# File 'lib/openc3/models/auth_model.rb', line 65

def self.hash(token)
  Digest::SHA2.hexdigest token
end

.set(token, old_token, key = PRIMARY_KEY) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/openc3/models/auth_model.rb', line 55

def self.set(token, old_token, key = PRIMARY_KEY)
  raise "token must not be nil or empty" if token.nil? or token.empty?

  if set?(key)
    raise "old_token must not be nil or empty" if old_token.nil? or old_token.empty?
    raise "old_token incorrect" unless verify(old_token)
  end
  Store.set(key, hash(token))
end

.set?(key = PRIMARY_KEY) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/openc3/models/auth_model.rb', line 34

def self.set?(key = PRIMARY_KEY)
  Store.exists(key) == 1
end

.verify(token) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/openc3/models/auth_model.rb', line 38

def self.verify(token)
  return false if token.nil? or token.empty?

  token_hash = hash(token)
  return true if @@token_cache and (Time.now - @@token_cache_time) < TOKEN_CACHE_TIMEOUT and @@token_cache == token_hash

  @@token_cache = Store.get(PRIMARY_KEY)
  @@token_cache_time = Time.now
  return true if @@token_cache == token_hash

  # Handle a service password - Generally only used by ScriptRunner
  service_password = ENV['OPENC3_SERVICE_PASSWORD']
  return true if service_password and service_password == token

  return false
end