From f5d9930d54899824f6aafef58c912a6a2d234c7d Mon Sep 17 00:00:00 2001 From: Roger Oba Date: Sat, 10 Apr 2021 20:08:22 -0300 Subject: [PATCH 01/15] Add --exports_to_clipboard option to spaceauth. --- spaceship/lib/spaceship/commands_generator.rb | 3 ++- spaceship/lib/spaceship/spaceauth_runner.rb | 13 +++++++---- spaceship/spec/spaceauth_spec.rb | 23 +++++++++++++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/spaceship/lib/spaceship/commands_generator.rb b/spaceship/lib/spaceship/commands_generator.rb index 9ea1ba4874d..3d9c5f8bdbd 100644 --- a/spaceship/lib/spaceship/commands_generator.rb +++ b/spaceship/lib/spaceship/commands_generator.rb @@ -39,9 +39,10 @@ def run command :spaceauth do |c| c.syntax = 'fastlane spaceship spaceauth' c.description = 'Authentication helper for spaceship/fastlane to work with Apple 2-Step/2FA' + c.option('--exports_to_clipboard', 'Whether the export method should be copied to clipboard. When using this option, the session can be exported to an env var by calling `eval $(pbpaste)`') c.action do |args, options| - Spaceship::SpaceauthRunner.new(username: options.user).run + Spaceship::SpaceauthRunner.new(username: options.user, exports_to_clipboard: options.exports_to_clipboard).run end end diff --git a/spaceship/lib/spaceship/spaceauth_runner.rb b/spaceship/lib/spaceship/spaceauth_runner.rb index b5f3772d737..12c4c651e05 100644 --- a/spaceship/lib/spaceship/spaceauth_runner.rb +++ b/spaceship/lib/spaceship/spaceauth_runner.rb @@ -6,10 +6,11 @@ module Spaceship class SpaceauthRunner - def initialize(username: nil) + def initialize(username: nil, exports_to_clipboard: nil) @username = username @username ||= CredentialsManager::AppfileConfig.try_fetch_value(:apple_id) @username ||= ask("Username: ") + @exports_to_clipboard = exports_to_clipboard end def run @@ -22,7 +23,7 @@ def run puts("Could not login to App Store Connect".red) puts("Please check your credentials and try again.".yellow) puts("This could be an issue with App Store Connect,".yellow) - puts("Please try unsetting the FASTLANE_SESSION environment variable".yellow) + puts("Please try unsetting the FASTLANE_SESSION environment variable by calling 'unset FASTLANE_SESSION'".yellow) puts("(if it is set) and re-run `fastlane spaceauth`".yellow) puts("") puts("Exception type: #{ex.class}") @@ -50,6 +51,7 @@ def run end yaml = cookies.to_yaml.gsub("\n", "\\n") + export_command = "export FASTLANE_SESSION='#{yaml}'" puts("---") puts("") @@ -58,9 +60,12 @@ def run puts("") puts("") puts("Example:") - puts("export FASTLANE_SESSION='#{yaml}'".cyan.underline) + puts(export_command.cyan.underline) - if mac? && Spaceship::Client::UserInterface.interactive? && agree("🙄 Should fastlane copy the cookie into your clipboard, so you can easily paste it? (y/n)", true) + if @exports_to_clipboard + require 'open3' + Open3.popen3('pbcopy') { |input, _, _| input << export_command } + elsif mac? && Spaceship::Client::UserInterface.interactive? && agree("🙄 Should fastlane copy the cookie into your clipboard, so you can easily paste it? (y/n)", true) require 'open3' Open3.popen3('pbcopy') { |input, _, _| input << yaml } puts("Successfully copied text into your clipboard 🎨".green) diff --git a/spaceship/spec/spaceauth_spec.rb b/spaceship/spec/spaceauth_spec.rb index ec064288320..8e4cf79718b 100644 --- a/spaceship/spec/spaceauth_spec.rb +++ b/spaceship/spec/spaceauth_spec.rb @@ -13,4 +13,27 @@ Spaceship::SpaceauthRunner.new.run end.to output(/export FASTLANE_SESSION=.*name: DES.*name: myacinfo.*name: dqsid.*/).to_stdout end + + describe 'exports_to_clipboard option' do + before :each do + # Save clipboard + @clipboard = `pbpaste` + end + + after :each do + # Restore clipboard + require 'open3' + Open3.popen3('pbcopy') { |input, _, _| input << @clipboard } + end + + it 'when true, it should copy "export FASTLANE_SESSION=…" command to clipboard' do + Spaceship::SpaceauthRunner.new(exports_to_clipboard: true).run + expect(`pbpaste`).to match(/export FASTLANE_SESSION=.*/) + end + + it 'when false, it should not copy "export FASTLANE_SESSION=…" command to clipboard' do + Spaceship::SpaceauthRunner.new(exports_to_clipboard: false).run + expect(`pbpaste`).to eq(@clipboard) + end + end end From ae5476061e017ddaaffedc516b51b98dea29dcd1 Mon Sep 17 00:00:00 2001 From: Roger Oba Date: Sat, 10 Apr 2021 20:08:34 -0300 Subject: [PATCH 02/15] Add documentation for exports_to_clipboard option. --- spaceship/README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spaceship/README.md b/spaceship/README.md index d32f47b30f9..925f34cd513 100644 --- a/spaceship/README.md +++ b/spaceship/README.md @@ -123,6 +123,14 @@ This will authenticate you and provide a string that can be transferred to your export FASTLANE_SESSION='---\n- !ruby/object:HTTP::Cookie\n name: DES5c148586dfd451e55afbaaa5f62418f91\n value: HSARMTKNSRVTWFla1+yO4gVPowH17VaaaxPFnUdMUegQZxqy1Ie1c2v6bM1vSOzIbuOmrl/FNenlScsd/NbF7/Lw4cpnL15jsyg0TOJwP32tC/NguPiyOaaaU+jrj4tf4uKdIywVaaaFSRVT\n domain: idmsa.apple.com\n for_domain: true\n path: "/"\n secure: true\n httponly: true\n expires: 2016-04-27 23:55:56.000000000 Z\n max_age: \n created_at: 2016-03-28 16:55:57.032086000 -07:00\n accessed_at: 2016-03-28 19:11:17.828141000 -07:00\n' ``` +Alternatively, pass in the `--exports_to_clipboard` option so that the export code is automatically copied to clipboard, which you can then execute by running `eval $(pbpaste)` right after running `spaceauth`, for instance: + +```sh +fastlane spaceauth -u user@example.org --exports_to_clipboard && eval $(pbpaste) +``` + +This is specially helpful in a CI environment. + #### Transporter See [Continuous Integration > Authentication with Apple services > Application specific passwords](https://docs.fastlane.tools/best-practices/continuous-integration/#application-specific-passwords) From 42c0dc0faa0b336e2a28c061b7d2b236c7e7f7c6 Mon Sep 17 00:00:00 2001 From: Roger Oba Date: Sun, 11 Apr 2021 14:26:22 -0300 Subject: [PATCH 03/15] Add FastlaneCore::Clipboard utility. --- fastlane/lib/fastlane/actions/clipboard.rb | 5 +--- fastlane_core/lib/fastlane_core/clipboard.rb | 15 +++++++++++ fastlane_core/spec/clipboard_spec.rb | 26 ++++++++++++++++++++ spaceship/lib/spaceship/spaceauth_runner.rb | 9 +++---- spaceship/spec/spaceauth_spec.rb | 12 ++++----- 5 files changed, 52 insertions(+), 15 deletions(-) create mode 100644 fastlane_core/lib/fastlane_core/clipboard.rb create mode 100644 fastlane_core/spec/clipboard_spec.rb diff --git a/fastlane/lib/fastlane/actions/clipboard.rb b/fastlane/lib/fastlane/actions/clipboard.rb index eb4d77b2975..a4709dbcb67 100644 --- a/fastlane/lib/fastlane/actions/clipboard.rb +++ b/fastlane/lib/fastlane/actions/clipboard.rb @@ -7,10 +7,7 @@ def self.run(params) truncated_value = value[0..800].gsub(/\s\w+\s*$/, '...') UI.message("Storing '#{truncated_value}' in the clipboard 🎨") - if FastlaneCore::Helper.mac? - require 'open3' - Open3.popen3('pbcopy') { |input, _, _| input << value } - end + FastlaneCore::Clipboard.copy(content: value) end ##################################################### diff --git a/fastlane_core/lib/fastlane_core/clipboard.rb b/fastlane_core/lib/fastlane_core/clipboard.rb new file mode 100644 index 00000000000..41494b784f7 --- /dev/null +++ b/fastlane_core/lib/fastlane_core/clipboard.rb @@ -0,0 +1,15 @@ +require 'open3' + +module FastlaneCore + class Clipboard + def self.copy(content: nil) + return UI.error!("Clipboard.copy is only supported in macOS environment.") if !Helper.mac? + Open3.popen3('pbcopy') { |input, _, _| input << content } + end + + def self.paste + return UI.error!("Clipboard.paste is only supported in macOS environment.") if !Helper.mac? + return `pbpaste` + end + end +end diff --git a/fastlane_core/spec/clipboard_spec.rb b/fastlane_core/spec/clipboard_spec.rb new file mode 100644 index 00000000000..477e61e1201 --- /dev/null +++ b/fastlane_core/spec/clipboard_spec.rb @@ -0,0 +1,26 @@ +require 'fastlane_core/lib/fastlane_core/clipboard' + +describe FastlaneCore do + describe FastlaneCore::Clipboard do + + describe '#copy and paste' do + it 'should work on macOS environment', :if => FastlaneCore::Helper.mac? do + # Save clipboard + clipboard = FastlaneCore::Clipboard.paste + + # Test copy and paste + FastlaneCore::Clipboard.copy(content: "_fastlane_ is awesome") + expect(FastlaneCore::Clipboard.paste).to eq("_fastlane_ is awesome") + + # Restore clipboard + FastlaneCore::Clipboard.copy(content: clipboard) + expect(FastlaneCore::Clipboard.paste).to eq(clipboard) + end + + it 'should throw on non-macOS environment', :if => !FastlaneCore::Helper.mac? do + expect { FastlaneCore::Clipboard.copy(content: "_fastlane_ is awesome") }.to raise_error("Clipboard.copy is only supported in macOS environment.") + expect { FastlaneCore::Clipboard.paste }.to raise_error("Clipboard.paste is only supported in macOS environment.") + end + end + end +end diff --git a/spaceship/lib/spaceship/spaceauth_runner.rb b/spaceship/lib/spaceship/spaceauth_runner.rb index 12c4c651e05..3d30bb3a79e 100644 --- a/spaceship/lib/spaceship/spaceauth_runner.rb +++ b/spaceship/lib/spaceship/spaceauth_runner.rb @@ -63,12 +63,11 @@ def run puts(export_command.cyan.underline) if @exports_to_clipboard - require 'open3' - Open3.popen3('pbcopy') { |input, _, _| input << export_command } + FastlaneCore::Clipboard.copy(content: export_command) + UI.success("Successfully copied export command into your clipboard 🎨") elsif mac? && Spaceship::Client::UserInterface.interactive? && agree("🙄 Should fastlane copy the cookie into your clipboard, so you can easily paste it? (y/n)", true) - require 'open3' - Open3.popen3('pbcopy') { |input, _, _| input << yaml } - puts("Successfully copied text into your clipboard 🎨".green) + FastlaneCore::Clipboard.copy(content: yaml) + UI.success("Successfully copied text into your clipboard 🎨") end end diff --git a/spaceship/spec/spaceauth_spec.rb b/spaceship/spec/spaceauth_spec.rb index 8e4cf79718b..26a81d8b599 100644 --- a/spaceship/spec/spaceauth_spec.rb +++ b/spaceship/spec/spaceauth_spec.rb @@ -1,4 +1,5 @@ require_relative 'tunes/tunes_stubbing' +require 'fastlane_core/clipboard' describe Spaceship::SpaceauthRunner do let(:user_cookie) { TunesStubbing.itc_read_fixture_file('spaceauth_cookie.yml') } @@ -14,26 +15,25 @@ end.to output(/export FASTLANE_SESSION=.*name: DES.*name: myacinfo.*name: dqsid.*/).to_stdout end - describe 'exports_to_clipboard option' do + describe 'exports_to_clipboard option', :if => FastlaneCore::Helper.mac? do before :each do # Save clipboard - @clipboard = `pbpaste` + @clipboard = FastlaneCore::Clipboard.paste end after :each do # Restore clipboard - require 'open3' - Open3.popen3('pbcopy') { |input, _, _| input << @clipboard } + FastlaneCore::Clipboard.copy(content: @clipboard) end it 'when true, it should copy "export FASTLANE_SESSION=…" command to clipboard' do Spaceship::SpaceauthRunner.new(exports_to_clipboard: true).run - expect(`pbpaste`).to match(/export FASTLANE_SESSION=.*/) + expect(FastlaneCore::Clipboard.paste).to match(/export FASTLANE_SESSION=.*/) end it 'when false, it should not copy "export FASTLANE_SESSION=…" command to clipboard' do Spaceship::SpaceauthRunner.new(exports_to_clipboard: false).run - expect(`pbpaste`).to eq(@clipboard) + expect(FastlaneCore::Clipboard.paste).to eq(@clipboard) end end end From 4b14cefc0f3c281d3c9c916d74989f9ca2ea4a72 Mon Sep 17 00:00:00 2001 From: Roger Oba Date: Sun, 11 Apr 2021 14:26:42 -0300 Subject: [PATCH 04/15] Fix clipboard action platform availability. --- fastlane/lib/fastlane/actions/clipboard.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastlane/lib/fastlane/actions/clipboard.rb b/fastlane/lib/fastlane/actions/clipboard.rb index a4709dbcb67..b63df191310 100644 --- a/fastlane/lib/fastlane/actions/clipboard.rb +++ b/fastlane/lib/fastlane/actions/clipboard.rb @@ -31,7 +31,7 @@ def self.authors end def self.is_supported?(platform) - true + return Helper.mac? end def self.example_code From aef508d29b91a33474de1c4618330d759efe8156 Mon Sep 17 00:00:00 2001 From: Roger Oba Date: Sun, 11 Apr 2021 16:07:56 -0300 Subject: [PATCH 05/15] Make clipboard be more resilient on unsupported OS. --- fastlane/lib/fastlane/actions/clipboard.rb | 4 +- fastlane_core/lib/fastlane_core.rb | 43 ++++++++++---------- fastlane_core/lib/fastlane_core/clipboard.rb | 8 +++- fastlane_core/spec/clipboard_spec.rb | 19 +++++---- spaceship/lib/spaceship/spaceauth_runner.rb | 5 ++- spaceship/spec/spaceauth_spec.rb | 2 +- 6 files changed, 44 insertions(+), 37 deletions(-) diff --git a/fastlane/lib/fastlane/actions/clipboard.rb b/fastlane/lib/fastlane/actions/clipboard.rb index b63df191310..2922a4054cc 100644 --- a/fastlane/lib/fastlane/actions/clipboard.rb +++ b/fastlane/lib/fastlane/actions/clipboard.rb @@ -27,11 +27,11 @@ def self.available_options end def self.authors - ["KrauseFx", "joshdholtz"] + ["KrauseFx", "joshdholtz", "rogerluan"] end def self.is_supported?(platform) - return Helper.mac? + return FastlaneCore::Clipboard.is_supported? end def self.example_code diff --git a/fastlane_core/lib/fastlane_core.rb b/fastlane_core/lib/fastlane_core.rb index fc14e561d1e..ae3a8dd5caf 100644 --- a/fastlane_core/lib/fastlane_core.rb +++ b/fastlane_core/lib/fastlane_core.rb @@ -3,39 +3,40 @@ require_relative 'fastlane_core/core_ext/string' require_relative 'fastlane_core/core_ext/shellwords' +require_relative 'fastlane_core/analytics/action_completion_context' +require_relative 'fastlane_core/analytics/action_launch_context' +require_relative 'fastlane_core/analytics/analytics_event_builder' +require_relative 'fastlane_core/analytics/analytics_ingester_client' +require_relative 'fastlane_core/analytics/analytics_session' +require_relative 'fastlane_core/build_watcher' +require_relative 'fastlane_core/cert_checker' +require_relative 'fastlane_core/clipboard' +require_relative 'fastlane_core/command_executor' +require_relative 'fastlane_core/configuration/configuration' +require_relative 'fastlane_core/device_manager' require_relative 'fastlane_core/env' +require_relative 'fastlane_core/fastlane_folder' +require_relative 'fastlane_core/fastlane_pty' require_relative 'fastlane_core/feature/feature' require_relative 'fastlane_core/features' require_relative 'fastlane_core/helper' -require_relative 'fastlane_core/configuration/configuration' -require_relative 'fastlane_core/update_checker/update_checker' -require_relative 'fastlane_core/languages' -require_relative 'fastlane_core/cert_checker' require_relative 'fastlane_core/ipa_file_analyser' +require_relative 'fastlane_core/ipa_upload_package_builder' require_relative 'fastlane_core/itunes_transporter' -require_relative 'fastlane_core/provisioning_profile' +require_relative 'fastlane_core/keychain_importer' +require_relative 'fastlane_core/languages' require_relative 'fastlane_core/pkg_file_analyser' require_relative 'fastlane_core/pkg_upload_package_builder' -require_relative 'fastlane_core/command_executor' -require_relative 'fastlane_core/ipa_upload_package_builder' require_relative 'fastlane_core/print_table' require_relative 'fastlane_core/project' -require_relative 'fastlane_core/device_manager' -require_relative 'fastlane_core/ui/ui' -require_relative 'fastlane_core/fastlane_folder' -require_relative 'fastlane_core/keychain_importer' +require_relative 'fastlane_core/provisioning_profile' +require_relative 'fastlane_core/queue_worker' require_relative 'fastlane_core/swag' -require_relative 'fastlane_core/build_watcher' -require_relative 'fastlane_core/ui/errors' -require_relative 'fastlane_core/test_parser' -require_relative 'fastlane_core/analytics/action_completion_context' -require_relative 'fastlane_core/analytics/action_launch_context' -require_relative 'fastlane_core/analytics/analytics_event_builder' -require_relative 'fastlane_core/analytics/analytics_ingester_client' -require_relative 'fastlane_core/analytics/analytics_session' require_relative 'fastlane_core/tag_version' -require_relative 'fastlane_core/fastlane_pty' -require_relative 'fastlane_core/queue_worker' +require_relative 'fastlane_core/test_parser' +require_relative 'fastlane_core/ui/errors' +require_relative 'fastlane_core/ui/ui' +require_relative 'fastlane_core/update_checker/update_checker' # Third Party code require 'colored' diff --git a/fastlane_core/lib/fastlane_core/clipboard.rb b/fastlane_core/lib/fastlane_core/clipboard.rb index 41494b784f7..ad5c6b249a5 100644 --- a/fastlane_core/lib/fastlane_core/clipboard.rb +++ b/fastlane_core/lib/fastlane_core/clipboard.rb @@ -3,13 +3,17 @@ module FastlaneCore class Clipboard def self.copy(content: nil) - return UI.error!("Clipboard.copy is only supported in macOS environment.") if !Helper.mac? + return UI.crash!("'pbcopy' or 'pbpaste' command not found.") unless is_supported? Open3.popen3('pbcopy') { |input, _, _| input << content } end def self.paste - return UI.error!("Clipboard.paste is only supported in macOS environment.") if !Helper.mac? + return UI.crash!("'pbcopy' or 'pbpaste' command not found.") unless is_supported? return `pbpaste` end + + def self.is_supported? + return `which pbcopy`.length > 0 && `which pbpaste`.length > 0 + end end end diff --git a/fastlane_core/spec/clipboard_spec.rb b/fastlane_core/spec/clipboard_spec.rb index 477e61e1201..483a720b64c 100644 --- a/fastlane_core/spec/clipboard_spec.rb +++ b/fastlane_core/spec/clipboard_spec.rb @@ -1,25 +1,26 @@ -require 'fastlane_core/lib/fastlane_core/clipboard' - describe FastlaneCore do describe FastlaneCore::Clipboard do - describe '#copy and paste' do - it 'should work on macOS environment', :if => FastlaneCore::Helper.mac? do + before(:each) do + @test_message = "_fastlane_ is awesome" + end + + it 'should work on supported environments', if: FastlaneCore::Clipboard.is_supported? do # Save clipboard clipboard = FastlaneCore::Clipboard.paste # Test copy and paste - FastlaneCore::Clipboard.copy(content: "_fastlane_ is awesome") - expect(FastlaneCore::Clipboard.paste).to eq("_fastlane_ is awesome") + FastlaneCore::Clipboard.copy(content: @test_message) + expect(FastlaneCore::Clipboard.paste).to eq(@test_message) # Restore clipboard FastlaneCore::Clipboard.copy(content: clipboard) expect(FastlaneCore::Clipboard.paste).to eq(clipboard) end - it 'should throw on non-macOS environment', :if => !FastlaneCore::Helper.mac? do - expect { FastlaneCore::Clipboard.copy(content: "_fastlane_ is awesome") }.to raise_error("Clipboard.copy is only supported in macOS environment.") - expect { FastlaneCore::Clipboard.paste }.to raise_error("Clipboard.paste is only supported in macOS environment.") + it 'should throw on non-supported environment', if: !FastlaneCore::Clipboard.is_supported? do + expect { FastlaneCore::Clipboard.copy(content: @test_message) }.to raise_error("'pbcopy' or 'pbpaste' command not found.") + expect { FastlaneCore::Clipboard.paste }.to raise_error("'pbcopy' or 'pbpaste' command not found.") end end end diff --git a/spaceship/lib/spaceship/spaceauth_runner.rb b/spaceship/lib/spaceship/spaceauth_runner.rb index 3d30bb3a79e..160733f26a5 100644 --- a/spaceship/lib/spaceship/spaceauth_runner.rb +++ b/spaceship/lib/spaceship/spaceauth_runner.rb @@ -1,6 +1,7 @@ require 'colored' require 'credentials_manager/appfile_config' require 'yaml' +require 'fastlane_core' require_relative 'tunes/tunes_client' @@ -64,10 +65,10 @@ def run if @exports_to_clipboard FastlaneCore::Clipboard.copy(content: export_command) - UI.success("Successfully copied export command into your clipboard 🎨") + puts("Successfully copied export command into your clipboard 🎨".green) elsif mac? && Spaceship::Client::UserInterface.interactive? && agree("🙄 Should fastlane copy the cookie into your clipboard, so you can easily paste it? (y/n)", true) FastlaneCore::Clipboard.copy(content: yaml) - UI.success("Successfully copied text into your clipboard 🎨") + puts("Successfully copied text into your clipboard 🎨".green) end end diff --git a/spaceship/spec/spaceauth_spec.rb b/spaceship/spec/spaceauth_spec.rb index 26a81d8b599..e86bee0a90e 100644 --- a/spaceship/spec/spaceauth_spec.rb +++ b/spaceship/spec/spaceauth_spec.rb @@ -15,7 +15,7 @@ end.to output(/export FASTLANE_SESSION=.*name: DES.*name: myacinfo.*name: dqsid.*/).to_stdout end - describe 'exports_to_clipboard option', :if => FastlaneCore::Helper.mac? do + describe 'exports_to_clipboard option', if: FastlaneCore::Clipboard.is_supported? do before :each do # Save clipboard @clipboard = FastlaneCore::Clipboard.paste From b2b214f4a6d2450bdefff4dd36d50df79471b51a Mon Sep 17 00:00:00 2001 From: Roger Oba Date: Sun, 11 Apr 2021 16:08:14 -0300 Subject: [PATCH 06/15] Document exports_to_clipboard OS limitations. --- spaceship/README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spaceship/README.md b/spaceship/README.md index 925f34cd513..b4a310ac722 100644 --- a/spaceship/README.md +++ b/spaceship/README.md @@ -131,6 +131,20 @@ fastlane spaceauth -u user@example.org --exports_to_clipboard && eval $(pbpaste) This is specially helpful in a CI environment. +Note: `--exports_to_clipboard` uses `pbcopy`, and both `pbcopy` and `pbpaste` are only available in macOS. If you use a different OS, make sure you create an alias to `pbcopy` and `pbpaste`, such as: + +```sh +# On Linux: + +alias pbcopy='xsel --clipboard --input' +alias pbpaste='xsel --clipboard --output' + +# or + +alias pbcopy='xclip -selection clipboard' +alias pbpaste='xclip -selection clipboard -o' +``` + #### Transporter See [Continuous Integration > Authentication with Apple services > Application specific passwords](https://docs.fastlane.tools/best-practices/continuous-integration/#application-specific-passwords) From 07acc4169538b5e8fed28f5a330a33a771104695 Mon Sep 17 00:00:00 2001 From: Roger Oba Date: Sun, 11 Apr 2021 16:34:34 -0300 Subject: [PATCH 07/15] Add missing require. --- fastlane_core/lib/fastlane_core/clipboard.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/fastlane_core/lib/fastlane_core/clipboard.rb b/fastlane_core/lib/fastlane_core/clipboard.rb index ad5c6b249a5..6f6dea19447 100644 --- a/fastlane_core/lib/fastlane_core/clipboard.rb +++ b/fastlane_core/lib/fastlane_core/clipboard.rb @@ -1,3 +1,4 @@ +require 'fastlane_core' require 'open3' module FastlaneCore From bbf9c332f0d64e749afe94077777ef566a9dc1b4 Mon Sep 17 00:00:00 2001 From: Roger Oba Date: Mon, 12 Apr 2021 09:09:09 -0300 Subject: [PATCH 08/15] Export only the session itself. --- spaceship/lib/spaceship/spaceauth_runner.rb | 16 ++++++---------- spaceship/spec/spaceauth_spec.rb | 6 +++--- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/spaceship/lib/spaceship/spaceauth_runner.rb b/spaceship/lib/spaceship/spaceauth_runner.rb index 160733f26a5..060b75f295f 100644 --- a/spaceship/lib/spaceship/spaceauth_runner.rb +++ b/spaceship/lib/spaceship/spaceauth_runner.rb @@ -51,24 +51,20 @@ def run cookie.name.start_with?("myacinfo") || cookie.name == "dqsid" || cookie.name.start_with?("DES") end - yaml = cookies.to_yaml.gsub("\n", "\\n") - export_command = "export FASTLANE_SESSION='#{yaml}'" + @yaml = cookies.to_yaml.gsub("\n", "\\n") puts("---") puts("") puts("Pass the following via the FASTLANE_SESSION environment variable:") - puts(yaml.cyan.underline) + puts(@yaml.cyan.underline) puts("") puts("") puts("Example:") - puts(export_command.cyan.underline) + puts("export FASTLANE_SESSION='#{@yaml}'".cyan.underline) - if @exports_to_clipboard - FastlaneCore::Clipboard.copy(content: export_command) - puts("Successfully copied export command into your clipboard 🎨".green) - elsif mac? && Spaceship::Client::UserInterface.interactive? && agree("🙄 Should fastlane copy the cookie into your clipboard, so you can easily paste it? (y/n)", true) - FastlaneCore::Clipboard.copy(content: yaml) - puts("Successfully copied text into your clipboard 🎨".green) + if @exports_to_clipboard || (mac? && Spaceship::Client::UserInterface.interactive? && agree("🙄 Should fastlane copy the cookie into your clipboard, so you can easily paste it? (y/n)", true)) + FastlaneCore::Clipboard.copy(content: @yaml) + puts("Successfully copied the session string into your clipboard 🎨".green) end end diff --git a/spaceship/spec/spaceauth_spec.rb b/spaceship/spec/spaceauth_spec.rb index e86bee0a90e..fda8014845b 100644 --- a/spaceship/spec/spaceauth_spec.rb +++ b/spaceship/spec/spaceauth_spec.rb @@ -26,12 +26,12 @@ FastlaneCore::Clipboard.copy(content: @clipboard) end - it 'when true, it should copy "export FASTLANE_SESSION=…" command to clipboard' do + it 'when true, it should copy the session to clipboard' do Spaceship::SpaceauthRunner.new(exports_to_clipboard: true).run - expect(FastlaneCore::Clipboard.paste).to match(/export FASTLANE_SESSION=.*/) + expect(FastlaneCore::Clipboard.paste).to match(%r{.*domain: idmsa.apple.com.*path: \"\/appleauth\/auth\/\".*}) end - it 'when false, it should not copy "export FASTLANE_SESSION=…" command to clipboard' do + it 'when false, it should not copy the session to clipboard' do Spaceship::SpaceauthRunner.new(exports_to_clipboard: false).run expect(FastlaneCore::Clipboard.paste).to eq(@clipboard) end From 203c687fdbeb986a1a0fc132b67c0507e63f05fe Mon Sep 17 00:00:00 2001 From: Roger Oba Date: Mon, 12 Apr 2021 09:09:39 -0300 Subject: [PATCH 09/15] Add #session_string method to SpaceauthRunner. --- spaceship/lib/spaceship/spaceauth_runner.rb | 7 +++++++ spaceship/spec/spaceauth_spec.rb | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/spaceship/lib/spaceship/spaceauth_runner.rb b/spaceship/lib/spaceship/spaceauth_runner.rb index 060b75f295f..645aa4aab03 100644 --- a/spaceship/lib/spaceship/spaceauth_runner.rb +++ b/spaceship/lib/spaceship/spaceauth_runner.rb @@ -66,6 +66,13 @@ def run FastlaneCore::Clipboard.copy(content: @yaml) puts("Successfully copied the session string into your clipboard 🎨".green) end + + return self + end + + def session_string + FastlaneCore::UI.user_error!("`#{__method__}` method called before calling `run` in `SpaceauthRunner`") unless @yaml + @yaml end def mac? diff --git a/spaceship/spec/spaceauth_spec.rb b/spaceship/spec/spaceauth_spec.rb index fda8014845b..cdd257e6495 100644 --- a/spaceship/spec/spaceauth_spec.rb +++ b/spaceship/spec/spaceauth_spec.rb @@ -36,4 +36,17 @@ expect(FastlaneCore::Clipboard.paste).to eq(@clipboard) end end + + describe '#session_string' do + it 'should return the session when called after run' do + expect(Spaceship::SpaceauthRunner.new.run.session_string).to match(%r{.*domain: idmsa.apple.com.*path: \"\/appleauth\/auth\/\".*}) + end + + it 'should throw when called before run' do + expect(FastlaneCore::UI).to receive(:user_error!).with(/method called before calling `run` in `SpaceauthRunner`/).and_raise("boom") + expect do + Spaceship::SpaceauthRunner.new.session_string + end.to raise_error("boom") + end + end end From 20f3d8ad2cce2da3d7b6b12585bea87408dcf7d5 Mon Sep 17 00:00:00 2001 From: Roger Oba Date: Mon, 12 Apr 2021 09:14:56 -0300 Subject: [PATCH 10/15] Fix "Application-specific passwords" external link. --- spaceship/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spaceship/README.md b/spaceship/README.md index b4a310ac722..d46a9ae75ae 100644 --- a/spaceship/README.md +++ b/spaceship/README.md @@ -147,7 +147,7 @@ alias pbpaste='xclip -selection clipboard -o' #### Transporter -See [Continuous Integration > Authentication with Apple services > Application specific passwords](https://docs.fastlane.tools/best-practices/continuous-integration/#application-specific-passwords) +See [Continuous Integration > Authenticating with Apple services > Method 3: Application-specific passwords](https://docs.fastlane.tools/best-practices/continuous-integration/#method-3-application-specific-passwords) ## _spaceship_ in use From 88ce4906c7cccba4869628d0b96afa53f78c97f3 Mon Sep 17 00:00:00 2001 From: Roger Oba Date: Mon, 12 Apr 2021 09:15:22 -0300 Subject: [PATCH 11/15] Redirect official docs instead of duplicating it. --- spaceship/README.md | 34 +--------------------------------- 1 file changed, 1 insertion(+), 33 deletions(-) diff --git a/spaceship/README.md b/spaceship/README.md index d46a9ae75ae..d3d54a914c1 100644 --- a/spaceship/README.md +++ b/spaceship/README.md @@ -111,39 +111,7 @@ When your Apple account has 2 factor verification enabled, you'll automatically #### Web sessions -To generate a web session for your CI machine, use - -```sh -fastlane spaceauth -u user@example.org -``` - -This will authenticate you and provide a string that can be transferred to your CI system. Copy everything from `---\n` to your CI server and provide it as environment variable named `FASTLANE_SESSION`. For example: - -``` -export FASTLANE_SESSION='---\n- !ruby/object:HTTP::Cookie\n name: DES5c148586dfd451e55afbaaa5f62418f91\n value: HSARMTKNSRVTWFla1+yO4gVPowH17VaaaxPFnUdMUegQZxqy1Ie1c2v6bM1vSOzIbuOmrl/FNenlScsd/NbF7/Lw4cpnL15jsyg0TOJwP32tC/NguPiyOaaaU+jrj4tf4uKdIywVaaaFSRVT\n domain: idmsa.apple.com\n for_domain: true\n path: "/"\n secure: true\n httponly: true\n expires: 2016-04-27 23:55:56.000000000 Z\n max_age: \n created_at: 2016-03-28 16:55:57.032086000 -07:00\n accessed_at: 2016-03-28 19:11:17.828141000 -07:00\n' -``` - -Alternatively, pass in the `--exports_to_clipboard` option so that the export code is automatically copied to clipboard, which you can then execute by running `eval $(pbpaste)` right after running `spaceauth`, for instance: - -```sh -fastlane spaceauth -u user@example.org --exports_to_clipboard && eval $(pbpaste) -``` - -This is specially helpful in a CI environment. - -Note: `--exports_to_clipboard` uses `pbcopy`, and both `pbcopy` and `pbpaste` are only available in macOS. If you use a different OS, make sure you create an alias to `pbcopy` and `pbpaste`, such as: - -```sh -# On Linux: - -alias pbcopy='xsel --clipboard --input' -alias pbpaste='xsel --clipboard --output' - -# or - -alias pbcopy='xclip -selection clipboard' -alias pbpaste='xclip -selection clipboard -o' -``` +See [Continuous Integration > Authenticating with Apple services > Method 2: Two-step or two-factor authentication > Storing a manually verified session using spaceauth](https://docs.fastlane.tools/best-practices/continuous-integration/#storing-a-manually-verified-session-using-spaceauth) #### Transporter From 829064f9b83c9a690abc6b323937958c2e2083cd Mon Sep 17 00:00:00 2001 From: Roger Oba Date: Wed, 14 Apr 2021 16:31:15 -0300 Subject: [PATCH 12/15] Rename option from exports_to_clipboard to copy_to_clipboard. --- spaceship/lib/spaceship/commands_generator.rb | 4 ++-- spaceship/lib/spaceship/spaceauth_runner.rb | 6 +++--- spaceship/spec/spaceauth_spec.rb | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/spaceship/lib/spaceship/commands_generator.rb b/spaceship/lib/spaceship/commands_generator.rb index 3d9c5f8bdbd..22b0f4f98a0 100644 --- a/spaceship/lib/spaceship/commands_generator.rb +++ b/spaceship/lib/spaceship/commands_generator.rb @@ -39,10 +39,10 @@ def run command :spaceauth do |c| c.syntax = 'fastlane spaceship spaceauth' c.description = 'Authentication helper for spaceship/fastlane to work with Apple 2-Step/2FA' - c.option('--exports_to_clipboard', 'Whether the export method should be copied to clipboard. When using this option, the session can be exported to an env var by calling `eval $(pbpaste)`') + c.option('--copy_to_clipboard', 'Whether the session string should be copied to clipboard. For more info see https://docs.fastlane.tools/best-practices/continuous-integration/#storing-a-manually-verified-session-using-spaceauth`') c.action do |args, options| - Spaceship::SpaceauthRunner.new(username: options.user, exports_to_clipboard: options.exports_to_clipboard).run + Spaceship::SpaceauthRunner.new(username: options.user, copy_to_clipboard: options.copy_to_clipboard).run end end diff --git a/spaceship/lib/spaceship/spaceauth_runner.rb b/spaceship/lib/spaceship/spaceauth_runner.rb index 645aa4aab03..4f9efac90c3 100644 --- a/spaceship/lib/spaceship/spaceauth_runner.rb +++ b/spaceship/lib/spaceship/spaceauth_runner.rb @@ -7,11 +7,11 @@ module Spaceship class SpaceauthRunner - def initialize(username: nil, exports_to_clipboard: nil) + def initialize(username: nil, copy_to_clipboard: nil) @username = username @username ||= CredentialsManager::AppfileConfig.try_fetch_value(:apple_id) @username ||= ask("Username: ") - @exports_to_clipboard = exports_to_clipboard + @copy_to_clipboard = copy_to_clipboard end def run @@ -62,7 +62,7 @@ def run puts("Example:") puts("export FASTLANE_SESSION='#{@yaml}'".cyan.underline) - if @exports_to_clipboard || (mac? && Spaceship::Client::UserInterface.interactive? && agree("🙄 Should fastlane copy the cookie into your clipboard, so you can easily paste it? (y/n)", true)) + if @copy_to_clipboard || (mac? && Spaceship::Client::UserInterface.interactive? && agree("🙄 Should fastlane copy the cookie into your clipboard, so you can easily paste it? (y/n)", true)) FastlaneCore::Clipboard.copy(content: @yaml) puts("Successfully copied the session string into your clipboard 🎨".green) end diff --git a/spaceship/spec/spaceauth_spec.rb b/spaceship/spec/spaceauth_spec.rb index cdd257e6495..15f7acb59f9 100644 --- a/spaceship/spec/spaceauth_spec.rb +++ b/spaceship/spec/spaceauth_spec.rb @@ -15,7 +15,7 @@ end.to output(/export FASTLANE_SESSION=.*name: DES.*name: myacinfo.*name: dqsid.*/).to_stdout end - describe 'exports_to_clipboard option', if: FastlaneCore::Clipboard.is_supported? do + describe 'copy_to_clipboard option', if: FastlaneCore::Clipboard.is_supported? do before :each do # Save clipboard @clipboard = FastlaneCore::Clipboard.paste @@ -27,12 +27,12 @@ end it 'when true, it should copy the session to clipboard' do - Spaceship::SpaceauthRunner.new(exports_to_clipboard: true).run + Spaceship::SpaceauthRunner.new(copy_to_clipboard: true).run expect(FastlaneCore::Clipboard.paste).to match(%r{.*domain: idmsa.apple.com.*path: \"\/appleauth\/auth\/\".*}) end it 'when false, it should not copy the session to clipboard' do - Spaceship::SpaceauthRunner.new(exports_to_clipboard: false).run + Spaceship::SpaceauthRunner.new(copy_to_clipboard: false).run expect(FastlaneCore::Clipboard.paste).to eq(@clipboard) end end From 0934348a55451e439ec49db9b92e3bc172499381 Mon Sep 17 00:00:00 2001 From: Roger Oba Date: Wed, 14 Apr 2021 16:31:20 -0300 Subject: [PATCH 13/15] Code style. --- fastlane/lib/fastlane/actions/clipboard.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastlane/lib/fastlane/actions/clipboard.rb b/fastlane/lib/fastlane/actions/clipboard.rb index 2922a4054cc..14e96abfdd3 100644 --- a/fastlane/lib/fastlane/actions/clipboard.rb +++ b/fastlane/lib/fastlane/actions/clipboard.rb @@ -31,7 +31,7 @@ def self.authors end def self.is_supported?(platform) - return FastlaneCore::Clipboard.is_supported? + FastlaneCore::Clipboard.is_supported? end def self.example_code From 7ded3b6716a2bfbca28cdee131d5bffe56624c99 Mon Sep 17 00:00:00 2001 From: Roger Oba Date: Sat, 17 Apr 2021 10:08:51 -0300 Subject: [PATCH 14/15] Allow non-interactive script in interactive session. --- spaceship/lib/spaceship/spaceauth_runner.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spaceship/lib/spaceship/spaceauth_runner.rb b/spaceship/lib/spaceship/spaceauth_runner.rb index 4f9efac90c3..241a26fec38 100644 --- a/spaceship/lib/spaceship/spaceauth_runner.rb +++ b/spaceship/lib/spaceship/spaceauth_runner.rb @@ -62,7 +62,9 @@ def run puts("Example:") puts("export FASTLANE_SESSION='#{@yaml}'".cyan.underline) - if @copy_to_clipboard || (mac? && Spaceship::Client::UserInterface.interactive? && agree("🙄 Should fastlane copy the cookie into your clipboard, so you can easily paste it? (y/n)", true)) + if @copy_to_clipboard == false + puts("Skipped asking to copy the session string into your clipboard ⏭️".green) + elsif @copy_to_clipboard || (mac? && Spaceship::Client::UserInterface.interactive? && agree("🙄 Should fastlane copy the cookie into your clipboard, so you can easily paste it? (y/n)", true)) FastlaneCore::Clipboard.copy(content: @yaml) puts("Successfully copied the session string into your clipboard 🎨".green) end From 6572f48715d8946aa932a7fad9915447f2918e80 Mon Sep 17 00:00:00 2001 From: Roger Oba Date: Mon, 19 Apr 2021 10:28:09 -0300 Subject: [PATCH 15/15] Update docs to point to new docs page introduced in https://github.com/fastlane/docs/pull/1052 --- spaceship/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spaceship/README.md b/spaceship/README.md index d3d54a914c1..38b73f2de78 100644 --- a/spaceship/README.md +++ b/spaceship/README.md @@ -115,7 +115,7 @@ See [Continuous Integration > Authenticating with Apple services > Method 2: Two #### Transporter -See [Continuous Integration > Authenticating with Apple services > Method 3: Application-specific passwords](https://docs.fastlane.tools/best-practices/continuous-integration/#method-3-application-specific-passwords) +See [Getting Started > iOS > Authentication > Method 3: Application-specific passwords](https://docs.fastlane.tools/getting-started/ios/authentication/#method-3-application-specific-passwords) ## _spaceship_ in use