Class: Signet::OAuth2::Client
- Inherits:
-
Object
- Object
- Signet::OAuth2::Client
- Defined in:
- lib/signet/oauth_2/client.rb
Instance Method Summary collapse
-
#access_token ⇒ String
Returns the access token associated with this client.
-
#access_token=(new_access_token) ⇒ Object
Sets the access token associated with this client.
-
#assertion ⇒ String
Returns the assertion associated with this client.
-
#assertion=(new_assertion) ⇒ Object
Sets the assertion associated with this client.
-
#assertion_type ⇒ String
Returns the assertion type associated with this client.
-
#assertion_type=(new_assertion_type) ⇒ Object
Sets the assertion type associated with this client.
-
#authorization_uri(options = {}) ⇒ Addressable::URI
Returns the authorization URI that the user should be redirected to.
-
#authorization_uri=(new_authorization_uri) ⇒ Object
Sets the authorization URI for this client.
-
#client_id ⇒ String
Returns the client identifier for this client.
-
#client_id=(new_client_id) ⇒ Object
Sets the client identifier for this client.
-
#client_secret ⇒ String
Returns the client secret for this client.
-
#client_secret=(new_client_secret) ⇒ Object
Sets the client secret for this client.
-
#code ⇒ String
Returns the authorization code issued to this client.
-
#code=(new_code) ⇒ Object
Sets the authorization code issued to this client.
-
#expired? ⇒ TrueClass, FalseClass
Returns true if the access token has expired.
-
#expires_at ⇒ Integer
Returns the timestamp the access token will expire at.
-
#expires_in ⇒ Integer
Returns the lifetime of the access token in seconds.
-
#expires_in=(new_expires_in) ⇒ Object
Sets the lifetime of the access token in seconds.
- #fetch_access_token(options = {}) ⇒ Object
- #fetch_access_token!(options = {}) ⇒ Object
-
#fetch_protected_resource(options = {}) ⇒ Array
Transmits a request for a protected resource.
-
#generate_access_token_request ⇒ Array
Generates a request for token credentials.
-
#generate_authenticated_request(options = {}) ⇒ Array
Generates an authenticated request for protected resources.
-
#grant_type ⇒ String
Returns the inferred grant type, based on the current state of the client object.
-
#initialize(options = {}) ⇒ Client
constructor
Creates an OAuth 2.0 client.
-
#issued_at ⇒ Integer
Returns the timestamp the access token was issued at.
-
#issued_at=(new_issued_at) ⇒ Object
Sets the timestamp the access token was issued at.
-
#password ⇒ String
Returns the password associated with this client.
-
#password=(new_password) ⇒ Object
Sets the password associated with this client.
-
#redirect_uri ⇒ String
Returns the redirect URI for this client.
-
#redirect_uri=(new_redirect_uri) ⇒ Object
Sets the redirect URI for this client.
-
#refresh_token ⇒ String
Returns the refresh token associated with this client.
-
#refresh_token=(new_refresh_token) ⇒ Object
Sets the refresh token associated with this client.
-
#scope ⇒ Array
Returns the scope for this client.
-
#scope=(new_scope) ⇒ Object
Sets the scope for this client.
-
#state ⇒ String
Returns the client’s current state value.
-
#state=(new_state) ⇒ Object
Sets the client’s current state value.
-
#token_credential_uri ⇒ Addressable::URI
Returns the token credential URI for this client.
-
#token_credential_uri=(new_token_credential_uri) ⇒ Object
Sets the token credential URI for this client.
-
#update!(options = {}) ⇒ Object
Updates an OAuth 2.0 client.
-
#update_token!(options = {}) ⇒ Object
Updates an OAuth 2.0 client.
-
#username ⇒ String
Returns the username associated with this client.
-
#username=(new_username) ⇒ Object
Sets the username associated with this client.
Constructor Details
#initialize(options = {}) ⇒ Client
Creates an OAuth 2.0 client.
78 79 80 |
# File 'lib/signet/oauth_2/client.rb', line 78 def initialize(={}) self.update!() end |
Instance Method Details
#access_token ⇒ String
Returns the access token associated with this client.
492 493 494 |
# File 'lib/signet/oauth_2/client.rb', line 492 def access_token return @access_token end |
#access_token=(new_access_token) ⇒ Object
Sets the access token associated with this client.
501 502 503 |
# File 'lib/signet/oauth_2/client.rb', line 501 def access_token=(new_access_token) @access_token = new_access_token end |
#assertion ⇒ String
Returns the assertion associated with this client. Used only by the assertion access grant type.
457 458 459 |
# File 'lib/signet/oauth_2/client.rb', line 457 def assertion return @assertion end |
#assertion=(new_assertion) ⇒ Object
Sets the assertion associated with this client. Used only by the assertion access grant type.
467 468 469 |
# File 'lib/signet/oauth_2/client.rb', line 467 def assertion=(new_assertion) @assertion = new_assertion end |
#assertion_type ⇒ String
Returns the assertion type associated with this client. Used only by the assertion access grant type.
433 434 435 |
# File 'lib/signet/oauth_2/client.rb', line 433 def assertion_type return @assertion_type end |
#assertion_type=(new_assertion_type) ⇒ Object
Sets the assertion type associated with this client. Used only by the assertion access grant type.
443 444 445 446 447 448 449 450 |
# File 'lib/signet/oauth_2/client.rb', line 443 def assertion_type=(new_assertion_type) new_assertion_type = Addressable::URI.parse(new_assertion_type) if new_assertion_type == nil || new_assertion_type.absolute? @assertion_type = new_assertion_type else raise ArgumentError, "Assertion type must be an absolute URI." end end |
#authorization_uri(options = {}) ⇒ Addressable::URI
Returns the authorization URI that the user should be redirected to.
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/signet/oauth_2/client.rb', line 196 def (={}) return nil if @authorization_uri == nil unless [:response_type] [:response_type] = :code end [:client_id] ||= self.client_id [:redirect_uri] ||= self.redirect_uri unless [:client_id] raise ArgumentError, "Missing required client identifier." end unless [:redirect_uri] raise ArgumentError, "Missing required redirect URI." end if ![:scope] && self.scope [:scope] = self.scope.join(' ') end [:state] = self.state unless [:state] uri = Addressable::URI.parse( ::Signet::OAuth2.( @authorization_uri, ) ) if uri.normalized_scheme != 'https' raise Signet::UnsafeOperationError, 'Authorization endpoint must be protected by TLS.' end return uri end |
#authorization_uri=(new_authorization_uri) ⇒ Object
Sets the authorization URI for this client.
230 231 232 233 234 235 236 237 238 |
# File 'lib/signet/oauth_2/client.rb', line 230 def () if != nil = Addressable::URI.parse() @authorization_uri = else @authorization_uri = nil end end |
#client_id ⇒ String
Returns the client identifier for this client.
267 268 269 |
# File 'lib/signet/oauth_2/client.rb', line 267 def client_id return @client_id end |
#client_id=(new_client_id) ⇒ Object
Sets the client identifier for this client.
276 277 278 |
# File 'lib/signet/oauth_2/client.rb', line 276 def client_id=(new_client_id) @client_id = new_client_id end |
#client_secret ⇒ String
Returns the client secret for this client.
284 285 286 |
# File 'lib/signet/oauth_2/client.rb', line 284 def client_secret return @client_secret end |
#client_secret=(new_client_secret) ⇒ Object
Sets the client secret for this client.
293 294 295 |
# File 'lib/signet/oauth_2/client.rb', line 293 def client_secret=(new_client_secret) @client_secret = new_client_secret end |
#code ⇒ String
Returns the authorization code issued to this client. Used only by the authorization code access grant type.
354 355 356 |
# File 'lib/signet/oauth_2/client.rb', line 354 def code return @code end |
#code=(new_code) ⇒ Object
Sets the authorization code issued to this client. Used only by the authorization code access grant type.
364 365 366 |
# File 'lib/signet/oauth_2/client.rb', line 364 def code=(new_code) @code = new_code end |
#expired? ⇒ TrueClass, FalseClass
Returns true if the access token has expired.
558 559 560 |
# File 'lib/signet/oauth_2/client.rb', line 558 def expired? return Time.now >= self.expires_at end |
#expires_at ⇒ Integer
Returns the timestamp the access token will expire at.
545 546 547 548 549 550 551 |
# File 'lib/signet/oauth_2/client.rb', line 545 def expires_at if @issued_at && @expires_in return @issued_at + @expires_in else return nil end end |
#expires_in ⇒ Integer
Returns the lifetime of the access token in seconds.
509 510 511 |
# File 'lib/signet/oauth_2/client.rb', line 509 def expires_in return @expires_in end |
#expires_in=(new_expires_in) ⇒ Object
Sets the lifetime of the access token in seconds. Resets the issued timestamp.
519 520 521 522 |
# File 'lib/signet/oauth_2/client.rb', line 519 def expires_in=(new_expires_in) @expires_in = new_expires_in.to_i @issued_at = Time.now end |
#fetch_access_token(options = {}) ⇒ Object
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 |
# File 'lib/signet/oauth_2/client.rb', line 638 def fetch_access_token(={}) adapter = [:adapter] unless adapter require 'httpadapter' require 'httpadapter/adapters/net_http' adapter = HTTPAdapter::NetHTTPAdapter.new end connection = [:connection] request = self.generate_access_token_request response = adapter.transmit(request, connection) status, headers, body = response merged_body = StringIO.new body.each do |chunk| merged_body.write(chunk) end body = merged_body.string if status.to_i == 200 return ::Signet::OAuth2.parse_json_credentials(body) elsif [400, 401, 403].include?(status.to_i) = 'Authorization failed.' if body.strip.length > 0 += " Server message:\n#{body.strip}" end raise ::Signet::AuthorizationError.new( , :request => request, :response => response ) else = "Unexpected status code: #{status}." if body.strip.length > 0 += " Server message:\n#{body.strip}" end raise ::Signet::AuthorizationError.new( , :request => request, :response => response ) end end |
#fetch_access_token!(options = {}) ⇒ Object
675 676 677 678 679 680 681 682 683 684 685 686 |
# File 'lib/signet/oauth_2/client.rb', line 675 def fetch_access_token!(={}) token_hash = self.fetch_access_token() if token_hash # No-op for grant types other than `authorization_code`. # An authorization code is a one-time use token and is immediately # revoked after usage. self.code = nil self.issued_at = Time.now self.update_token!(token_hash) end return token_hash end |
#fetch_protected_resource(options = {}) ⇒ Array
Transmits a request for a protected resource.
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 |
# File 'lib/signet/oauth_2/client.rb', line 812 def fetch_protected_resource(={}) adapter = [:adapter] unless adapter require 'httpadapter' require 'httpadapter/adapters/net_http' adapter = HTTPAdapter::NetHTTPAdapter.new end connection = [:connection] request = self.generate_authenticated_request() response = adapter.transmit(request, connection) status, headers, body = response merged_body = StringIO.new body.each do |chunk| merged_body.write(chunk) end body = merged_body.string if status.to_i == 401 # When accessing a protected resource, we only want to raise an # error for 401 responses. = 'Authorization failed.' if body.strip.length > 0 += " Server message:\n#{body.strip}" end raise ::Signet::AuthorizationError.new( , :request => request, :response => response ) else return response end end |
#generate_access_token_request ⇒ Array
Generates a request for token credentials.
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 |
# File 'lib/signet/oauth_2/client.rb', line 594 def generate_access_token_request if self.token_credential_uri == nil raise ArgumentError, 'Missing token endpoint URI.' end if self.client_id == nil raise ArgumentError, 'Missing client identifier.' end if self.client_secret == nil raise ArgumentError, 'Missing client secret.' end if self.redirect_uri && !self.code # Grant type can be assumed to be `authorization_code` because of # the presence of the redirect URI. raise ArgumentError, 'Missing authorization code.' end method = 'POST' parameters = {"grant_type" => self.grant_type} case self.grant_type when 'authorization_code' parameters['code'] = self.code parameters['redirect_uri'] = self.redirect_uri when 'password' parameters['username'] = self.username parameters['password'] = self.password when 'assertion' parameters['assertion_type'] = self.assertion_type parameters['assertion'] = self.assertion when 'refresh_token' parameters['refresh_token'] = self.refresh_token end parameters['client_id'] = self.client_id parameters['client_secret'] = self.client_secret headers = [ ['Cache-Control', 'no-store'], ['Content-Type', 'application/x-www-form-urlencoded'] ] return [ method, self.token_credential_uri.to_str, headers, [Addressable::URI.form_encode(parameters)] ] end |
#generate_authenticated_request(options = {}) ⇒ Array
Generates an authenticated request for protected resources.
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 |
# File 'lib/signet/oauth_2/client.rb', line 709 def generate_authenticated_request(={}) if self.access_token == nil raise ArgumentError, 'Missing access token.' end = { :realm => nil }.merge() if [:request] if [:request].kind_of?(Array) request = [:request] elsif [:adapter] request = [:adapter].adapt_request([:request]) end method, uri, headers, body = request else method = [:method] || 'GET' uri = [:uri] headers = [:headers] || [] body = [:body] || '' end headers = headers.to_a if headers.kind_of?(Hash) request_components = { :method => method, :uri => uri, :headers => headers, :body => body } # Verify that we have all pieces required to return an HTTP request request_components.each do |(key, value)| unless value raise ArgumentError, "Missing :#{key} parameter." end end if !body.kind_of?(String) && body.respond_to?(:each) # Just in case we get a chunked body merged_body = StringIO.new body.each do |chunk| merged_body.write(chunk) end body = merged_body.string end if !body.kind_of?(String) raise TypeError, "Expected String, got #{body.class}." end method = method.to_s.upcase headers << [ 'Authorization', ::Signet::OAuth2.( self.access_token, [:realm] ? ['realm', [:realm]] : nil ) ] headers << ['Cache-Control', 'no-store'] return [method, uri.to_str, headers, [body]] end |
#grant_type ⇒ String
Returns the inferred grant type, based on the current state of the client object. Returns ‘“none”` if the client has insufficient information to make an in-band authorization request.
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 |
# File 'lib/signet/oauth_2/client.rb', line 569 def grant_type if self.code && self.redirect_uri return 'authorization_code' elsif self.assertion && self.assertion_type return 'assertion' elsif self.refresh_token return 'refresh_token' elsif self.username && self.password return 'password' else # We don't have sufficient auth information, assume an out-of-band # authorization arrangement between the client and server. return 'none' end end |
#issued_at ⇒ Integer
Returns the timestamp the access token was issued at.
528 529 530 |
# File 'lib/signet/oauth_2/client.rb', line 528 def issued_at return @issued_at end |
#issued_at=(new_issued_at) ⇒ Object
Sets the timestamp the access token was issued at.
537 538 539 |
# File 'lib/signet/oauth_2/client.rb', line 537 def issued_at=(new_issued_at) @issued_at = new_issued_at end |
#password ⇒ String
Returns the password associated with this client. Used only by the resource owner password credential access grant type.
414 415 416 |
# File 'lib/signet/oauth_2/client.rb', line 414 def password return @password end |
#password=(new_password) ⇒ Object
Sets the password associated with this client. Used only by the resource owner password credential access grant type.
424 425 426 |
# File 'lib/signet/oauth_2/client.rb', line 424 def password=(new_password) @password = new_password end |
#redirect_uri ⇒ String
Returns the redirect URI for this client.
372 373 374 |
# File 'lib/signet/oauth_2/client.rb', line 372 def redirect_uri return @redirect_uri end |
#redirect_uri=(new_redirect_uri) ⇒ Object
Sets the redirect URI for this client.
381 382 383 384 385 386 387 388 |
# File 'lib/signet/oauth_2/client.rb', line 381 def redirect_uri=(new_redirect_uri) new_redirect_uri = Addressable::URI.parse(new_redirect_uri) if new_redirect_uri == nil || new_redirect_uri.absolute? @redirect_uri = new_redirect_uri else raise ArgumentError, "Redirect URI must be an absolute URI." end end |
#refresh_token ⇒ String
Returns the refresh token associated with this client.
475 476 477 |
# File 'lib/signet/oauth_2/client.rb', line 475 def refresh_token return @refresh_token end |
#refresh_token=(new_refresh_token) ⇒ Object
Sets the refresh token associated with this client.
484 485 486 |
# File 'lib/signet/oauth_2/client.rb', line 484 def refresh_token=(new_refresh_token) @refresh_token = new_refresh_token end |
#scope ⇒ Array
Returns the scope for this client. Scope is a list of access ranges defined by the authorization server.
302 303 304 |
# File 'lib/signet/oauth_2/client.rb', line 302 def scope return @scope end |
#scope=(new_scope) ⇒ Object
Sets the scope for this client.
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/signet/oauth_2/client.rb', line 313 def scope=(new_scope) case new_scope when Array new_scope.each do |scope| if scope.include?(' ') raise Signet::ParseError, "Individual scopes cannot contain the space character." end end @scope = new_scope when String @scope = new_scope.split(' ') when nil @scope = nil else raise TypeError, "Expected Array or String, got #{new_scope.class}" end end |
#state ⇒ String
Returns the client’s current state value.
336 337 338 |
# File 'lib/signet/oauth_2/client.rb', line 336 def state return @state end |
#state=(new_state) ⇒ Object
Sets the client’s current state value.
345 346 347 |
# File 'lib/signet/oauth_2/client.rb', line 345 def state=(new_state) @state = new_state end |
#token_credential_uri ⇒ Addressable::URI
Returns the token credential URI for this client.
244 245 246 |
# File 'lib/signet/oauth_2/client.rb', line 244 def token_credential_uri return @token_credential_uri end |
#token_credential_uri=(new_token_credential_uri) ⇒ Object
Sets the token credential URI for this client.
253 254 255 256 257 258 259 260 261 |
# File 'lib/signet/oauth_2/client.rb', line 253 def token_credential_uri=(new_token_credential_uri) if new_token_credential_uri != nil new_token_credential_uri = Addressable::URI.parse(new_token_credential_uri) @token_credential_uri = new_token_credential_uri else @token_credential_uri = nil end end |
#update!(options = {}) ⇒ Object
Updates an OAuth 2.0 client.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/signet/oauth_2/client.rb', line 134 def update!(={}) # Normalize key to String to allow indifferent access. = .inject({}) do |accu, (key, value)| accu[key.to_s] = value accu end self. = ["authorization_uri"] self.token_credential_uri = ["token_credential_uri"] self.client_id = ["client_id"] self.client_secret = ["client_secret"] self.scope = ["scope"] self.state = ["state"] self.code = ["code"] self.redirect_uri = ["redirect_uri"] self.username = ["username"] self.password = ["password"] self.assertion_type = ["assertion_type"] self.assertion = ["assertion"] self.update_token!() return self end |
#update_token!(options = {}) ⇒ Object
Updates an OAuth 2.0 client.
178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/signet/oauth_2/client.rb', line 178 def update_token!(={}) # Normalize key to String to allow indifferent access. = .inject({}) do |accu, (key, value)| accu[key.to_s] = value accu end self.refresh_token = ["refresh_token"] self.access_token = ["access_token"] self.expires_in = ["expires_in"] return self end |
#username ⇒ String
Returns the username associated with this client. Used only by the resource owner password credential access grant type.
395 396 397 |
# File 'lib/signet/oauth_2/client.rb', line 395 def username return @username end |
#username=(new_username) ⇒ Object
Sets the username associated with this client. Used only by the resource owner password credential access grant type.
405 406 407 |
# File 'lib/signet/oauth_2/client.rb', line 405 def username=(new_username) @username = new_username end |