fastlane-plugin-samsungknox 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +20 -37
- data/lib/fastlane/plugin/samsungknox/actions/knox_configure_library_upload_app_action.rb +69 -37
- data/lib/fastlane/plugin/samsungknox/helper/knox_configure_error_message.rb +1 -2
- data/lib/fastlane/plugin/samsungknox/helper/knox_configure_library_already_exists_app_version_check.rb +10 -19
- data/lib/fastlane/plugin/samsungknox/helper/knox_configure_library_upload_app.rb +64 -12
- data/lib/fastlane/plugin/samsungknox/helper/knox_http_response.rb +29 -0
- data/lib/fastlane/plugin/samsungknox/helper/knox_token_generator.rb +45 -31
- data/lib/fastlane/plugin/samsungknox/version.rb +1 -1
- metadata +51 -28
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: afc3565403824fa3fdafd645840f1dff1f38520e3b5f8a41de0d188fa6e59cde
|
|
4
|
+
data.tar.gz: 5c8435ab074d1b78390b8b67c3acd2994df1299266ebfae6df89d64c745e0fd6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7204c34bce3d5fb9d34db7e746211678101e6976134dc4007a4d09c279edb38240cd8f25df4a3b3db07a6973a443c3fd4ef30dda5eb83695cf4db98cb0e39499
|
|
7
|
+
data.tar.gz: b1bc852f1b25c049d40d8e62604c592aeb685209cee3680fb832617d7c4ec6cfc730045b111e116124488065aa5a5f63354ab270bf197da0a36457c2c0cefc31
|
data/README.md
CHANGED
|
@@ -1,53 +1,36 @@
|
|
|
1
|
-
# samsungknox
|
|
1
|
+
# `fastlane-plugin-samsungknox`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
## Installation
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
This project is a [_fastlane_](https://github.com/fastlane/fastlane) plugin. To get started with `fastlane-plugin-samsungknox`, add it to your project by running:
|
|
5
|
+
Requires Ruby 3.1 or later. Inside your Android fastlane folder:
|
|
8
6
|
|
|
9
7
|
```bash
|
|
10
8
|
fastlane add_plugin samsungknox
|
|
11
9
|
```
|
|
12
10
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
SamsungKnox products to Fastlanehttps://redredgroup.github.io/samsungknox/docs/knox-fastlane
|
|
16
|
-
|
|
17
|
-
**Note to author:** Add a more detailed description about this plugin here. If your plugin contains multiple actions, make sure to mention them here.
|
|
18
|
-
|
|
19
|
-
## Example
|
|
20
|
-
|
|
21
|
-
Check out the [example `Fastfile`](fastlane/Fastfile) to see how to use this plugin. Try it by cloning the repo, running `fastlane install_plugins` and `bundle exec fastlane test`.
|
|
22
|
-
|
|
23
|
-
**Note to author:** Please set up a sample project to make it easy for users to explore what your plugin does. Provide everything that is necessary to try out the plugin in this project (including a sample Xcode/Android project if necessary)
|
|
24
|
-
|
|
25
|
-
## Run tests for this plugin
|
|
26
|
-
|
|
27
|
-
To run both the tests, and code style validation, run
|
|
28
|
-
|
|
29
|
-
```
|
|
30
|
-
rake
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
To automatically fix many of the styling issues, use
|
|
11
|
+
Pin the major version in `fastlane/Pluginfile`:
|
|
34
12
|
|
|
13
|
+
```ruby
|
|
14
|
+
gem 'fastlane-plugin-samsungknox', '~> 1.0'
|
|
35
15
|
```
|
|
36
|
-
rubocop -a
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
## Issues and Feedback
|
|
40
16
|
|
|
41
|
-
|
|
17
|
+
Upgrading from 0.1.x? Version 1.0.0 switches the default authentication to
|
|
18
|
+
OAuth. See [Upgrading from 0.1.x](https://github.com/REDREDGROUP/samsungknox/blob/main/packages/knox-docs/docs/knox-fastlane-plugin/oauth-v2.mdx#upgrading-from-01x).
|
|
42
19
|
|
|
43
|
-
##
|
|
20
|
+
## Usage
|
|
44
21
|
|
|
45
|
-
|
|
22
|
+
The `knox_configure_library_upload_app` action uses OAuth by default. Provide
|
|
23
|
+
`client_id`, `client_secret`, and an existing readable APK path. Token expiry
|
|
24
|
+
and renewal are not handled by the action. The OAuth flow never falls back to
|
|
25
|
+
legacy credentials.
|
|
46
26
|
|
|
47
|
-
|
|
27
|
+
To deliberately use the retained regional JWT flow, set `auth_mode: 'legacy'`
|
|
28
|
+
and supply its legacy options. See the [plugin OAuth guide](https://github.com/REDREDGROUP/samsungknox/blob/main/packages/knox-docs/docs/knox-fastlane-plugin/oauth-v2.mdx)
|
|
29
|
+
for both configurations, upload status meanings, and evidence limits.
|
|
48
30
|
|
|
49
|
-
|
|
31
|
+
The current Configure reference is labeled **26.10 UAT**. Production rollout
|
|
32
|
+
and authentication against an actual Samsung tenant are unverified.
|
|
50
33
|
|
|
51
|
-
##
|
|
34
|
+
## License
|
|
52
35
|
|
|
53
|
-
|
|
36
|
+
Apache-2.0. REDREDGROUP is not affiliated with Samsung Knox.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
require 'fastlane/action'
|
|
2
|
+
require 'fastlane_core/configuration/config_item'
|
|
2
3
|
require_relative '../helper/knox_token_generator'
|
|
3
|
-
# require_relative '../helper/knox_configure_library_already_exists_app_version_check'
|
|
4
4
|
require_relative '../helper/knox_configure_library_upload_app'
|
|
5
5
|
|
|
6
6
|
module Fastlane
|
|
@@ -8,25 +8,46 @@ module Fastlane
|
|
|
8
8
|
class KnoxConfigureLibraryUploadAppAction < Action
|
|
9
9
|
def self.run(params)
|
|
10
10
|
UI.header("Step: KC Upload App")
|
|
11
|
+
validate_options(params)
|
|
12
|
+
case params[:auth_mode] || 'oauth'
|
|
13
|
+
when 'oauth'
|
|
14
|
+
token = KnoxTokenGenerator.generate_oauth_access_token(
|
|
15
|
+
client_id: params[:client_id], client_secret: params[:client_secret],
|
|
16
|
+
scope: params[:scope] || 'kc.application:manage'
|
|
17
|
+
)
|
|
18
|
+
KnoxConfigureLibraryUploadApp.upload_oauth_app(token, params[:app_file_path], params[:app_description])
|
|
19
|
+
when 'legacy'
|
|
20
|
+
access_token = KnoxTokenGenerator.generate_knox_access_token(
|
|
21
|
+
{ path: params[:credential_path], key: params[:credential_key] },
|
|
22
|
+
params[:clientIdentifierJwtToken], params[:api_region].downcase
|
|
23
|
+
)
|
|
24
|
+
KnoxConfigureLibraryUploadApp.upload_app(
|
|
25
|
+
access_token[:token], params[:app_file_path], params[:app_description], params[:api_region].downcase
|
|
26
|
+
)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
11
29
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
+
def self.validate_options(params)
|
|
31
|
+
path = params[:app_file_path]
|
|
32
|
+
UI.user_error!("app_file_path must be a readable APK file") unless path && File.file?(path) && File.readable?(path) && File.extname(path).casecmp?('.apk')
|
|
33
|
+
legacy_keys = [:credential_path, :credential_key, :clientIdentifierJwtToken, :api_region]
|
|
34
|
+
oauth_keys = [:client_id, :client_secret, :scope]
|
|
35
|
+
case params[:auth_mode] || 'oauth'
|
|
36
|
+
when 'oauth'
|
|
37
|
+
UI.user_error!("OAuth cannot be mixed with legacy credentials or region") if legacy_keys.any? { |key| params[key] }
|
|
38
|
+
required = [:client_id, :client_secret]
|
|
39
|
+
when 'legacy'
|
|
40
|
+
UI.user_error!("Legacy cannot be mixed with OAuth credentials or scope") if oauth_keys.any? { |key| params[key] }
|
|
41
|
+
sources = [:credential_path, :credential_key].count { |key| params[key] && !params[key].strip.empty? }
|
|
42
|
+
UI.user_error!("Legacy requires exactly one credential source") unless sources == 1
|
|
43
|
+
required = [:api_region, :clientIdentifierJwtToken]
|
|
44
|
+
else
|
|
45
|
+
UI.user_error!("auth_mode must be oauth or legacy")
|
|
46
|
+
end
|
|
47
|
+
required.each do |key|
|
|
48
|
+
UI.user_error!("#{key} is required for this auth_mode") unless params[key] && !params[key].strip.empty?
|
|
49
|
+
end
|
|
50
|
+
UI.user_error!("scope must not be empty") if params[:scope] && params[:scope].strip.empty?
|
|
30
51
|
end
|
|
31
52
|
|
|
32
53
|
def self.description
|
|
@@ -46,16 +67,26 @@ module Fastlane
|
|
|
46
67
|
|
|
47
68
|
def self.available_options
|
|
48
69
|
[
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
70
|
+
FastlaneCore::ConfigItem.new(key: :auth_mode,
|
|
71
|
+
env_name: "KNOX_AUTH_MODE",
|
|
72
|
+
description: "OAuth (default) or explicit legacy authentication",
|
|
73
|
+
default_value: 'oauth',
|
|
74
|
+
verify_block: proc do |value|
|
|
75
|
+
UI.user_error!("auth_mode must be oauth or legacy") unless ['oauth', 'legacy'].include?(value)
|
|
76
|
+
end),
|
|
77
|
+
FastlaneCore::ConfigItem.new(key: :client_id,
|
|
78
|
+
env_name: "KNOX_CLIENT_ID",
|
|
79
|
+
description: "OAuth client ID",
|
|
80
|
+
optional: true),
|
|
81
|
+
FastlaneCore::ConfigItem.new(key: :client_secret,
|
|
82
|
+
env_name: "KNOX_CLIENT_SECRET",
|
|
83
|
+
description: "OAuth client secret",
|
|
84
|
+
sensitive: true,
|
|
85
|
+
optional: true),
|
|
86
|
+
FastlaneCore::ConfigItem.new(key: :scope,
|
|
87
|
+
env_name: "KNOX_SCOPE",
|
|
88
|
+
description: "OAuth scopes (default kc.application:manage)",
|
|
89
|
+
optional: true),
|
|
59
90
|
FastlaneCore::ConfigItem.new(key: :app_description,
|
|
60
91
|
env_name: "KC_APP_DESCRIPTION",
|
|
61
92
|
description: "",
|
|
@@ -63,20 +94,20 @@ module Fastlane
|
|
|
63
94
|
FastlaneCore::ConfigItem.new(key: :app_file_path,
|
|
64
95
|
env_name: "KC_APP_FILE_PATH",
|
|
65
96
|
description: "Path to your APK file",
|
|
66
|
-
optional:
|
|
67
|
-
|
|
97
|
+
optional: false,
|
|
98
|
+
verify_block: proc do |value|
|
|
99
|
+
UI.user_error!("app_file_path must be a readable APK file") unless File.file?(value) && File.readable?(value) && File.extname(value).casecmp?('.apk')
|
|
100
|
+
end),
|
|
101
|
+
|
|
68
102
|
# Knox Credential Configuration
|
|
69
103
|
FastlaneCore::ConfigItem.new(key: :api_region,
|
|
70
104
|
env_name: "KC_API_REGION",
|
|
71
105
|
description: "",
|
|
72
|
-
optional:
|
|
73
|
-
verify_block: proc do |value|
|
|
74
|
-
UI.user_error!("No API region for Knox Credential given, pass using `api_region: 'value'`") unless (value and not value.empty?)
|
|
75
|
-
end
|
|
76
|
-
),
|
|
106
|
+
optional: true),
|
|
77
107
|
FastlaneCore::ConfigItem.new(key: :credential_key,
|
|
78
108
|
env_name: "KNOX_CREDENTIAL_KEY",
|
|
79
109
|
description: "",
|
|
110
|
+
sensitive: true,
|
|
80
111
|
optional: true),
|
|
81
112
|
FastlaneCore::ConfigItem.new(key: :credential_path,
|
|
82
113
|
env_name: "KNOX_CREDENTIAL_PATH",
|
|
@@ -85,7 +116,8 @@ module Fastlane
|
|
|
85
116
|
FastlaneCore::ConfigItem.new(key: :clientIdentifierJwtToken,
|
|
86
117
|
env_name: "KNOX_CLIENT_IEDNTIFIER_JWT_TOKEN",
|
|
87
118
|
description: "",
|
|
88
|
-
|
|
119
|
+
sensitive: true,
|
|
120
|
+
optional: true)
|
|
89
121
|
]
|
|
90
122
|
end
|
|
91
123
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
module ErrorMessage
|
|
2
2
|
INVALID_JWT = 'clientIdentifierJwtToken is not a json web token type. check your clientIdentifierJwtToken'
|
|
3
|
-
BOTH_CREDENTIAL_DETECTED = "credentials exceeded one. You must specify either key or path."
|
|
3
|
+
BOTH_CREDENTIAL_DETECTED = "credentials exceeded one. You must specify either key or path."
|
|
4
4
|
CREDENTIAL_IS_NOT_JSON_TYPE = "credential is not json type. Please check the credential file or path"
|
|
5
5
|
CREDENTIAL_MISSING = "credential is missing. Please check the credential file or path"
|
|
6
6
|
CREDENTIAL_FILE_NOT_FOUND = "credential read failed. Please check the credential path"
|
|
@@ -8,7 +8,6 @@ module ErrorMessage
|
|
|
8
8
|
PRIVATE_MISSING = 'credential -> Private is missing. Please check the credential file or path.'
|
|
9
9
|
PUBLIC_MISSING = 'credential -> Public is missing. Please check the credential file or path.'
|
|
10
10
|
|
|
11
|
-
|
|
12
11
|
def self.aab_upload_error(aab_state)
|
|
13
12
|
"Failed to process the AAB: #{aab_state}"
|
|
14
13
|
end
|
|
@@ -1,33 +1,24 @@
|
|
|
1
|
+
require 'net/http'
|
|
2
|
+
require 'uri'
|
|
3
|
+
require_relative 'knox_http_response'
|
|
1
4
|
|
|
2
5
|
module Fastlane
|
|
3
6
|
UI = FastlaneCore::UI unless Fastlane.const_defined?("UI")
|
|
4
7
|
|
|
5
8
|
module KnoxConfigureLibraryAlreadyExistsAppVersionCheck
|
|
9
|
+
def self.search_by_package_name(access_token, package_name, api_region: 'eu')
|
|
10
|
+
uri = URI("https://#{api_region.downcase}-kcs-api.samsungknox.com/kcs/v1/kc/applications")
|
|
11
|
+
uri.query = URI.encode_www_form(packageName: package_name)
|
|
6
12
|
|
|
7
|
-
def self.search_by_package_name(access_token, package_name)
|
|
8
|
-
uri = URI("https://eu-kcs-api.samsungknox.com/kcs/v1/kc/applications?packageName=#{package_name}")
|
|
9
|
-
|
|
10
|
-
UI.message(access_token)
|
|
11
13
|
request = Net::HTTP::Get.new(uri)
|
|
12
14
|
request["Content-Type"] = "application/json"
|
|
13
15
|
request["X-KNOX-APITOKEN"] = access_token
|
|
14
|
-
|
|
15
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
|
16
|
-
http.use_ssl = true if uri.scheme == 'https'
|
|
17
16
|
|
|
18
|
-
response =
|
|
19
|
-
|
|
20
|
-
if response.is_a?(Net::HTTPSuccess)
|
|
21
|
-
data = JSON.parse(response.body)
|
|
22
|
-
# UI.success(data)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
# UI.success('✅ ams accessToken generated successfully!')
|
|
26
|
-
return data["accessToken"]
|
|
27
|
-
else
|
|
28
|
-
raise "Request failed with response: #{response.body}"
|
|
17
|
+
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https', open_timeout: 30, read_timeout: 30) do |http|
|
|
18
|
+
http.request(request)
|
|
29
19
|
end
|
|
20
|
+
|
|
21
|
+
KnoxHttpResponse.parse(response, 'Legacy application search')
|
|
30
22
|
end
|
|
31
|
-
|
|
32
23
|
end
|
|
33
24
|
end
|
|
@@ -1,29 +1,81 @@
|
|
|
1
1
|
require 'net/http/post/multipart'
|
|
2
2
|
require 'uri'
|
|
3
|
+
require_relative 'knox_http_response'
|
|
3
4
|
|
|
4
5
|
module Fastlane
|
|
5
6
|
UI = FastlaneCore::UI unless Fastlane.const_defined?("UI")
|
|
6
7
|
|
|
7
8
|
module KnoxConfigureLibraryUploadApp
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
class UploadFailed < StandardError
|
|
10
|
+
attr_reader :application_id
|
|
11
|
+
|
|
12
|
+
def initialize(application_id)
|
|
13
|
+
@application_id = application_id
|
|
14
|
+
super("Application #{application_id} processing failed")
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.upload_oauth_app(access_token, app_file_path, app_description)
|
|
19
|
+
uri = URI('https://api.samsungknox.com/kc/v2/applications/inhouseApplications')
|
|
20
|
+
uri.query = URI.encode_www_form(description: app_description) unless app_description.nil?
|
|
21
|
+
File.open(app_file_path, 'rb') do |file|
|
|
22
|
+
request = Net::HTTP::Post::Multipart.new(uri, "file" => UploadIO.new(file, "application/vnd.android.package-archive", File.basename(app_file_path)))
|
|
23
|
+
request['Authorization'] = "Bearer #{access_token}"
|
|
24
|
+
response = Net::HTTP.start(uri.host, uri.port, use_ssl: true, open_timeout: 30, read_timeout: 30) do |http|
|
|
25
|
+
http.request(request)
|
|
26
|
+
end
|
|
27
|
+
data = KnoxHttpResponse.parse(response, 'Application upload')
|
|
28
|
+
application_id = data['applicationId']
|
|
29
|
+
raise KnoxHttpResponse::InvalidResponse, 'Application upload returned an invalid applicationId' unless application_id.kind_of?(String) && !application_id.strip.empty?
|
|
30
|
+
|
|
31
|
+
case data['status']
|
|
32
|
+
when 'InProgress'
|
|
33
|
+
UI.message("Application #{application_id} accepted; processing is in progress")
|
|
34
|
+
when 'Deployable'
|
|
35
|
+
UI.success("Application #{application_id} is deployable")
|
|
36
|
+
when 'Failed'
|
|
37
|
+
raise UploadFailed, application_id
|
|
38
|
+
else
|
|
39
|
+
raise KnoxHttpResponse::InvalidResponse, 'Application upload returned an invalid status'
|
|
40
|
+
end
|
|
41
|
+
data
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def self.upload_app(access_token, app_file_path, app_description, api_region)
|
|
46
|
+
UI.header('Step: 📲 Upload Application to Knox Configure Library 📲 ')
|
|
47
|
+
|
|
48
|
+
uri = URI("https://#{api_region}-kcs-api.samsungknox.com/kcs/v1/kc/applications/upload")
|
|
49
|
+
|
|
50
|
+
File.open(app_file_path, 'rb') do |file|
|
|
51
|
+
request = Net::HTTP::Post::Multipart.new(uri,
|
|
52
|
+
"file" => UploadIO.new(file, "multipart/form-data", File.basename(app_file_path)))
|
|
10
53
|
|
|
11
|
-
File.open(app_file_path) do |file|
|
|
12
|
-
request = Net::HTTP::Post::Multipart.new uri,
|
|
13
|
-
"file" => UploadIO.new(file, "multipart/form-data", File.basename(app_file_path))
|
|
14
|
-
|
|
15
54
|
request['X-KNOX-APITOKEN'] = access_token
|
|
16
|
-
|
|
17
|
-
UI.message("⌛ Uploading the
|
|
55
|
+
|
|
56
|
+
UI.message("⌛ Uploading the Application. Please wait a moment...")
|
|
18
57
|
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
|
19
58
|
http.request(request)
|
|
20
59
|
end
|
|
21
60
|
|
|
22
|
-
if response.
|
|
23
|
-
|
|
24
|
-
|
|
61
|
+
if response.kind_of?(Net::HTTPSuccess)
|
|
62
|
+
upload_response_body = KnoxHttpResponse.parse(response, 'Legacy application upload')
|
|
63
|
+
success_response = upload_response_body['successResponse'][0]
|
|
64
|
+
app_info = success_response['appInfo'][0]
|
|
65
|
+
UI.success("🎉 Uploaded Application [#{app_info['name']}] v#{app_info['packageVersion']} successfully!")
|
|
25
66
|
else
|
|
26
|
-
|
|
67
|
+
begin
|
|
68
|
+
upload_response_body = JSON.parse(response.body)
|
|
69
|
+
rescue JSON::ParserError
|
|
70
|
+
raise KnoxHttpResponse::RequestError.new('Legacy application upload', response.code), cause: nil
|
|
71
|
+
end
|
|
72
|
+
raise KnoxHttpResponse::RequestError.new('Legacy application upload', response.code) unless upload_response_body.kind_of?(Hash)
|
|
73
|
+
|
|
74
|
+
if upload_response_body['data'].kind_of?(String) && upload_response_body['data'].strip == 'APP_BINARY_EXISTS'
|
|
75
|
+
UI.success("✅ The same Application was found in release with no changes, skipping.")
|
|
76
|
+
else
|
|
77
|
+
raise KnoxHttpResponse::RequestError.new('Legacy application upload', response.code)
|
|
78
|
+
end
|
|
27
79
|
end
|
|
28
80
|
end
|
|
29
81
|
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
require 'net/http'
|
|
3
|
+
|
|
4
|
+
module Fastlane
|
|
5
|
+
module KnoxHttpResponse
|
|
6
|
+
class RequestError < StandardError
|
|
7
|
+
attr_reader :status
|
|
8
|
+
|
|
9
|
+
def initialize(operation, status)
|
|
10
|
+
@status = status
|
|
11
|
+
super("#{operation} failed (HTTP #{status})")
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
class InvalidResponse < StandardError
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.parse(response, operation)
|
|
19
|
+
raise RequestError.new(operation, response.code) unless response.kind_of?(Net::HTTPSuccess)
|
|
20
|
+
|
|
21
|
+
data = JSON.parse(response.body)
|
|
22
|
+
raise InvalidResponse, "#{operation} returned an invalid response" unless data.kind_of?(Hash)
|
|
23
|
+
|
|
24
|
+
data
|
|
25
|
+
rescue JSON::ParserError
|
|
26
|
+
raise InvalidResponse, "#{operation} returned invalid JSON", cause: nil
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -3,15 +3,36 @@ require 'jwt'
|
|
|
3
3
|
require 'openssl'
|
|
4
4
|
require 'base64'
|
|
5
5
|
require 'securerandom'
|
|
6
|
+
require 'net/http'
|
|
7
|
+
require 'uri'
|
|
8
|
+
require 'json'
|
|
9
|
+
require_relative 'knox_configure_error_message'
|
|
10
|
+
require_relative 'knox_http_response'
|
|
6
11
|
|
|
7
12
|
module Fastlane
|
|
8
13
|
UI = FastlaneCore::UI unless Fastlane.const_defined?("UI")
|
|
9
14
|
|
|
10
15
|
module KnoxTokenGenerator
|
|
16
|
+
def self.generate_oauth_access_token(client_id:, client_secret:, scope: 'kc.application:manage')
|
|
17
|
+
uri = URI('https://api.samsungknox.com/ams/v1/oauth2/token')
|
|
18
|
+
request = Net::HTTP::Post.new(uri)
|
|
19
|
+
request.set_form_data(grant_type: 'client_credentials', client_id: client_id, client_secret: client_secret, scope: scope)
|
|
20
|
+
response = Net::HTTP.start(uri.host, uri.port, use_ssl: true, open_timeout: 30, read_timeout: 30) do |http|
|
|
21
|
+
http.request(request)
|
|
22
|
+
end
|
|
23
|
+
data = KnoxHttpResponse.parse(response, 'OAuth token exchange')
|
|
24
|
+
token = data['access_token']
|
|
25
|
+
unless token.kind_of?(String) && !token.strip.empty? && data['token_type'].kind_of?(String) && data['token_type'].casecmp?('Bearer') && !token.match?(/[\r\n]/)
|
|
26
|
+
raise KnoxHttpResponse::InvalidResponse, 'OAuth token exchange returned an invalid bearer token'
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
token
|
|
30
|
+
end
|
|
31
|
+
|
|
11
32
|
def self.read_credential(credential)
|
|
12
33
|
UI.crash!(ErrorMessage::BOTH_CREDENTIAL_DETECTED) if credential[:path] && credential[:key]
|
|
13
|
-
UI.crash!(ErrorMessage::
|
|
14
|
-
|
|
34
|
+
UI.crash!(ErrorMessage::CREDENTIAL_MISSING) unless credential[:path] || credential[:key]
|
|
35
|
+
|
|
15
36
|
begin
|
|
16
37
|
credential_manifest = if credential[:path]
|
|
17
38
|
begin
|
|
@@ -23,10 +44,10 @@ module Fastlane
|
|
|
23
44
|
JSON.parse(credential[:key])
|
|
24
45
|
end
|
|
25
46
|
|
|
26
|
-
raise TypeError
|
|
27
|
-
raise TypeError
|
|
28
|
-
raise TypeError
|
|
29
|
-
|
|
47
|
+
raise TypeError, ErrorMessage::IDENTIFIER_MISSING if credential_manifest['Identifier'].nil? || credential_manifest['Identifier'].empty?
|
|
48
|
+
raise TypeError, ErrorMessage::PRIVATE_MISSING if credential_manifest['Private'].nil? || credential_manifest['Private'].empty?
|
|
49
|
+
raise TypeError, ErrorMessage::PUBLIC_MISSING if credential_manifest['Public'].nil? || credential_manifest['Public'].empty?
|
|
50
|
+
|
|
30
51
|
credential_manifest
|
|
31
52
|
rescue JSON::ParserError
|
|
32
53
|
raise ErrorMessage::CREDENTIAL_IS_NOT_JSON_TYPE
|
|
@@ -36,7 +57,7 @@ module Fastlane
|
|
|
36
57
|
def self.generate_jwt_id
|
|
37
58
|
SecureRandom.uuid + SecureRandom.uuid
|
|
38
59
|
end
|
|
39
|
-
|
|
60
|
+
|
|
40
61
|
def self.export_credential_private_key(private_key_str)
|
|
41
62
|
pem_header = "-----BEGIN PRIVATE KEY-----\n"
|
|
42
63
|
pem_footer = "\n-----END PRIVATE KEY-----"
|
|
@@ -50,56 +71,50 @@ module Fastlane
|
|
|
50
71
|
full_key = pem_header + public_key_str + pem_footer
|
|
51
72
|
rsa_public_key = OpenSSL::PKey::RSA.new(full_key)
|
|
52
73
|
der_public_key = rsa_public_key.to_der
|
|
53
|
-
|
|
54
|
-
base64_public_key
|
|
74
|
+
Base64.encode64(der_public_key)
|
|
55
75
|
end
|
|
56
76
|
|
|
57
77
|
def self.get_access_token(validity_for_access_token_in_minutes, base64_encoded_string_public_key, client_identifier_jwt, api_region)
|
|
58
78
|
uri = URI("https://#{api_region}-kcs-api.samsungknox.com/ams/v1/users/accesstoken")
|
|
59
|
-
|
|
79
|
+
|
|
60
80
|
request_body = {
|
|
61
81
|
base64EncodedStringPublicKey: base64_encoded_string_public_key,
|
|
62
82
|
clientIdentifierJwt: client_identifier_jwt
|
|
63
83
|
}
|
|
64
84
|
request_body[:validityForAccessTokenInMinutes] = validity_for_access_token_in_minutes unless validity_for_access_token_in_minutes.nil?
|
|
65
|
-
|
|
85
|
+
|
|
66
86
|
response = Net::HTTP.post(uri, request_body.to_json, "Content-Type" => "application/json")
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return data["accessToken"]
|
|
72
|
-
else
|
|
73
|
-
raise "Request failed with response: #{response.body}"
|
|
74
|
-
end
|
|
87
|
+
|
|
88
|
+
data = KnoxHttpResponse.parse(response, 'Legacy token exchange')
|
|
89
|
+
UI.success('✅ ams accessToken generated successfully')
|
|
90
|
+
return data["accessToken"]
|
|
75
91
|
end
|
|
76
92
|
|
|
77
|
-
def self.generate_knox_access_token(credential,
|
|
93
|
+
def self.generate_knox_access_token(credential, client_identifier_jwt_token, api_region)
|
|
78
94
|
UI.header('Step: 🔐 KNOX ACCESS TOKEN GENERATE 🔐 ')
|
|
79
95
|
|
|
80
|
-
UI.message('
|
|
81
|
-
generate_signed_jwt_result = generate_signed_jwt(credential,
|
|
96
|
+
UI.message('🔐 Sign in the knox token using the credential...')
|
|
97
|
+
generate_signed_jwt_result = generate_signed_jwt(credential, client_identifier_jwt_token, true)
|
|
82
98
|
|
|
83
|
-
|
|
99
|
+
base64_encoding_public_key = generate_signed_jwt_result[:public_key]
|
|
84
100
|
sign_by_client_identifier_jwt_token = generate_signed_jwt_result[:token]
|
|
85
101
|
|
|
86
|
-
UI.message('⏳ Request the knox client identifier jwt ams access token
|
|
87
|
-
ams_access_token = get_access_token(30,
|
|
88
|
-
UI.message('
|
|
102
|
+
UI.message('⏳ Request the knox client identifier jwt ams access token.')
|
|
103
|
+
ams_access_token = get_access_token(30, base64_encoding_public_key, sign_by_client_identifier_jwt_token, api_region)
|
|
104
|
+
UI.message('🔐 Sign the knox token using the ams access token and private key.')
|
|
89
105
|
token = generate_signed_jwt(credential, ams_access_token, false)
|
|
90
106
|
|
|
91
107
|
return { token: token[:token] }
|
|
92
108
|
end
|
|
93
109
|
|
|
94
110
|
def self.generate_signed_jwt(credential, token_or_access, is_client_identifier)
|
|
95
|
-
|
|
96
111
|
cred_data = read_credential(credential)
|
|
97
112
|
|
|
98
113
|
private_key_str = cred_data['Private']
|
|
99
114
|
public_key_str = cred_data['Public']
|
|
100
115
|
|
|
101
116
|
private_key = export_credential_private_key(private_key_str)
|
|
102
|
-
public_key = export_credential_public_der_key(public_key_str).gsub(
|
|
117
|
+
public_key = export_credential_public_der_key(public_key_str).gsub("\n", '')
|
|
103
118
|
|
|
104
119
|
payload = { publicKey: public_key }
|
|
105
120
|
payload[:clientIdentifier] = token_or_access if is_client_identifier
|
|
@@ -111,11 +126,10 @@ module Fastlane
|
|
|
111
126
|
payload[:algorithm] = 'RS512'
|
|
112
127
|
payload[:jti] = generate_jwt_id
|
|
113
128
|
|
|
114
|
-
|
|
115
129
|
token = JWT.encode(payload, private_key, 'RS512')
|
|
116
130
|
|
|
117
|
-
UI.success('✅ token generated successfully
|
|
131
|
+
UI.success('✅ token generated successfully')
|
|
118
132
|
return { public_key: public_key, token: token }
|
|
119
133
|
end
|
|
120
134
|
end
|
|
121
|
-
end
|
|
135
|
+
end
|
metadata
CHANGED
|
@@ -1,15 +1,54 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: fastlane-plugin-samsungknox
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- jieey1140
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: jwt
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 2.10.3
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '4'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: 2.10.3
|
|
29
|
+
- - "<"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '4'
|
|
32
|
+
- !ruby/object:Gem::Dependency
|
|
33
|
+
name: multipart-post
|
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - ">="
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '2.0'
|
|
39
|
+
- - "<"
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '3'
|
|
42
|
+
type: :runtime
|
|
43
|
+
prerelease: false
|
|
44
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ">="
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: '2.0'
|
|
49
|
+
- - "<"
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: '3'
|
|
13
52
|
- !ruby/object:Gem::Dependency
|
|
14
53
|
name: bundler
|
|
15
54
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -100,42 +139,28 @@ dependencies:
|
|
|
100
139
|
requirements:
|
|
101
140
|
- - '='
|
|
102
141
|
- !ruby/object:Gem::Version
|
|
103
|
-
version: 1.
|
|
142
|
+
version: 1.87.0
|
|
104
143
|
type: :development
|
|
105
144
|
prerelease: false
|
|
106
145
|
version_requirements: !ruby/object:Gem::Requirement
|
|
107
146
|
requirements:
|
|
108
147
|
- - '='
|
|
109
148
|
- !ruby/object:Gem::Version
|
|
110
|
-
version: 1.
|
|
149
|
+
version: 1.87.0
|
|
111
150
|
- !ruby/object:Gem::Dependency
|
|
112
151
|
name: rubocop-performance
|
|
113
152
|
requirement: !ruby/object:Gem::Requirement
|
|
114
153
|
requirements:
|
|
115
|
-
- -
|
|
116
|
-
- !ruby/object:Gem::Version
|
|
117
|
-
version: '0'
|
|
118
|
-
type: :development
|
|
119
|
-
prerelease: false
|
|
120
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
121
|
-
requirements:
|
|
122
|
-
- - ">="
|
|
123
|
-
- !ruby/object:Gem::Version
|
|
124
|
-
version: '0'
|
|
125
|
-
- !ruby/object:Gem::Dependency
|
|
126
|
-
name: rubocop-require_tools
|
|
127
|
-
requirement: !ruby/object:Gem::Requirement
|
|
128
|
-
requirements:
|
|
129
|
-
- - ">="
|
|
154
|
+
- - '='
|
|
130
155
|
- !ruby/object:Gem::Version
|
|
131
|
-
version:
|
|
156
|
+
version: 1.26.1
|
|
132
157
|
type: :development
|
|
133
158
|
prerelease: false
|
|
134
159
|
version_requirements: !ruby/object:Gem::Requirement
|
|
135
160
|
requirements:
|
|
136
|
-
- -
|
|
161
|
+
- - '='
|
|
137
162
|
- !ruby/object:Gem::Version
|
|
138
|
-
version:
|
|
163
|
+
version: 1.26.1
|
|
139
164
|
- !ruby/object:Gem::Dependency
|
|
140
165
|
name: simplecov
|
|
141
166
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -150,7 +175,6 @@ dependencies:
|
|
|
150
175
|
- - ">="
|
|
151
176
|
- !ruby/object:Gem::Version
|
|
152
177
|
version: '0'
|
|
153
|
-
description:
|
|
154
178
|
email: seihoonkim@redredgroup.com
|
|
155
179
|
executables: []
|
|
156
180
|
extensions: []
|
|
@@ -163,6 +187,7 @@ files:
|
|
|
163
187
|
- lib/fastlane/plugin/samsungknox/helper/knox_configure_error_message.rb
|
|
164
188
|
- lib/fastlane/plugin/samsungknox/helper/knox_configure_library_already_exists_app_version_check.rb
|
|
165
189
|
- lib/fastlane/plugin/samsungknox/helper/knox_configure_library_upload_app.rb
|
|
190
|
+
- lib/fastlane/plugin/samsungknox/helper/knox_http_response.rb
|
|
166
191
|
- lib/fastlane/plugin/samsungknox/helper/knox_token_generator.rb
|
|
167
192
|
- lib/fastlane/plugin/samsungknox/version.rb
|
|
168
193
|
homepage: https://github.com/REDREDGROUP/samsungknox
|
|
@@ -170,7 +195,6 @@ licenses:
|
|
|
170
195
|
- Apache-2.0
|
|
171
196
|
metadata:
|
|
172
197
|
rubygems_mfa_required: 'true'
|
|
173
|
-
post_install_message:
|
|
174
198
|
rdoc_options: []
|
|
175
199
|
require_paths:
|
|
176
200
|
- lib
|
|
@@ -178,15 +202,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
178
202
|
requirements:
|
|
179
203
|
- - ">="
|
|
180
204
|
- !ruby/object:Gem::Version
|
|
181
|
-
version: '
|
|
205
|
+
version: '3.1'
|
|
182
206
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
183
207
|
requirements:
|
|
184
208
|
- - ">="
|
|
185
209
|
- !ruby/object:Gem::Version
|
|
186
210
|
version: '0'
|
|
187
211
|
requirements: []
|
|
188
|
-
rubygems_version: 3.
|
|
189
|
-
signing_key:
|
|
212
|
+
rubygems_version: 3.6.9
|
|
190
213
|
specification_version: 4
|
|
191
214
|
summary: SamsungKnox products to Fastlane https://redredgroup.github.io/samsungknox/docs/knox-fastlane
|
|
192
215
|
test_files: []
|