Class: SmilyCli::Commands::AuthCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/smily_cli/commands/auth_command.rb

Overview

OAuth helpers: obtain, refresh, inspect and (optionally) persist access tokens. Client id/secret and refresh token resolve from flags, then env, then the active profile, so once a profile holds the app credentials the flags become optional.

Instance Method Summary collapse

Methods inherited from BaseCommand

exit_on_failure?

Instance Method Details

#authorize_urlObject



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/smily_cli/commands/auth_command.rb', line 60

def authorize_url
  url = context.oauth.authorize_url(
    client_id: options["client_id"],
    redirect_uri: options["redirect_uri"],
    scope: options["scope"],
    state: options["state"],
    account_id: options["account_id"],
    response_type: options["response_type"]
  )
  notice("Open this URL, approve the app, then use `smily auth exchange` with the returned code:")
  emit(url)
end

#client_credentialsObject



25
26
27
28
29
30
31
32
33
# File 'lib/smily_cli/commands/auth_command.rb', line 25

def client_credentials
  token = context.oauth.client_credentials(
    client_id: require_option(:client_id, context.client_id, "client id"),
    client_secret: require_option(:client_secret, context.client_secret, "client secret"),
    scope: options["scope"]
  )
  render_token(token, save_client_id: options["client_id"] || context.client_id,
                      save_client_secret: options["client_secret"] || context.client_secret)
end

#exchangeObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/smily_cli/commands/auth_command.rb', line 79

def exchange
  token = context.oauth.authorization_code(
    client_id: require_option(:client_id, context.client_id, "client id"),
    client_secret: require_option(:client_secret, context.client_secret, "client secret"),
    code: options["code"],
    redirect_uri: options["redirect_uri"]
  )
  render_token(token, save_client_id: options["client_id"] || context.client_id,
                      save_client_secret: options["client_secret"] || context.client_secret)
end

#refreshObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/smily_cli/commands/auth_command.rb', line 42

def refresh
  token = context.oauth.refresh(
    client_id: require_option(:client_id, context.client_id, "client id"),
    client_secret: require_option(:client_secret, context.client_secret, "client secret"),
    refresh_token: require_option(:refresh_token, context.refresh_token, "refresh token"),
    redirect_uri: options["redirect_uri"]
  )
  render_token(token, save_client_id: options["client_id"] || context.client_id,
                      save_client_secret: options["client_secret"] || context.client_secret)
end

#statusObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/smily_cli/commands/auth_command.rb', line 92

def status
  token = context.token
  if token.nil?
    notice(Color.yellow("No token configured for profile #{context.profile_name.inspect}."))
    notice("Obtain one with `smily auth client-credentials` or configure it via `smily config set token ...`.")
    return
  end

  notice("Profile : #{context.profile_name}")
  notice("Base URL: #{context.base_url}")
  notice("Token   : #{mask(token)}")
  return unless options["check"]

  me = client.me.record || {}
   = me["account"] || me["id"]
  success("Token is valid. Account: #{}.")
end

#tokenObject



111
112
113
114
# File 'lib/smily_cli/commands/auth_command.rb', line 111

def token
  value = context.token!
  emit(value)
end