Class: Cased::CLI::InteractiveSession

Inherits:
Object
  • Object
show all
Defined in:
lib/cased/cli/interactive_session.rb

Overview

InteractiveSession is responsible for initiating a Cased CLI session and responding to all its possible states.

InteractiveSession is intended to be used where a TTY is present to handle the entire flow from authentication, reason required, waiting for approval, canceled, or timed out.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reason: nil, command: nil, metadata: {}) ⇒ InteractiveSession

Returns a new instance of InteractiveSession.



25
26
27
28
29
30
31
32
33
# File 'lib/cased/cli/interactive_session.rb', line 25

def initialize(reason: nil, command: nil, metadata: {})
  @session = Cased::CLI::Session.new(
    reason: reason,
    command: command,
    metadata: ,
  )

  @prompt = TTY::Prompt.new
end

Instance Attribute Details

#sessionObject (readonly)

Returns the value of attribute session.



23
24
25
# File 'lib/cased/cli/interactive_session.rb', line 23

def session
  @session
end

Class Method Details

.start(reason: nil, command: nil, metadata: {}) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/cased/cli/interactive_session.rb', line 15

def self.start(reason: nil, command: nil, metadata: {})
  return Cased::CLI::Session.current if Cased::CLI::Session.current&.approved?

  Cased::CLI::Log.log 'Running under Cased CLI.'

  new(reason: reason, command: command, metadata: ).create
end

Instance Method Details

#createObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cased/cli/interactive_session.rb', line 35

def create
  signal_handler = Signal.trap('SIGINT') do
    if session.requested?
      Cased::CLI::Log.log 'Exiting and canceling request…'
      session.cancel
      exit 0
    elsif signal_handler.respond_to?(:call)
      # We need to call the original handler if we exit this interactive
      # session successfully
      signal_handler.call
    else
      raise Interrupt
    end
  end

  if session.create
    handle_state(session.state)
  elsif session.reauthenticate?
    Cased::CLI::Log.log "You must re-authenticate with Cased due to recent changes to this application's settings."

    identity = Cased::CLI::Identity.new
    token, ip_address = identity.identify
    session.authentication.token = token
    session.forwarded_ip_address = ip_address

    create
  elsif session.unauthorized?
    if session.authentication.exists?
      Cased::CLI::Log.log "Existing credentials at #{session.authentication.credentials_path} are not valid."
    else
      Cased::CLI::Log.log "Could not find credentials at #{session.authentication.credentials_path}, looking up now…"
    end

    identity = Cased::CLI::Identity.new
    token, ip_address = identity.identify
    session.authentication.token = token
    session.forwarded_ip_address = ip_address

    create
  elsif session.reason_required?
    reason_prompt && create
  else
    Cased::CLI::Log.log 'Could not start CLI session.'
    exit 1 if Cased.config.guard_deny_if_unreachable?
  end

  session
end