-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#!/usr/bin/env ruby | ||
|
||
# | ||
# Call me this way: | ||
# $ VERBOSE=true gcloud_auth_check | ||
# | ||
|
||
require 'json' | ||
require 'date' | ||
#require 'rails' | ||
|
||
#include ActionView::Helpers::DateHelper | ||
|
||
# returns sth like: | ||
# { | ||
# "azp": "12345678901234.apps.googleusercontent.com", | ||
# "aud": "12345678901234.apps.googleusercontent.com", | ||
# "sub": "123456789012345678", | ||
# "scope": "https://www.googleapis.com/auth/accounts.reauth https://www.googleapis.com/auth/appengine.admin https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/compute https://www.googleapis.com/auth/sqlservice.login https://www.googleapis.com/auth/userinfo.email openid", | ||
# "exp": "1706020150", # probably a timestamp | ||
# "expires_in": "3405", # in how many seconds | ||
# "email": "ricc@google.com", # email | ||
# "email_verified": "true" | ||
# } | ||
|
||
VERBOSE = ENV.fetch('VERBOSE', "false").downcase == 'true' | ||
|
||
def ricc_time_ago(n_seconds) | ||
#ActionView::Helpers::DateHelper | ||
#ActionView::Helpers::DateHelper.time_ago_in_words(Time.now + (n_seconds).seconds) rescue "Err: #{$!}" | ||
"~#{n_seconds/60}m#{n_seconds - (n_seconds/60)*60}s" | ||
end | ||
|
||
def white(s) | ||
"\033[1;37m#{s}\033[0m" | ||
end | ||
|
||
|
||
def main(verbose: ) | ||
curl_ret = `curl https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=$(gcloud auth print-access-token) 2>/dev/null` | ||
ret = $? | ||
#puts "ret: #{ret} (class: #{ret.class})" | ||
if ret.success? | ||
#puts "curl_ret: #{curl_ret}" | ||
hash = JSON.load(curl_ret) | ||
#puts "hash: #{hash}" | ||
|
||
puts("📢📢📢 VERBOSE enabled, thanks! 📢📢📢") | ||
email = hash['email'] | ||
email_verified = hash['email_verified'] | ||
expires_in_seconds = hash['expires_in'].to_i | ||
exp = hash['exp'].to_i | ||
|
||
if verbose | ||
puts("📧 Email: #{email} (verified: #{email_verified})") | ||
puts("⏰ Expires in: #{ricc_time_ago expires_in_seconds} (#{expires_in_seconds}s)") | ||
puts("🕰️ Expires on: #{Time.at(exp).to_datetime} (#{exp})") | ||
puts("🕰️ Now: #{Time.now.to_datetime}") | ||
#puts("AGO: #{ricc_time_ago expires_in_seconds}") | ||
end | ||
puts("🟩 'gcloud' credentials for #{white email} seem valid for another #{white ricc_time_ago(expires_in_seconds)}") | ||
else | ||
puts("Something wrong with the CURL, maybe you are not authed correctly") | ||
end | ||
|
||
end | ||
|
||
|
||
main(verbose: VERBOSE) |