Skip to content

Commit 4fe5427

Browse files
Create SSO "login" option (#259)
* Use provided tokens for SSO "login" Part of https://aptible.atlassian.net/browse/DP-144 Allow the user to store a given token for later use to enable SSO "logins" via the CLI. The SSO login process involves redirects that must be done in a browser. Therefore, the way to allow users in SSO enforced organizations to use the CLI is to provide a way for them to copy a token from their browser (where they complete their SSO login) and store it via the CLI for use. The command is `aptible login --sso token'. If no token is provided, the user will be prompted for one. This persisted token will overwrite their existing one. Therefore, they will need to issue `aptible login` again to access other orgs beyond the one the SSO "organization" token is valid for. * Bump version
1 parent 07bb06f commit 4fe5427

3 files changed

Lines changed: 47 additions & 1 deletion

File tree

lib/aptible/cli/agent.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'base64'
12
require 'uri'
23

34
require 'aptible/auth'
@@ -84,7 +85,22 @@ def version
8485
option :lifetime, desc: 'The duration the token should be valid for ' \
8586
'(example usage: 24h, 1d, 600s, etc.)'
8687
option :otp_token, desc: 'A token generated by your second-factor app'
88+
option :sso, desc: 'Use a token from a Single Sign On login on the ' \
89+
'dashboard'
8790
def login
91+
if options[:sso]
92+
begin
93+
token = options[:sso]
94+
token = ask('Paste token copied from Dashboard:') if token == 'sso'
95+
Base64.urlsafe_decode64(token.split('.').first)
96+
save_token(token)
97+
CLI.logger.info "Token written to #{token_file}"
98+
return
99+
rescue StandardError
100+
raise Thor::Error, 'Invalid token provided for SSO'
101+
end
102+
end
103+
88104
email = options[:email] || ask('Email: ')
89105
password = options[:password] || ask_then_line(
90106
'Password: ', echo: false

lib/aptible/cli/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Aptible
22
module CLI
3-
VERSION = '0.16.3'.freeze
3+
VERSION = '0.16.4'.freeze
44
end
55
end

spec/aptible/cli/agent_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,36 @@ def make_oauth2_error(code, ctx = nil)
258258
subject.login
259259
end
260260
end
261+
262+
context 'SSO logins' do
263+
let(:token) { 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzUxMiJ9.eyJpZCI6I' }
264+
265+
it 'accepts a token as an argument' do
266+
options = { sso: token }
267+
allow(subject).to receive(:options).and_return options
268+
269+
expect(subject).to receive(:save_token).with(token)
270+
271+
subject.login
272+
end
273+
274+
it 'rejects clearly invalid tokens' do
275+
options = { sso: 'blarg' }
276+
allow(subject).to receive(:options).and_return options
277+
278+
expect { subject.login }.to raise_error Thor::Error
279+
end
280+
281+
it 'prompts for a token if none provided' do
282+
options = { sso: 'sso' }
283+
allow(subject).to receive(:options).and_return options
284+
285+
expect(subject).to receive(:ask).once.and_return(token)
286+
expect(subject).to receive(:save_token).with(token)
287+
288+
subject.login
289+
end
290+
end
261291
end
262292
end
263293

0 commit comments

Comments
 (0)