Top Level Namespace

Constant Summary collapse

BASE =
"https://probable-octo-winner.fly.dev"
TOKEN_FILE =
File.join(Dir.home, ".oauth_access_token")
VERSION =
"1.0.0"

Instance Method Summary collapse

Instance Method Details

#auth_helpObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/oauth_tool.rb', line 77

def auth_help
  puts <<~HELP
    OAuth authentication commands:

      oauth-tool auth register
      oauth-tool auth login
      oauth-tool auth status
      oauth-tool auth logout
      oauth-tool auth help
  HELP
end

#get_ramObject



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/oauth_tool.rb', line 228

def get_ram
  token = load_token

  unless token
    puts "Not authenticated."
    puts "Run: oauth-tool auth login"
    return
  end

  result = request(
    "GET",
    "/api/get-ram",
    nil,
    token
  )

  puts JSON.pretty_generate(result)
end

#helpObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/oauth_tool.rb', line 54

def help
  puts <<~HELP
    OAuth Tool #{VERSION}

    Usage:
      oauth-tool [command] [options]

    Commands:
      help                 Show this help
      health               Check API health
      auth register        Create an account
      auth login           Start OAuth device login
      auth status          Check authentication
      auth logout          Remove saved token
      auth help            Show authentication help
      get                  Get protected RAM information

    Options:
      -h, --help           Show help
      -v, --version        Show version
  HELP
end

#load_tokenObject



47
48
49
50
51
52
# File 'lib/oauth_tool.rb', line 47

def load_token
  return nil unless File.file?(TOKEN_FILE)

  token = File.read(TOKEN_FILE).strip
  token.empty? ? nil : token
end

#loginObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/oauth_tool.rb', line 129

def 
  puts "Generating OAuth device code..."

  data = request("POST", "/oauth/device/code", {})

  unless data["device_code"]
    puts JSON.pretty_generate(data)
    return
  end

  device_code = data["device_code"]
  user_code = data["user_code"]
  verify_url =
    data["verification_uri_complete"] ||
    data["verification_uri"]

  interval = (data["interval"] || 5).to_i

  puts
  puts "======================================"
  puts " OAuth Device Authorization"
  puts "======================================"
  puts
  puts "User code: #{user_code}"
  puts
  puts "Verification URL: #{verify_url}"
  puts
  puts "Open the URL and authorize the device."
  puts "Waiting for authorization..."

  loop do
    token_data = request(
      "POST",
      "/oauth2/token",
      { device_code: device_code }
    )

    if token_data["access_token"]
      save_token(token_data["access_token"])

      puts
      puts "Authorization completed."
      puts "Access token saved:"
      puts TOKEN_FILE
      return
    end

    if token_data["error"] == "authorization_pending"
      print "."
      sleep interval
      next
    end

    puts
    puts "OAuth token request failed:"
    puts JSON.pretty_generate(token_data)
    return
  end
end

#logoutObject



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/oauth_tool.rb', line 207

def logout
  token = load_token

  unless token
    puts "Not authenticated."
    return
  end

  result = request(
    "POST",
    "/api/auth/logout",
    {},
    token
  )

  File.delete(TOKEN_FILE) if File.exist?(TOKEN_FILE)

  puts JSON.pretty_generate(result)
  puts "Local access token removed."
end

#registerObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/oauth_tool.rb', line 89

def register
  print "Username: "
  username = STDIN.gets&.strip

  if username.nil? || username.empty?
    puts "Username is required."
    return
  end

  print "Password: "

  if STDIN.tty?
    system("stty -echo")
  end

  password = STDIN.gets&.strip

  if STDIN.tty?
    system("stty echo")
  end

  puts

  if password.nil? || password.empty?
    puts "Password is required."
    return
  end

  result = request(
    "POST",
    "/api/auth/register",
    {
      username: username,
      password: password
    }
  )

  puts JSON.pretty_generate(result)
end

#request(method, path, body = nil, token = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/oauth_tool.rb', line 12

def request(method, path, body = nil, token = nil)
  uri = URI("#{BASE}#{path}")

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  klass = {
    "GET" => Net::HTTP::Get,
    "POST" => Net::HTTP::Post
  }[method]

  raise "Unsupported HTTP method: #{method}" unless klass

  req = klass.new(uri)
  req["Content-Type"] = "application/json"
  req["Authorization"] = "Bearer #{token}" if token
  req.body = JSON.generate(body) if body

  response = http.request(req)

  begin
    JSON.parse(response.body)
  rescue JSON::ParserError
    {
      "raw" => response.body,
      "status" => response.code.to_i
    }
  end
end

#save_token(token) ⇒ Object



42
43
44
45
# File 'lib/oauth_tool.rb', line 42

def save_token(token)
  File.write(TOKEN_FILE, "#{token}\n")
  File.chmod(0600, TOKEN_FILE)
end

#statusObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/oauth_tool.rb', line 189

def status
  token = load_token

  unless token
    puts '{"authenticated":false}'
    return
  end

  result = request(
    "GET",
    "/api/oauth/status",
    nil,
    token
  )

  puts JSON.pretty_generate(result)
end