Class: Aspera::OAuth::Web

Inherits:
Base
  • Object
show all
Defined in:
lib/aspera/oauth/client/web.rb

Overview

Authentication using Web browser

Direct Known Subclasses

Plugins::Shares::SharesWeb

Constant Summary collapse

PKCE_METHOD =

PKCE code challenge method (RFC 7636 §4.2)

'S256'

Class Attribute Summary collapse

Attributes inherited from Base

#api, #params, #path_token

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

absolute_url?, #authorization, #base_parameters, client_id_digest, #create_token_base, #endpoint_api, #endpoint_url, #renew_token, #renewed_authorization, #token

Constructor Details

#initialize(redirect_uri:, path_authorize: 'authorize', pkce: false, **base_params) ⇒ Web

Returns a new instance of Web.

Parameters:

  • redirect_uri (String) —

    url to receive the code after auth (to be exchanged for token)

  • path_authorize (String) (defaults to: 'authorize') —

    path to login page on web app, or absolute URL (http or https), e.g. on another host than the token

  • pkce (Boolean) (defaults to: false) —

    use PKCE (RFC 7636): the code can be exchanged only by this process



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/aspera/oauth/client/web.rb', line 31

def initialize(
  redirect_uri:,
  path_authorize: 'authorize',
  pkce: false,
  **base_params
)
  super(**base_params)
  @redirect_uri = redirect_uri
  @path_authorize = path_authorize
  @pkce = pkce
  uri = URI.parse(@redirect_uri)
  Aspera.assert_values(uri.scheme, %w[http https]) { 'redirect_uri scheme must be http or https' }
  Aspera.assert(!uri.port.nil?, 'redirect_uri must have a port')
  Aspera.assert(WebServerSimple.loopback?(uri.hostname)) { "redirect_uri host must be loopback (localhost, 127.0.0.0/8, ::1): #{uri.host}" }
end

Class Attribute Details

.additional_info ⇒ Object

Returns the value of attribute additional_info.



20
21
22
# File 'lib/aspera/oauth/client/web.rb', line 20

def additional_info
  @additional_info
end

Class Method Details

.pkce_challenge(verifier) ⇒ String

Returns PKCE S256 code challenge: BASE64URL(SHA256(verifier)), without padding (RFC 7636 §4.2).

Parameters:

  • verifier (String) —

    PKCE code verifier

Returns:

  • (String) —

    PKCE S256 code challenge: BASE64URL(SHA256(verifier)), without padding (RFC 7636 §4.2)



24
25
26
# File 'lib/aspera/oauth/client/web.rb', line 24

def pkce_challenge(verifier)
  Base64.urlsafe_encode64(Digest::SHA256.digest(verifier), padding: false)
end

Instance Method Details

#create_token ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/aspera/oauth/client/web.rb', line 47

def create_token
  # generate secure state to check later
  random_state = SecureRandom.uuid
  authorize_params = {response_type: 'code', redirect_uri: @redirect_uri, state: random_state}
  token_params = {}
  if @pkce
    # 32 random bytes: 43 characters of base64url, within the 43..128 allowed (RFC 7636 §4.1)
    code_verifier = SecureRandom.urlsafe_base64(32)
    authorize_params[:code_challenge] = self.class.pkce_challenge(code_verifier)
    authorize_params[:code_challenge_method] = PKCE_METHOD
    token_params[:code_verifier] = code_verifier
  end
   = Rest.build_uri(
    endpoint_url(@path_authorize),
    base_parameters(**authorize_params)
  )
  # here, we need a human to authorize on a web page
  Log.log.debug { "login_page_url=#{}" }
  # receive the code: local web server, or redirect URL pasted by the user (see option ui.web_auth)
   = WebAuth.receiver(@redirect_uri, self.class.additional_info, login_url: , reply_keys: %w[code state])
  # start browser on login page
  WebAuth.(, )
  # wait for code in request
  received_params = .received_request
  Aspera.assert(random_state.eql?(received_params['state'])) { 'wrong received state' }
  # exchange code for token
  create_token_base(
    include_secret: true,
    grant_type:     'authorization_code',
    code:           received_params['code'],
    redirect_uri:   @redirect_uri,
    **token_params
  )
end