Class: Flycal::Cli::Auth

Inherits:
Object
  • Object
show all
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.expand_path("../../../config/oauth_client.json", __dir__)

Class Method Summary collapse

Class Method Details

.bundled_client_pathObject



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, missing_client_message(mode, path)
  end

  Google::Auth::ClientId.from_file(path)
end

.credentialsObject



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)

  authorizer_for(mode).get_credentials(USER_ID)
rescue Signet::AuthorizationError, Flycal::Error
  nil
end

.logged_in?Boolean

Returns:

  • (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

Raises:



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 (mode)
  mode = normalize_mode(mode)
  if Config.auth_client && Config.auth_client != mode && File.exist?(Config.tokens_path)
    logout
  end

  Config.auth_client = mode
  authorizer = authorizer_for(mode)

  creds = authorizer.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)
      authorizer = authorizer_for(mode)
    end
  end

  code = start_redirect_server(authorizer)
  raise Flycal::Error, Locale.t("login.cancelled") if code.nil? || code.empty?

  authorizer.get_and_store_credentials_from_code(
    user_id: USER_ID,
    code: code,
    base_url: "http://127.0.0.1:#{REDIRECT_PORT}"
  )
end

.logoutObject



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).

Returns:

  • (Boolean)


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