Class: Teems::Services::TokenStore

Inherits:
Object
  • Object
show all
Includes:
TokenLookup
Defined in:
lib/teems/services/token_store.rb

Overview

Manages Teams authentication tokens stored in config directory

Constant Summary collapse

TOKENS_FILE =
'tokens.json'

Instance Method Summary collapse

Methods included from TokenLookup

#client_id, #configured?, #refresh_token, #skype_spaces_token, #tenant_id, #token_age

Constructor Details

#initialize(paths: Support::XdgPaths.new) ⇒ TokenStore

Returns a new instance of TokenStore.



36
37
38
# File 'lib/teems/services/token_store.rb', line 36

def initialize(paths: Support::XdgPaths.new)
  @paths = paths
end

Instance Method Details

#accountObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/teems/services/token_store.rb', line 40

def 
  auth, skype, name, chatsvc, presence =
    load_tokens.values_at('auth_token', 'skype_token', 'name', 'chatsvc_token', 'skype_spaces_token')
  return nil unless auth && skype

  Models::Account.new(
    name: name || 'default', auth_token: auth,
    skype_token: skype, chatsvc_token: chatsvc, presence_token: presence
  )
end

#clearObject



76
# File 'lib/teems/services/token_store.rb', line 76

def clear = FileUtils.rm_f(tokens_file)

#save(**tokens) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/teems/services/token_store.rb', line 51

def save(**tokens)
  @paths.ensure_config_dir
  data = tokens.transform_keys(&:to_s).merge('saved_at' => Time.now.iso8601).compact
  write_token_file(data)
rescue SystemCallError, IOError => e
  warn "teems: Could not save tokens: #{e.message}"
  false
end

#update_all_tokens(**tokens) ⇒ Object

Update all tokens at once (used by OIDC refresh)



69
70
71
72
73
74
# File 'lib/teems/services/token_store.rb', line 69

def update_all_tokens(**tokens)
  with_loaded_tokens { |data| apply_all_tokens(data, tokens) }
rescue SystemCallError, IOError => e
  warn "teems: Could not update token file: #{e.message}"
  false
end

#update_skype_token(skype_token) ⇒ Object

Update just the skype_token (used during refresh)



61
62
63
64
65
66
# File 'lib/teems/services/token_store.rb', line 61

def update_skype_token(skype_token)
  with_loaded_tokens { |data| apply_skype_token(data, skype_token) }
rescue SystemCallError, IOError => e
  warn "teems: Could not update token file: #{e.message}"
  false
end