Class: FlycalCli::Auth
- Inherits:
-
Object
- Object
- FlycalCli::Auth
- Defined in:
- lib/flycal_cli/auth.rb
Constant Summary collapse
- SCOPE =
"https://www.googleapis.com/auth/calendar.readonly"- REDIRECT_PORT =
9292- REDIRECT_URI =
"http://127.0.0.1:#{REDIRECT_PORT}/oauth2callback"
Class Method Summary collapse
Class Method Details
.credentials ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/flycal_cli/auth.rb', line 15 def credentials return nil unless Config.credentials_exist? token_store = Google::Auth::Stores::FileTokenStore.new(file: Config.tokens_path) client_id = Google::Auth::ClientId.from_file(Config.credentials_path) = Google::Auth::UserAuthorizer.new( client_id, SCOPE, token_store, "/oauth2callback" ) creds = .get_credentials("flycal_user") return creds if creds nil end |
.logged_in? ⇒ Boolean
33 34 35 36 37 38 39 40 41 |
# File 'lib/flycal_cli/auth.rb', line 33 def logged_in? creds = credentials return false unless creds creds.fetch_access_token! true rescue Signet::AuthorizationError false end |
.login ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/flycal_cli/auth.rb', line 43 def login unless Config.credentials_exist? raise FlycalCli::Error, "Credentials file not found. Create #{Config.credentials_path} with OAuth credentials from Google Cloud Console.\n" \ "Go to: https://console.cloud.google.com/apis/credentials\n" \ "Create 'Desktop app' credentials and download the JSON as credentials.json" end token_store = Google::Auth::Stores::FileTokenStore.new(file: Config.tokens_path) client_id = Google::Auth::ClientId.from_file(Config.credentials_path) = Google::Auth::UserAuthorizer.new( client_id, SCOPE, token_store, "/oauth2callback" ) creds = .get_credentials("flycal_user") if creds creds.fetch_access_token! return creds end # Start local server to receive the auth code code = start_redirect_server() raise FlycalCli::Error, "Authentication cancelled or failed" if code.nil? || code.empty? .get_and_store_credentials_from_code( user_id: "flycal_user", code: code, base_url: "http://127.0.0.1:#{REDIRECT_PORT}" ) end |
.logout ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/flycal_cli/auth.rb', line 77 def logout return true unless File.exist?(Config.tokens_path) token_store = Google::Auth::Stores::FileTokenStore.new(file: Config.tokens_path) token_store.delete("flycal_user") File.delete(Config.tokens_path) if File.exist?(Config.tokens_path) true end |