Class: Aspera::OAuth::Web
Overview
Authentication using Web browser
Direct Known Subclasses
Constant Summary collapse
- PKCE_METHOD =
PKCE code challenge method (RFC 7636 §4.2)
'S256'
Class Attribute Summary collapse
-
.additional_info ⇒ Object
Returns the value of attribute additional_info.
Attributes inherited from Base
Class Method Summary collapse
-
.pkce_challenge(verifier) ⇒ String
PKCE S256 code challenge: BASE64URL(SHA256(verifier)), without padding (RFC 7636 §4.2).
Instance Method Summary collapse
- #create_token ⇒ Object
-
#initialize(redirect_uri:, path_authorize: 'authorize', pkce: false, **base_params) ⇒ Web
constructor
A new instance of Web.
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.
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 = @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).
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 = {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) [:code_challenge] = self.class.pkce_challenge(code_verifier) [:code_challenge_method] = PKCE_METHOD token_params[:code_verifier] = code_verifier end login_page_url = Rest.build_uri( endpoint_url(@path_authorize), base_parameters(**) ) # here, we need a human to authorize on a web page Log.log.debug { "login_page_url=#{login_page_url}" } # receive the code: local web server, or redirect URL pasted by the user (see option ui.web_auth) login_receiver = WebAuth.receiver(@redirect_uri, self.class.additional_info, login_url: login_page_url, reply_keys: %w[code state]) # start browser on login page WebAuth.open_login_page(login_receiver, login_page_url) # wait for code in request received_params = login_receiver.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 |