Class: Flycal::Cli::Auth
- Inherits:
-
Object
- Object
- Flycal::Cli::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"- CLIENT_FLYCAL =
"flycal"- CLIENT_JSON =
"json"- USER_ID =
"flycal_user"- OAUTH_CLIENT_FILE =
File.("../../../config/oauth_client.json", __dir__)
Class Method Summary collapse
- .bundled_client_path ⇒ Object
- .client_id_for(mode) ⇒ Object
- .credentials ⇒ Object
- .logged_in? ⇒ Boolean
- .login(mode) ⇒ Object
- .logout ⇒ Object
-
.tokens_present? ⇒ Boolean
Google session file.
Class Method Details
.bundled_client_path ⇒ Object
100 101 102 |
# File 'lib/flycal/cli/auth.rb', line 100 def bundled_client_path OAUTH_CLIENT_FILE end |
.client_id_for(mode) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/flycal/cli/auth.rb', line 90 def client_id_for(mode) mode = normalize_mode(mode) path = client_id_path(mode) unless File.exist?(path) raise Flycal::Error, (mode, path) end Google::Auth::ClientId.from_file(path) end |
.credentials ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/flycal/cli/auth.rb', line 20 def credentials mode = resolved_mode return nil unless mode return nil unless File.exist?(Config.tokens_path) (mode).get_credentials(USER_ID) rescue Signet::AuthorizationError, Flycal::Error nil end |
.logged_in? ⇒ Boolean
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/flycal/cli/auth.rb', line 30 def logged_in? return false unless tokens_present? creds = credentials return false unless creds creds.fetch_access_token! true rescue Signet::AuthorizationError false end |
.login(mode) ⇒ Object
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 76 77 78 |
# File 'lib/flycal/cli/auth.rb', line 48 def login(mode) mode = normalize_mode(mode) if Config.auth_client && Config.auth_client != mode && File.exist?(Config.tokens_path) logout end Config.auth_client = mode = (mode) creds = .get_credentials(USER_ID) if creds begin creds.fetch_access_token! return creds rescue Signet::AuthorizationError token_store = Google::Auth::Stores::FileTokenStore.new(file: Config.tokens_path) token_store.delete(USER_ID) File.delete(Config.tokens_path) if File.exist?(Config.tokens_path) = (mode) end end code = start_redirect_server() raise Flycal::Error, Locale.t("login.cancelled") if code.nil? || code.empty? .get_and_store_credentials_from_code( user_id: USER_ID, code: code, base_url: "http://127.0.0.1:#{REDIRECT_PORT}" ) end |
.logout ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/flycal/cli/auth.rb', line 80 def logout if File.exist?(Config.tokens_path) token_store = Google::Auth::Stores::FileTokenStore.new(file: Config.tokens_path) token_store.delete(USER_ID) File.delete(Config.tokens_path) if File.exist?(Config.tokens_path) end Config.auth_client = nil true end |
.tokens_present? ⇒ Boolean
Google session file. config.yml is not a login signal (it survives uninstall).
43 44 45 46 |
# File 'lib/flycal/cli/auth.rb', line 43 def tokens_present? path = Config.tokens_path File.file?(path) && !File.size(path).to_i.zero? end |