Class: SmilyCli::Commands::AuthCommand
- Inherits:
-
BaseCommand
- Object
- Thor
- BaseCommand
- SmilyCli::Commands::AuthCommand
- 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
- #authorize_url ⇒ Object
- #client_credentials ⇒ Object
- #exchange ⇒ Object
- #refresh ⇒ Object
- #status ⇒ Object
- #token ⇒ Object
Methods inherited from BaseCommand
Instance Method Details
#authorize_url ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/smily_cli/commands/auth_command.rb', line 60 def url = context.oauth.( client_id: ["client_id"], redirect_uri: ["redirect_uri"], scope: ["scope"], state: ["state"], account_id: ["account_id"], response_type: ["response_type"] ) notice("Open this URL, approve the app, then use `smily auth exchange` with the returned code:") emit(url) end |
#client_credentials ⇒ Object
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: ["scope"] ) render_token(token, save_client_id: ["client_id"] || context.client_id, save_client_secret: ["client_secret"] || context.client_secret) end |
#exchange ⇒ Object
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.( client_id: require_option(:client_id, context.client_id, "client id"), client_secret: require_option(:client_secret, context.client_secret, "client secret"), code: ["code"], redirect_uri: ["redirect_uri"] ) render_token(token, save_client_id: ["client_id"] || context.client_id, save_client_secret: ["client_secret"] || context.client_secret) end |
#refresh ⇒ Object
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: ["redirect_uri"] ) render_token(token, save_client_id: ["client_id"] || context.client_id, save_client_secret: ["client_secret"] || context.client_secret) end |
#status ⇒ Object
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 ["check"] me = client.me.record || {} account = me["account"] || me["id"] success("Token is valid. Account: #{account}.") end |
#token ⇒ Object
111 112 113 114 |
# File 'lib/smily_cli/commands/auth_command.rb', line 111 def token value = context.token! emit(value) end |