Class: Hyraft::Rule::Jwt::JwtCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/hyraft/rule/jwt/jwt_command.rb

Constant Summary collapse

ENV_FILE =
'env.yml'

Class Method Summary collapse

Class Method Details

.generate_jwtObject



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
# File 'lib/hyraft/rule/jwt/jwt_command.rb', line 53

def self.generate_jwt
  puts "Generating JWT Secret Key..."
  key = SecureRandom.hex(64)
  
  # Read existing env.yml
  if File.exist?(ENV_FILE)
    config = YAML.load_file(ENV_FILE)
  else
    config = {}
  end
  
  # Update JWT_SECRET_KEY
  config['JWT_SECRET_KEY'] = key
  
  # Ensure JWT_EXPIRY_HOURS exists
  config['JWT_EXPIRY_HOURS'] ||= 24
  
  # Write back to file
  File.write(ENV_FILE, config.to_yaml)
  
  puts "JWT Secret Key generated and saved to #{ENV_FILE}"
  puts "=" * 50
  puts key
  puts "=" * 50
  puts "JWT_EXPIRY_HOURS is set to: #{config['JWT_EXPIRY_HOURS']}"
end

.handle_generate(subcommand) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/hyraft/rule/jwt/jwt_command.rb', line 33

def self.handle_generate(subcommand)
  case subcommand
  when 'jwt', nil
    generate_jwt
  else
    puts "Unknown generate subcommand: #{subcommand}"
    show_usage
  end
end

.handle_show(subcommand) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/hyraft/rule/jwt/jwt_command.rb', line 43

def self.handle_show(subcommand)
  case subcommand
  when 'jwt', nil
    show_jwt
  else
    puts "Unknown show subcommand: #{subcommand}"
    show_usage
  end
end

.show_jwtObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hyraft/rule/jwt/jwt_command.rb', line 80

def self.show_jwt
  if File.exist?(ENV_FILE)
    config = YAML.load_file(ENV_FILE)
    if config['JWT_SECRET_KEY']
      puts "Current JWT Secret Key:"
      puts "=" * 50
      puts config['JWT_SECRET_KEY']
      puts "=" * 50
      puts "Expiry: #{config['JWT_EXPIRY_HOURS'] || 24} hours"
      puts "Environment: #{ENV['RACK_ENV'] || 'development'}"
    else
      puts "No JWT_SECRET_KEY found in #{ENV_FILE}"
      puts "   Run: hyr-rule g jwt"
    end
  else
    puts "#{ENV_FILE} not found"
    puts "   Creating new env.yml file..."
    
    # Create basic env.yml
    config = {
      'JWT_SECRET_KEY' => SecureRandom.hex(64),
      'JWT_EXPIRY_HOURS' => 24,
      'APP_NAME' => 'hyraft-app',
      'SERVER_PORT' => 1091
    }
    
    File.write(ENV_FILE, config.to_yaml)
    puts "Created #{ENV_FILE} with new JWT key"
    show_jwt # Show the newly created key
  end
end

.show_usageObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/hyraft/rule/jwt/jwt_command.rb', line 112

def self.show_usage
  puts <<~USAGE
    Hyraft JWT Command
    ======================
    Usage: hyraft-rule jwt <command> [subcommand]

    Commands:
      g, generate jwt        - Generate JWT secret key (auto-saves to env.yml)
      show jwt               - Show current JWT secret
      help                   - Show this help message

    Examples:
      hyraft-rule jwt generate
      hyraft-rule jwt g
      hyraft-rule jwt show
      hyraft-rule jwt help
  USAGE
end

.start(args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hyraft/rule/jwt/jwt_command.rb', line 12

def self.start(args)
  return show_usage if args.empty?
  
  command = args[0]
  subcommand = args[1]
  
  case command
  when 'g', 'generate'
    handle_generate(subcommand)
  when 'show'
    handle_show(subcommand)
  when 'help', '--help', '-h'
    show_usage
  else
    puts "Unknown command: #{command}"
    show_usage
  end
end