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.
-
#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.
-
#decoded_id_token(public_key = nil) ⇒ String
Returns the decoded ID token associated with 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.
-
#extension_parameters ⇒ Hash
Returns the set of extension parameters used by the client.
-
#extension_parameters=(new_extension_parameters) ⇒ Object
Sets extension parameters used by the client.
- #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(options = {}) ⇒ 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.
- #grant_type=(new_grant_type) ⇒ Object
-
#id_token ⇒ String
Returns the ID token associated with this client.
-
#id_token=(new_id_token) ⇒ Object
Sets the ID token associated with this client.
-
#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.
85 86 87 |
# File 'lib/signet/oauth_2/client.rb', line 85 def initialize(={}) self.update!() end |
Instance Method Details
#access_token ⇒ String
Returns the access token associated with this client.
499 500 501 |
# File 'lib/signet/oauth_2/client.rb', line 499 def access_token return @access_token ||= nil end |
#access_token=(new_access_token) ⇒ Object
Sets the access token associated with this client.
508 509 510 |
# File 'lib/signet/oauth_2/client.rb', line 508 def access_token=(new_access_token) @access_token = new_access_token end |
#authorization_uri(options = {}) ⇒ Addressable::URI
Returns the authorization URI that the user should be redirected to.
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/signet/oauth_2/client.rb', line 215 def (={}) return nil if @authorization_uri == nil unless [:response_type] [:response_type] = :code end unless [:access_type] [:access_type] = :offline end unless [:approval_prompt] # This default will likely change in the future. [:approval_prompt] = :force end [:client_id] ||= self.client_id [:redirect_uri] ||= self.redirect_uri if ![: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.
256 257 258 259 260 261 262 263 264 |
# File 'lib/signet/oauth_2/client.rb', line 256 def () if != nil = Addressable::URI.parse() @authorization_uri = else @authorization_uri = nil end end |
#client_id ⇒ String
Returns the client identifier for this client.
293 294 295 |
# File 'lib/signet/oauth_2/client.rb', line 293 def client_id return @client_id end |
#client_id=(new_client_id) ⇒ Object
Sets the client identifier for this client.
302 303 304 |
# File 'lib/signet/oauth_2/client.rb', line 302 def client_id=(new_client_id) @client_id = new_client_id end |
#client_secret ⇒ String
Returns the client secret for this client.
310 311 312 |
# File 'lib/signet/oauth_2/client.rb', line 310 def client_secret return @client_secret end |
#client_secret=(new_client_secret) ⇒ Object
Sets the client secret for this client.
319 320 321 |
# File 'lib/signet/oauth_2/client.rb', line 319 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.
380 381 382 |
# File 'lib/signet/oauth_2/client.rb', line 380 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.
390 391 392 |
# File 'lib/signet/oauth_2/client.rb', line 390 def code=(new_code) @code = new_code end |
#decoded_id_token(public_key = nil) ⇒ String
Returns the decoded ID token associated with this client.
537 538 539 |
# File 'lib/signet/oauth_2/client.rb', line 537 def decoded_id_token(public_key=nil) JWT.decode(self.id_token, public_key, !!public_key) end |
#expired? ⇒ TrueClass, FalseClass
Returns true if the access token has expired.
598 599 600 |
# File 'lib/signet/oauth_2/client.rb', line 598 def expired? return self.expires_at != nil && Time.now >= self.expires_at end |
#expires_at ⇒ Integer
Returns the timestamp the access token will expire at.
585 586 587 588 589 590 591 |
# File 'lib/signet/oauth_2/client.rb', line 585 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.
545 546 547 |
# File 'lib/signet/oauth_2/client.rb', line 545 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.
555 556 557 558 559 560 561 562 |
# File 'lib/signet/oauth_2/client.rb', line 555 def expires_in=(new_expires_in) if new_expires_in != nil @expires_in = new_expires_in.to_i @issued_at = Time.now else @expires_in, @issued_at = nil, nil end end |
#extension_parameters ⇒ Hash
Returns the set of extension parameters used by the client. Used only by extension access grant types.
459 460 461 |
# File 'lib/signet/oauth_2/client.rb', line 459 def extension_parameters return @extension_parameters ||= {} end |
#extension_parameters=(new_extension_parameters) ⇒ Object
Sets extension parameters used by the client. Used only by extension access grant types.
469 470 471 472 473 474 475 476 |
# File 'lib/signet/oauth_2/client.rb', line 469 def extension_parameters=(new_extension_parameters) if new_extension_parameters.respond_to?(:to_hash) @extension_parameters = new_extension_parameters.to_hash else raise TypeError, "Expected Hash, got #{new_extension_parameters.class}." end end |
#fetch_access_token(options = {}) ⇒ Object
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 |
# File 'lib/signet/oauth_2/client.rb', line 688 def fetch_access_token(={}) [:connection] ||= Faraday.default_connection request = self.generate_access_token_request() request_env = request.to_env([:connection]) response = [:connection].app.call(request_env) if response.status.to_i == 200 return ::Signet::OAuth2.parse_json_credentials(response.body) elsif [400, 401, 403].include?(response.status.to_i) = 'Authorization failed.' if response.body.to_s.strip.length > 0 += " Server message:\n#{response.body.to_s.strip}" end raise ::Signet::AuthorizationError.new( , :request => request, :response => response ) else = "Unexpected status code: #{response.status}." if response.body.to_s.strip.length > 0 += " Server message:\n#{response.body.to_s.strip}" end raise ::Signet::AuthorizationError.new( , :request => request, :response => response ) end end |
#fetch_access_token!(options = {}) ⇒ Object
714 715 716 717 718 719 720 721 722 723 724 725 |
# File 'lib/signet/oauth_2/client.rb', line 714 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.
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 |
# File 'lib/signet/oauth_2/client.rb', line 856 def fetch_protected_resource(={}) [:connection] ||= Faraday.default_connection request = self.generate_authenticated_request() request_env = request.to_env([:connection]) response = [:connection].app.call(request_env) if response.status.to_i == 401 # When accessing a protected resource, we only want to raise an # error for 401 responses. = 'Authorization failed.' if response.body.to_s.strip.length > 0 += " Server message:\n#{response.body.to_s.strip}" end raise ::Signet::AuthorizationError.new( , :request => request, :response => response ) else return response end end |
#generate_access_token_request(options = {}) ⇒ Array
Generates a request for token credentials.
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 674 675 676 677 678 679 680 681 682 683 684 685 686 |
# File 'lib/signet/oauth_2/client.rb', line 647 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 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 'refresh_token' parameters['refresh_token'] = self.refresh_token else if self.redirect_uri # Grant type was intended to be `authorization_code` because of # the presence of the redirect URI. raise ArgumentError, 'Missing authorization code.' end 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 Faraday::Request.create(method.to_s.downcase.to_sym) do |req| req.url(Addressable::URI.parse(self.token_credential_uri)) req.headers = Faraday::Utils::Headers.new(headers) req.body = Addressable::URI.form_encode(parameters) end end |
#generate_authenticated_request(options = {}) ⇒ Array
Generates an authenticated request for protected resources.
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 |
# File 'lib/signet/oauth_2/client.rb', line 748 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) method, uri, headers, body = [:request] elsif [:request].kind_of?(Faraday::Request) unless [:connection] raise ArgumentError, "Faraday::Request used, requires a connection to be provided." end method = [:request].method.to_s.downcase.to_sym uri = [:connection].build_url( [:request].path, [:request].params ) headers = [:request].headers || {} body = [:request].body || '' end 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.downcase.to_sym headers << [ 'Authorization', ::Signet::OAuth2.( self.access_token, [:realm] ? [['realm', [:realm]]] : nil ) ] headers << ['Cache-Control', 'no-store'] return Faraday::Request.create(method.to_s.downcase.to_sym) do |req| req.url(Addressable::URI.parse(uri)) req.headers = Faraday::Utils::Headers.new(headers) req.body = body end 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.
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 |
# File 'lib/signet/oauth_2/client.rb', line 609 def grant_type if @grant_type return @grant_type else if self.code && self.redirect_uri 'authorization_code' elsif self.refresh_token 'refresh_token' elsif self.username && self.password 'password' else # We don't have sufficient auth information, assume an out-of-band # authorization arrangement between the client and server, or an # extension grant type. nil end end end |
#grant_type=(new_grant_type) ⇒ Object
628 629 630 631 632 633 634 635 636 |
# File 'lib/signet/oauth_2/client.rb', line 628 def grant_type=(new_grant_type) case new_grant_type when 'authorization_code', 'refresh_token', 'password', 'client_credentials' @grant_type = new_grant_type else @grant_type = Addressable::URI.parse(new_grant_type) end end |
#id_token ⇒ String
Returns the ID token associated with this client.
516 517 518 |
# File 'lib/signet/oauth_2/client.rb', line 516 def id_token return @id_token ||= nil end |
#id_token=(new_id_token) ⇒ Object
Sets the ID token associated with this client.
525 526 527 |
# File 'lib/signet/oauth_2/client.rb', line 525 def id_token=(new_id_token) @id_token = new_id_token end |
#issued_at ⇒ Integer
Returns the timestamp the access token was issued at.
568 569 570 |
# File 'lib/signet/oauth_2/client.rb', line 568 def issued_at return @issued_at end |
#issued_at=(new_issued_at) ⇒ Object
Sets the timestamp the access token was issued at.
577 578 579 |
# File 'lib/signet/oauth_2/client.rb', line 577 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.
440 441 442 |
# File 'lib/signet/oauth_2/client.rb', line 440 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.
450 451 452 |
# File 'lib/signet/oauth_2/client.rb', line 450 def password=(new_password) @password = new_password end |
#redirect_uri ⇒ String
Returns the redirect URI for this client.
398 399 400 |
# File 'lib/signet/oauth_2/client.rb', line 398 def redirect_uri return @redirect_uri end |
#redirect_uri=(new_redirect_uri) ⇒ Object
Sets the redirect URI for this client.
407 408 409 410 411 412 413 414 |
# File 'lib/signet/oauth_2/client.rb', line 407 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.
482 483 484 |
# File 'lib/signet/oauth_2/client.rb', line 482 def refresh_token return @refresh_token ||= nil end |
#refresh_token=(new_refresh_token) ⇒ Object
Sets the refresh token associated with this client.
491 492 493 |
# File 'lib/signet/oauth_2/client.rb', line 491 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.
328 329 330 |
# File 'lib/signet/oauth_2/client.rb', line 328 def scope return @scope end |
#scope=(new_scope) ⇒ Object
Sets the scope for this client.
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/signet/oauth_2/client.rb', line 339 def scope=(new_scope) case new_scope when Array new_scope.each do |scope| if scope.include?(' ') raise ArgumentError, "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.
362 363 364 |
# File 'lib/signet/oauth_2/client.rb', line 362 def state return @state end |
#state=(new_state) ⇒ Object
Sets the client’s current state value.
371 372 373 |
# File 'lib/signet/oauth_2/client.rb', line 371 def state=(new_state) @state = new_state end |
#token_credential_uri ⇒ Addressable::URI
Returns the token credential URI for this client.
270 271 272 |
# File 'lib/signet/oauth_2/client.rb', line 270 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.
279 280 281 282 283 284 285 286 287 |
# File 'lib/signet/oauth_2/client.rb', line 279 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.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/signet/oauth_2/client.rb', line 139 def update!(={}) # Normalize key to String to allow indifferent access. = .inject({}) { |accu, (k, v)| accu[k.to_s] = v; accu } 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.extension_parameters = ["extension_parameters"] || {} self.update_token!() return self end |
#update_token!(options = {}) ⇒ Object
Updates an OAuth 2.0 client.
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/signet/oauth_2/client.rb', line 183 def update_token!(={}) # Normalize key to String to allow indifferent access. = .inject({}) { |accu, (k, v)| accu[k.to_s] = v; accu } self.access_token = ["access_token"] if ["access_token"] self.expires_in = ["expires_in"] if ["expires_in"] # The refresh token may not be returned in a token response. # In which case, the old one should continue to be used. if ["refresh_token"] self.refresh_token = ["refresh_token"] end # The ID token may not be returned in a token response. # In which case, the old one should continue to be used. if ["id_token"] self.id_token = ["id_token"] end # By default, the token is issued at `Time.now` when `expires_in` is # set, but this can be used to supply a more precise time. if ["issued_at"] self.issued_at = ["issued_at"] end return self end |
#username ⇒ String
Returns the username associated with this client. Used only by the resource owner password credential access grant type.
421 422 423 |
# File 'lib/signet/oauth_2/client.rb', line 421 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.
431 432 433 |
# File 'lib/signet/oauth_2/client.rb', line 431 def username=(new_username) @username = new_username end |