Class: Terminalwire::Rails::Session
- Inherits:
-
Object
- Object
- Terminalwire::Rails::Session
- Extended by:
- Forwardable
- Defined in:
- lib/terminalwire/rails.rb
Constant Summary collapse
- FILENAME =
JWT file name for the session file.
"session.jwt"
- EMPTY_SESSION =
Empty dictionary the user can stash all their session data into.
{}.freeze
Instance Method Summary collapse
- #[]=(key, value) ⇒ Object
- #edit {|config| ... } ⇒ Object
-
#initialize(context:, path: nil, secret_key: self.class.secret_key) ⇒ Session
constructor
A new instance of Session.
- #read ⇒ Object
- #reset ⇒ Object
- #write(config) ⇒ Object
Constructor Details
#initialize(context:, path: nil, secret_key: self.class.secret_key) ⇒ Session
Returns a new instance of Session.
21 22 23 24 25 26 27 28 |
# File 'lib/terminalwire/rails.rb', line 21 def initialize(context:, path: nil, secret_key: self.class.secret_key) @context = context @path = Pathname.new(path || context.storage_path) @config_file_path = @path.join(FILENAME) @secret_key = secret_key ensure_file end |
Instance Method Details
#[]=(key, value) ⇒ Object
48 49 50 |
# File 'lib/terminalwire/rails.rb', line 48 def []=(key, value) edit { |config| config[key] = value } end |
#edit {|config| ... } ⇒ Object
42 43 44 45 46 |
# File 'lib/terminalwire/rails.rb', line 42 def edit config = read yield config write(config) end |
#read ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/terminalwire/rails.rb', line 30 def read jwt_token = @context.file.read(@config_file_path) decoded_data = JWT.decode(jwt_token, @secret_key, true, algorithm: 'HS256') decoded_data[0] # JWT payload is the first element in the array rescue JWT::DecodeError => e raise "Invalid or tampered file: #{e.}" end |
#reset ⇒ Object
38 39 40 |
# File 'lib/terminalwire/rails.rb', line 38 def reset @context.file.delete @config_file_path end |
#write(config) ⇒ Object
52 53 54 55 |
# File 'lib/terminalwire/rails.rb', line 52 def write(config) token = JWT.encode(config, @secret_key, 'HS256') @context.file.write(@config_file_path, token) end |