Class: Aspera::Api::Faspex

Inherits:
Rest
  • Object
show all
Defined in:
lib/aspera/api/faspex.rb

Constant Summary collapse

RECIPIENT_TYPES =
%w[user workgroup external_user distribution_list shared_inbox].freeze
PACKAGE_TERMINATED =
%w[completed failed].freeze
API_LIST_MAILBOX_TYPES =

list of supported mailbox types (to list packages)

%w[inbox inbox_history inbox_all inbox_all_history outbox outbox_history pending pending_history all].freeze
TRANSFER_CONNECT =

PACKAGE_SEND_FROM_REMOTE_SOURCE = ‘remote_source’ Faspex API v5: get transfer spec for connect

'connect'
ADMIN_RESOURCES =
%i[
  accounts distribution_lists contacts jobs workgroups shared_inboxes nodes oauth_clients registrations saml_configs
  metadata_profiles email_notifications alternate_addresses webhooks
].freeze
JOB_RUNNING =

states for jobs not in final state

%w[queued working].freeze
PATH_STANDARD_ROOT =
'/aspera/faspex'
PATH_API_DETECT =
"#{PATH_API_V5}/#{PATH_HEALTH}"
HEADER_ITERATION_TOKEN =
'X-Aspera-Next-Iteration-Token'
HEADER_FASPEX_VERSION =
'X-IBM-Aspera'
EMAIL_NOTIF_LIST =
%w[
  welcome_email
  forgot_password
  package_received
  package_received_cc
  package_sent_cc
  package_downloaded
  package_downloaded_cc
  workgroup_package
  upload_result
  upload_result_cc
  relay_started_cc
  relay_finished_cc
  relay_error_cc
  shared_inbox_invitation
  shared_inbox_submit
  personal_invitation
  personal_submit
  account_approved
  account_denied
  package_file_processing_failed_sender
  package_file_processing_failed_recipient
  relay_failed_admin
  relay_failed
  admin_sync_failed
  sync_failed
  account_exist
  mfa_code
]

Constants inherited from Rest

Rest::ENTITY_NOT_FOUND, Rest::MAX_ITEMS, Rest::MAX_PAGES, Rest::MIME_JSON, Rest::MIME_TEXT, Rest::MIME_WWW

Instance Attribute Summary collapse

Attributes inherited from Rest

#auth_params, #base_url, #headers

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Rest

basic_authorization, build_uri, #call, #cancel, #create, #delete, h_to_query_array, io_http_session, #lookup_by_name, #oauth, #params, parse_header, php_style, query_to_h, #read, remote_certificate_chain, start_http_session, #update

Constructor Details

#initialize(url:, auth:, password: nil, client_id: nil, client_secret: nil, redirect_uri: nil, username: nil, private_key: nil, passphrase: nil) ⇒ Faspex

Returns a new instance of Faspex.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/aspera/api/faspex.rb', line 119

def initialize(
  url:,
  auth:,
  password: nil,
  client_id: nil,
  client_secret: nil,
  redirect_uri: nil,
  username: nil,
  private_key: nil,
  passphrase: nil
)
  auth = :public_link if self.class.public_link?(url)
  @pub_link_context = nil
  super(**
    case auth
    when :public_link
      # Get URL of final redirect of public link
      redir_url = Rest.new(base_url: url, redirect_max: 3).call(operation: 'GET')[:http].uri.to_s
      Log.dump(:redir_url, redir_url, level: :trace1)
      # get context from query
      encoded_context = Rest.query_to_h(URI.parse(redir_url).query)['context']
      raise ParameterError, 'Bad faspex5 public link, missing context in query' if encoded_context.nil?
      # public link information (contains passcode and allowed usage)
      @pub_link_context = JSON.parse(Base64.decode64(encoded_context))
      Log.dump(:pub_link_context, @pub_link_context, level: :trace1)
      # Get the base url, i.e. .../aspera/faspex
      base_url = redir_url.gsub(%r{/public/.*}, '').gsub(/\?.*/, '')
      # Get web UI client_id and redirect_uri
      # TODO: change this for something more reliable
      config = JSON.parse(Rest.new(base_url: "#{base_url}/config.js", redirect_max: 3).call(operation: 'GET')[:data].sub(/^[^=]+=/, '').gsub(/([a-z_]+):/, '"\1":').delete("\n ").tr("'", '"')).symbolize_keys
      Log.dump(:configjs, config)
      {
        base_url: "#{base_url}/#{PATH_API_V5}",
        auth:     {
          type:         :oauth2,
          base_url:     "#{base_url}/#{PATH_AUTH}",
          grant_method: :faspex_pub_link,
          context:      encoded_context,
          client_id:    config[:client_id],
          redirect_uri: config[:redirect_uri]
        }
      }
    # old: headers:  {'Passcode' => @pub_link_context['passcode']}
    when :boot
      Aspera.assert(password, type: ParameterError){'Missing password'}
      # the password here is the token copied directly from browser in developer mode
      {
        base_url: "#{url}/#{PATH_API_V5}",
        headers:  {'Authorization' => password}
      }
    when :web
      Aspera.assert(client_id, type: ParameterError){'Missing client_id'}
      Aspera.assert(redirect_uri, type: ParameterError){'Missing redirect_uri'}
      # opens a browser and ask user to auth using web
      {
        base_url: "#{url}/#{PATH_API_V5}",
        auth:     {
          type:         :oauth2,
          base_url:     "#{url}/#{PATH_AUTH}",
          grant_method: :web,
          client_id:    client_id,
          redirect_uri: redirect_uri
        }
      }
    when :jwt
      Aspera.assert(client_id, type: ParameterError){'Missing client_id'}
      Aspera.assert(private_key, type: ParameterError){'Missing private_key'}
      {
        base_url: "#{url}/#{PATH_API_V5}",
        auth:     {
          type:            :oauth2,
          base_url:        "#{url}/#{PATH_AUTH}",
          grant_method:    :jwt,
          client_id:       client_id,
          payload:         {
            iss: client_id, # issuer
            aud: client_id, # audience (this field is not clear...)
            sub: "user:#{username}" # subject is a user
          },
          private_key_obj: OpenSSL::PKey::RSA.new(private_key, passphrase),
          headers:         {typ: 'JWT'}
        }
      }
    else Aspera.error_unexpected_value(auth, type: ParameterError){'auth'}
    end
  )
end

Instance Attribute Details

Returns the value of attribute pub_link_context.



117
118
119
# File 'lib/aspera/api/faspex.rb', line 117

def pub_link_context
  @pub_link_context
end

Class Method Details

.public_link?(url) ⇒ Boolean

Returns true if the URL is a public link.

Returns:

  • (Boolean)

    true if the URL is a public link



113
114
115
# File 'lib/aspera/api/faspex.rb', line 113

def public_link?(url)
  url.include?('?context=')
end

Instance Method Details

#auth_apiObject



207
208
209
# File 'lib/aspera/api/faspex.rb', line 207

def auth_api
  Rest.new(**params, base_url: base_url.sub(PATH_API_V5, PATH_AUTH))
end