0.2.5: Add token validation.
This commit is contained in:
parent
ee578cc90b
commit
b5b2a5a120
3 changed files with 39 additions and 0 deletions
18
src/main.cr
18
src/main.cr
|
@ -3,6 +3,7 @@ require "kemal"
|
|||
require "json"
|
||||
require "uri"
|
||||
require "redis"
|
||||
require "colorize"
|
||||
|
||||
require "./config"
|
||||
require "./routes/**"
|
||||
|
@ -19,6 +20,22 @@ REDIS_UTILS = TwAPI::Utils::Redis
|
|||
REDIS_DB = Redis::Client.new(URI.parse("redis://#{CONFIG.redis_addr}/#{CONFIG.redis_database}#?keepalive=true"))
|
||||
puts "Connected to Redis"
|
||||
|
||||
# Fiber
|
||||
spawn do
|
||||
loop do
|
||||
begin
|
||||
response = JSON.parse(TwAPI::Utils.validate_token)
|
||||
puts "#{"[INFO]".colorize(:green)} Token information:"
|
||||
puts "Client-Id:\t\t#{response["client_id"]}"
|
||||
puts "Login:\t\t\t#{response["login"]}"
|
||||
puts "Token exipires in:\t#{response["expires_in"]} (#{response["expires_in"] == 0 ? "Token does not expire" : Time.utc.shift(response["expires_in"].to_s.to_i, 0)})"
|
||||
rescue ex
|
||||
puts "#{"[ERROR]".colorize(:red)} #{ex.message}"
|
||||
end
|
||||
sleep 3600
|
||||
end
|
||||
end
|
||||
|
||||
TwAPI::Routes.register_misc
|
||||
TwAPI::Routes.register_all_twitch
|
||||
|
||||
|
@ -27,3 +44,4 @@ TwAPI::Routes.register_all_twitch
|
|||
{% end %}
|
||||
|
||||
Kemal.run
|
||||
Fiber.yield
|
|
@ -26,6 +26,11 @@ module TwAPI::Routes
|
|||
get "/v2" do
|
||||
{"message": "Welcome to v2, There is no docs!"}.to_json
|
||||
end
|
||||
|
||||
get "/twitch_expiration" do
|
||||
response = JSON.parse(TwAPI::Utils.validate_token)
|
||||
{"message": "Token exipires in: #{response["expires_in"]} (#{response["expires_in"] == 0 ? "Token does not expire" : Time.utc.shift(response["expires_in"].to_s.to_i, 0)})"}.to_json
|
||||
end
|
||||
end
|
||||
|
||||
def register_all_twitch
|
||||
|
|
|
@ -1,3 +1,19 @@
|
|||
module TwAPI::Utils
|
||||
extend self
|
||||
|
||||
def validate_token
|
||||
puts "#{"[INFO]".colorize(:green)} Validating twitch token..."
|
||||
headers = HTTP::Headers{
|
||||
"Content-Type" => "application/json",
|
||||
"Authorization" => "OAuth #{CONFIG.helixOAuth}",
|
||||
"Client-Id" => "#{CONFIG.helixClientID}",
|
||||
}
|
||||
response = HTTP::Client.get("https://id.twitch.tv/oauth2/validate", headers: headers)
|
||||
|
||||
if response.success?
|
||||
response.body
|
||||
else
|
||||
raise "Helix OAuth Token validation failed. #{response.status_code}: #{response.body}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue