Skip to content

Commit 08872d5

Browse files
authored
Add auth method with json content data (#354)
1 parent 3773412 commit 08872d5

9 files changed

+94
-35
lines changed

lib/fastlane/plugin/firebase_app_distribution/actions/firebase_app_distribution_action.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def self.run(params)
3939
# TODO(lkellogg): This sets the send timeout for all POST requests made by the client, but
4040
# ideally the timeout should only apply to the binary upload
4141
init_google_api_client(params[:debug], timeout)
42-
authorization = get_authorization(params[:service_credentials_file], params[:firebase_cli_token], params[:debug])
42+
authorization = get_authorization(params[:service_credentials_file], params[:firebase_cli_token], params[:service_credentials_json_data], params[:debug])
4343
client = Google::Apis::FirebaseappdistributionV1::FirebaseAppDistributionService.new
4444
client.authorization = authorization
4545

@@ -411,6 +411,10 @@ def self.available_options
411411
FastlaneCore::ConfigItem.new(key: :service_credentials_file,
412412
description: "Path to Google service account json file",
413413
optional: true,
414+
type: String),
415+
FastlaneCore::ConfigItem.new(key: :service_credentials_json_data,
416+
description: "Google service account json file content",
417+
optional: true,
414418
type: String)
415419
]
416420
end

lib/fastlane/plugin/firebase_app_distribution/actions/firebase_app_distribution_add_testers_action.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class FirebaseAppDistributionAddTestersAction < Action
1313
def self.run(params)
1414
init_google_api_client(params[:debug])
1515
client = Google::Apis::FirebaseappdistributionV1::FirebaseAppDistributionService.new
16-
client.authorization = get_authorization(params[:service_credentials_file], params[:firebase_cli_token], params[:debug])
16+
client.authorization = get_authorization(params[:service_credentials_file], params[:firebase_cli_token], params[:service_credentials_json_data], params[:debug])
1717

1818
if blank?(params[:emails]) && blank?(params[:file])
1919
UI.user_error!("Must specify `emails` or `file`.")
@@ -80,6 +80,10 @@ def self.available_options
8080
description: "Path to Google service credentials file",
8181
optional: true,
8282
type: String),
83+
FastlaneCore::ConfigItem.new(key: :service_credentials_json_data,
84+
description: "Google service account json file content",
85+
optional: true,
86+
type: String),
8387
FastlaneCore::ConfigItem.new(key: :firebase_cli_token,
8488
description: "Auth token generated using the Firebase CLI's login:ci command",
8589
optional: true,

lib/fastlane/plugin/firebase_app_distribution/actions/firebase_app_distribution_create_group_action.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def self.run(params)
2121

2222
init_google_api_client(params[:debug])
2323
client = Google::Apis::FirebaseappdistributionV1::FirebaseAppDistributionService.new
24-
client.authorization = get_authorization(params[:service_credentials_file], params[:firebase_cli_token], params[:debug])
24+
client.authorization = get_authorization(params[:service_credentials_file], params[:firebase_cli_token], params[:service_credentials_json_data], params[:debug])
2525

2626
project_number = params[:project_number]
2727
group_alias = params[:alias]
@@ -87,6 +87,10 @@ def self.available_options
8787
description: "Path to Google service credentials file",
8888
optional: true,
8989
type: String),
90+
FastlaneCore::ConfigItem.new(key: :service_credentials_json_data,
91+
description: "Google service account json file content",
92+
optional: true,
93+
type: String),
9094
FastlaneCore::ConfigItem.new(key: :firebase_cli_token,
9195
description: "Auth token generated using the Firebase CLI's login:ci command",
9296
optional: true,

lib/fastlane/plugin/firebase_app_distribution/actions/firebase_app_distribution_delete_group_action.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class FirebaseAppDistributionDeleteGroupAction < Action
1313
def self.run(params)
1414
init_google_api_client(params[:debug])
1515
client = Google::Apis::FirebaseappdistributionV1::FirebaseAppDistributionService.new
16-
client.authorization = get_authorization(params[:service_credentials_file], params[:firebase_cli_token], params[:debug])
16+
client.authorization = get_authorization(params[:service_credentials_file], params[:firebase_cli_token], params[:service_credentials_json_data], params[:debug])
1717

1818
if blank?(params[:alias])
1919
UI.user_error!("Must specify `alias`.")
@@ -67,6 +67,10 @@ def self.available_options
6767
description: "Path to Google service credentials file",
6868
optional: true,
6969
type: String),
70+
FastlaneCore::ConfigItem.new(key: :service_credentials_json_data,
71+
description: "Google service account json file content",
72+
optional: true,
73+
type: String),
7074
FastlaneCore::ConfigItem.new(key: :firebase_cli_token,
7175
description: "Auth token generated using the Firebase CLI's login:ci command",
7276
optional: true,

lib/fastlane/plugin/firebase_app_distribution/actions/firebase_app_distribution_get_latest_release.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class FirebaseAppDistributionGetLatestReleaseAction < Action
1515
def self.run(params)
1616
init_google_api_client(params[:debug])
1717
client = Google::Apis::FirebaseappdistributionV1::FirebaseAppDistributionService.new
18-
client.authorization = get_authorization(params[:service_credentials_file], params[:firebase_cli_token], params[:debug])
18+
client.authorization = get_authorization(params[:service_credentials_file], params[:firebase_cli_token], params[:service_credentials_json_data], params[:debug])
1919

2020
UI.message("⏳ Fetching latest release for app #{params[:app]}...")
2121

@@ -91,6 +91,10 @@ def self.available_options
9191
description: "Path to Google service account json",
9292
optional: true,
9393
type: String),
94+
FastlaneCore::ConfigItem.new(key: :service_credentials_json_data,
95+
description: "Google service account json file content",
96+
optional: true,
97+
type: String),
9498
FastlaneCore::ConfigItem.new(key: :debug,
9599
description: "Print verbose debug output",
96100
optional: true,

lib/fastlane/plugin/firebase_app_distribution/actions/firebase_app_distribution_get_udids.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class FirebaseAppDistributionGetUdidsAction < Action
1616
def self.run(params)
1717
init_google_api_client(params[:debug])
1818
client = Google::Apis::FirebaseappdistributionV1alpha::FirebaseAppDistributionService.new
19-
client.authorization = get_authorization(params[:service_credentials_file], params[:firebase_cli_token], params[:debug])
19+
client.authorization = get_authorization(params[:service_credentials_file], params[:firebase_cli_token], params[:service_credentials_json_data], params[:debug])
2020

2121
project_number = params[:project_number]
2222
if blank?(project_number)
@@ -86,6 +86,10 @@ def self.available_options
8686
description: "Path to Google service account json",
8787
optional: true,
8888
type: String),
89+
FastlaneCore::ConfigItem.new(key: :service_credentials_json_data,
90+
description: "Google service account json file content",
91+
optional: true,
92+
type: String),
8993
FastlaneCore::ConfigItem.new(key: :debug,
9094
description: "Print verbose debug output",
9195
optional: true,

lib/fastlane/plugin/firebase_app_distribution/actions/firebase_app_distribution_remove_testers_action.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class FirebaseAppDistributionRemoveTestersAction < Action
1313
def self.run(params)
1414
init_google_api_client(params[:debug])
1515
client = Google::Apis::FirebaseappdistributionV1::FirebaseAppDistributionService.new
16-
client.authorization = get_authorization(params[:service_credentials_file], params[:firebase_cli_token], params[:debug])
16+
client.authorization = get_authorization(params[:service_credentials_file], params[:firebase_cli_token], params[:service_credentials_json_data], params[:debug])
1717

1818
if blank?(params[:emails]) && blank?(params[:file])
1919
UI.user_error!("Must specify `emails` or `file`.")
@@ -75,6 +75,10 @@ def self.available_options
7575
description: "Path to Google service credentials file",
7676
optional: true,
7777
type: String),
78+
FastlaneCore::ConfigItem.new(key: :service_credentials_json_data,
79+
description: "Google service account json file content",
80+
optional: true,
81+
type: String),
7882
FastlaneCore::ConfigItem.new(key: :firebase_cli_token,
7983
description: "Auth token generated using the Firebase CLI's login:ci command",
8084
optional: true,

lib/fastlane/plugin/firebase_app_distribution/helper/firebase_app_distribution_auth_client.rb

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module FirebaseAppDistributionAuthClient
2020
# auth method is used, unset all other auth variables/parameters to nil/empty
2121
#
2222
# args
23+
# google_service_json_data - Google service account json file content as a string
2324
# google_service_path - Absolute path to the Google service account file
2425
# firebase_cli_token - Refresh token
2526
# debug - Whether to enable debug-level logging
@@ -28,10 +29,13 @@ module FirebaseAppDistributionAuthClient
2829
# FIREBASE_TOKEN - see firebase_cli_token
2930
#
3031
# Crashes if given invalid or missing credentials
31-
def get_authorization(google_service_path, firebase_cli_token, debug = false)
32+
def get_authorization(google_service_path, firebase_cli_token, google_service_json_data, debug = false)
3233
if !google_service_path.nil? && !google_service_path.empty?
3334
UI.message("🔐 Authenticating with --service_credentials_file path parameter: #{google_service_path}")
34-
service_account(google_service_path, debug)
35+
service_account_from_file(google_service_path, debug)
36+
elsif !google_service_json_data.nil? && !google_service_json_data.empty?
37+
UI.message("🔐 Authenticating with --service_credentials_json content parameter")
38+
service_account_from_json(google_service_json_data, debug)
3539
elsif !firebase_cli_token.nil? && !firebase_cli_token.empty?
3640
UI.message("🔐 Authenticating with --firebase_cli_token parameter")
3741
firebase_token(firebase_cli_token, debug)
@@ -100,24 +104,32 @@ def firebase_token(refresh_token, debug)
100104
UI.user_error!(error_message)
101105
end
102106

103-
def service_account(google_service_path, debug)
107+
def service_account_from_json(google_service_json_data, debug)
108+
get_service_account_credentials(google_service_json_data, debug)
109+
end
110+
111+
def service_account_from_file(google_service_path, debug)
112+
get_service_account_credentials(File.read(google_service_path), debug)
113+
rescue Errno::ENOENT
114+
UI.user_error!("#{ErrorMessage::SERVICE_CREDENTIALS_NOT_FOUND}: #{google_service_path}")
115+
end
116+
117+
def get_service_account_credentials(json_data, debug)
118+
json_file = JSON.parse(json_data)
104119
# check if it's an external account or service account
105-
json_file = JSON.parse(File.read(google_service_path))
106120
auth = json_file["type"] == "external_account" ? Google::Auth::ExternalAccount::Credentials : Google::Auth::ServiceAccountCredentials
107121
service_account_credentials = auth.make_creds(
108-
json_key_io: File.open(google_service_path),
122+
json_key_io: StringIO.new(json_data),
109123
scope: SCOPE
110124
)
111125
service_account_credentials.fetch_access_token!
112126
service_account_credentials
113-
rescue Errno::ENOENT
114-
UI.user_error!("#{ErrorMessage::SERVICE_CREDENTIALS_NOT_FOUND}: #{google_service_path}")
115127
rescue Signet::AuthorizationError => error
116-
error_message = "#{ErrorMessage::SERVICE_CREDENTIALS_ERROR}: \"#{google_service_path}\""
128+
error_message = "#{ErrorMessage::SERVICE_CREDENTIALS_ERROR}: "
117129
if debug
118130
error_message += "\n#{error_details(error)}"
119131
else
120-
error_message += ". #{debug_instructions}"
132+
error_message += debug_instructions.to_s
121133
end
122134
UI.user_error!(error_message)
123135
end

0 commit comments

Comments
 (0)