Class: Signet::OAuth2::Client
- Inherits:
-
Object
- Object
- Signet::OAuth2::Client
- Defined in:
- lib/signet/oauth_2/client.rb
Constant Summary collapse
- OOB_MODES =
["urn:ietf:wg:oauth:2.0:oob:auto", "urn:ietf:wg:oauth:2.0:oob", "oob"].freeze
Instance Attribute Summary collapse
-
#sub ⇒ Object
The target "sub" when issuing assertions.
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.
-
#access_type ⇒ String, Symbol
Returns the current access type parameter for #authorization_uri.
-
#access_type=(new_access_type) ⇒ Object
Sets the current access type parameter for #authorization_uri.
-
#additional_parameters ⇒ Hash
Returns the set of additional (non standard) parameters to be used by the client.
-
#additional_parameters=(new_additional_parameters) ⇒ Object
Sets additional (non standard) parameters to be used by the client.
-
#audience ⇒ String
Returns the target audience ID when issuing assertions.
-
#audience=(new_audience) ⇒ Object
Sets the target audience ID when issuing assertions.
-
#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.
-
#clear_credentials! ⇒ Object
Removes all credentials from the 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.
-
#coerce_uri(incoming_uri) ⇒ Object
Addressable expects URIs formatted as hashes to come in with symbols as keys.
-
#decoded_id_token(public_key = nil, options = {}, &keyfinder) ⇒ String
Returns the decoded ID token associated with this client.
-
#expired? ⇒ TrueClass, FalseClass
Returns true if the access token has expired.
-
#expires_at ⇒ Time?
Returns the timestamp the access token will expire at.
-
#expires_at=(new_expires_at) ⇒ Object
Limits the lifetime of the access token as number of seconds since the Epoch.
-
#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.
-
#expires_within?(sec) ⇒ TrueClass, FalseClass
Returns true if the access token has expired or expires within the next n seconds.
-
#expiry ⇒ Integer
Returns the number of seconds assertions are valid for Used only by the assertion grant type.
-
#expiry=(new_expiry) ⇒ Object
Sets the number of seconds assertions are valid for Used only by the assertion grant type.
-
#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_authenticated_request(options = {}) ⇒ Faraday::Request
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
-
#granted_scopes ⇒ Array?
Returns the scopes granted by the authorization server.
-
#granted_scopes=(new_granted_scopes) ⇒ Object
Sets the scopes returned by authorization server for this client.
-
#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 ⇒ Time?
Returns the timestamp the access token was issued at.
-
#issued_at=(new_issued_at) ⇒ Object
Sets the timestamp the access token was issued at.
-
#issuer ⇒ String
Returns the issuer ID associated with this client.
-
#issuer=(new_issuer) ⇒ Object
Sets the issuer ID associated with this client.
-
#password ⇒ String
Returns the password associated with this client.
-
#password=(new_password) ⇒ Object
Sets the password associated with this client.
-
#principal ⇒ String
(also: #person)
Returns the target resource owner for impersonation.
-
#principal=(new_person) ⇒ Object
(also: #person=)
Sets the target resource owner for impersonation.
-
#redirect_uri ⇒ String
Returns the redirect URI for this client.
-
#redirect_uri=(new_redirect_uri) ⇒ Object
Sets the redirect URI for this client.
-
#refresh!(options = {}) ⇒ Object
Refresh the access token, if possible.
-
#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.
-
#signing_algorithm ⇒ String
Algorithm used for signing JWTs.
-
#signing_key ⇒ String, OpenSSL::PKey
Returns the signing key associated with this client.
-
#signing_key=(new_key) ⇒ Object
Sets the signing key when issuing assertions.
-
#state ⇒ String
Returns the client's current state value.
-
#state=(new_state) ⇒ Object
Sets the client's current state value.
-
#target_audience ⇒ String
Returns the final target audience for ID tokens fetched by this client.
-
#target_audience=(new_target_audience) ⇒ Object
Sets the final target audience for ID tokens fetched by this client.
-
#to_json(*_args) ⇒ String
Serialize the client object to JSON.
- #to_jwt(options = {}) ⇒ Object
-
#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.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/signet/oauth_2/client.rb', line 97 def initialize = {} @authorization_uri = nil @token_credential_uri = nil @client_id = nil @client_secret = nil @code = nil @expires_at = nil @issued_at = nil @issuer = nil @password = nil @principal = nil @redirect_uri = nil @scope = nil @target_audience = nil @state = nil @username = nil @access_type = nil @granted_scopes = nil update! end |
Instance Attribute Details
#sub ⇒ Object
The target "sub" when issuing assertions. Used in some Admin SDK APIs.
601 602 603 |
# File 'lib/signet/oauth_2/client.rb', line 601 def sub @sub end |
Instance Method Details
#access_token ⇒ String
Returns the access token associated with this client.
715 716 717 |
# File 'lib/signet/oauth_2/client.rb', line 715 def access_token @access_token ||= nil end |
#access_token=(new_access_token) ⇒ Object
Sets the access token associated with this client.
724 725 726 |
# File 'lib/signet/oauth_2/client.rb', line 724 def access_token= new_access_token @access_token = new_access_token end |
#access_type ⇒ String, Symbol
Returns the current access type parameter for #authorization_uri.
342 343 344 |
# File 'lib/signet/oauth_2/client.rb', line 342 def access_type @access_type end |
#access_type=(new_access_type) ⇒ Object
Sets the current access type parameter for #authorization_uri.
351 352 353 |
# File 'lib/signet/oauth_2/client.rb', line 351 def access_type= new_access_type @access_type = new_access_type end |
#additional_parameters ⇒ Hash
Returns the set of additional (non standard) parameters to be used by the client.
676 677 678 |
# File 'lib/signet/oauth_2/client.rb', line 676 def additional_parameters @additional_parameters ||= {} end |
#additional_parameters=(new_additional_parameters) ⇒ Object
Sets additional (non standard) parameters to be used by the client.
685 686 687 688 689 690 691 692 |
# File 'lib/signet/oauth_2/client.rb', line 685 def additional_parameters= new_additional_parameters if new_additional_parameters.respond_to? :to_hash @additional_parameters = new_additional_parameters.to_hash else raise TypeError, "Expected Hash, got #{new_additional_parameters.class}." end end |
#audience ⇒ String
Returns the target audience ID when issuing assertions. Used only by the assertion grant type.
561 562 563 |
# File 'lib/signet/oauth_2/client.rb', line 561 def audience @audience end |
#audience=(new_audience) ⇒ Object
Sets the target audience ID when issuing assertions. Used only by the assertion grant type.
571 572 573 |
# File 'lib/signet/oauth_2/client.rb', line 571 def audience= new_audience @audience = new_audience end |
#authorization_uri(options = {}) ⇒ Addressable::URI
Returns the authorization URI that the user should be redirected to.
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/signet/oauth_2/client.rb', line 271 def = {} # Normalize external input = deep_hash_normalize return nil if @authorization_uri.nil? [:response_type] = :code unless [:response_type] [:access_type] = access_type if ![:access_type] && access_type [:client_id] ||= client_id [:redirect_uri] ||= redirect_uri if [:prompt] && [:approval_prompt] raise ArgumentError, "prompt and approval_prompt are mutually exclusive parameters" end raise ArgumentError, "Missing required client identifier." unless [:client_id] raise ArgumentError, "Missing required redirect URI." unless [:redirect_uri] [:scope] = scope.join " " if ![:scope] && scope [:state] = state unless [:state] .merge!(additional_parameters.merge([:additional_parameters] || {})) .delete :additional_parameters = .transform_keys(&:to_s) uri = Addressable::URI.parse( ::Signet::OAuth2.( @authorization_uri, ) ) if uri.normalized_scheme != "https" raise Signet::UnsafeOperationError, "Authorization endpoint must be protected by TLS." end uri end |
#authorization_uri=(new_authorization_uri) ⇒ Object
Sets the authorization URI for this client.
307 308 309 |
# File 'lib/signet/oauth_2/client.rb', line 307 def @authorization_uri = coerce_uri end |
#clear_credentials! ⇒ Object
Removes all credentials from the client.
883 884 885 886 887 888 889 890 891 892 893 |
# File 'lib/signet/oauth_2/client.rb', line 883 def clear_credentials! @access_token = nil @refresh_token = nil @id_token = nil @username = nil @password = nil @code = nil @issued_at = nil @expires_at = nil @granted_scopes = nil end |
#client_id ⇒ String
Returns the client identifier for this client.
359 360 361 |
# File 'lib/signet/oauth_2/client.rb', line 359 def client_id @client_id end |
#client_id=(new_client_id) ⇒ Object
Sets the client identifier for this client.
368 369 370 |
# File 'lib/signet/oauth_2/client.rb', line 368 def client_id= new_client_id @client_id = new_client_id end |
#client_secret ⇒ String
Returns the client secret for this client.
376 377 378 |
# File 'lib/signet/oauth_2/client.rb', line 376 def client_secret @client_secret end |
#client_secret=(new_client_secret) ⇒ Object
Sets the client secret for this client.
385 386 387 |
# File 'lib/signet/oauth_2/client.rb', line 385 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.
462 463 464 |
# File 'lib/signet/oauth_2/client.rb', line 462 def code @code end |
#code=(new_code) ⇒ Object
Sets the authorization code issued to this client. Used only by the authorization code access grant type.
472 473 474 |
# File 'lib/signet/oauth_2/client.rb', line 472 def code= new_code @code = new_code end |
#coerce_uri(incoming_uri) ⇒ Object
Addressable expects URIs formatted as hashes to come in with symbols as keys. Returns nil implicitly for the nil case.
330 331 332 333 334 335 336 |
# File 'lib/signet/oauth_2/client.rb', line 330 def coerce_uri incoming_uri if incoming_uri.is_a? Hash Addressable::URI.new deep_hash_normalize(incoming_uri) elsif incoming_uri Addressable::URI.parse incoming_uri end end |
#decoded_id_token(public_key = nil, options = {}, &keyfinder) ⇒ String
Returns the decoded ID token associated with this client.
753 754 755 756 757 758 759 760 761 762 763 |
# File 'lib/signet/oauth_2/client.rb', line 753 def decoded_id_token public_key = nil, = {}, &keyfinder [:algorithm] ||= signing_algorithm verify = !public_key.nil? || block_given? payload, _header = JWT.decode(id_token, public_key, verify, , &keyfinder) raise Signet::UnsafeOperationError, "No ID token audience declared." unless payload.key? "aud" unless Array(payload["aud"]).include?(client_id) raise Signet::UnsafeOperationError, "ID token audience did not match Client ID." end payload end |
#expired? ⇒ TrueClass, FalseClass
Returns true if the access token has expired. Returns false if the token has not expired or has an nil @expires_at.
864 865 866 |
# File 'lib/signet/oauth_2/client.rb', line 864 def expired? !expires_at.nil? && Time.now >= expires_at end |
#expires_at ⇒ Time?
Returns the timestamp the access token will expire at. Returns nil if the token does not expire.
817 818 819 |
# File 'lib/signet/oauth_2/client.rb', line 817 def expires_at @expires_at end |
#expires_at=(new_expires_at) ⇒ Object
Limits the lifetime of the access token as number of seconds since the Epoch. Nil values will be treated as though the token does not expire.
827 828 829 |
# File 'lib/signet/oauth_2/client.rb', line 827 def expires_at= new_expires_at @expires_at = new_expires_at end |
#expires_in ⇒ Integer?
Returns the lifetime of the access token in seconds. Returns nil if the token does not expire.
770 771 772 773 774 775 776 |
# File 'lib/signet/oauth_2/client.rb', line 770 def expires_in if @expires_at.nil? || @issued_at.nil? nil else (@expires_at - @issued_at).to_i end end |
#expires_in=(new_expires_in) ⇒ Object
Sets the lifetime of the access token in seconds. Resets the issued_at timestamp. Nil values will be treated as though the token does not expire.
785 786 787 788 789 790 791 792 793 |
# File 'lib/signet/oauth_2/client.rb', line 785 def expires_in= new_expires_in if new_expires_in.nil? @expires_at = nil @issued_at = nil else @issued_at = Time.now @expires_at = @issued_at + new_expires_in.to_i end end |
#expires_within?(sec) ⇒ TrueClass, FalseClass
Returns true if the access token has expired or expires within the next n seconds. Returns false for tokens with a nil @expires_at.
877 878 879 |
# File 'lib/signet/oauth_2/client.rb', line 877 def expires_within? sec !expires_at.nil? && Time.now >= (expires_at - sec) end |
#expiry ⇒ Integer
Returns the number of seconds assertions are valid for Used only by the assertion grant type.
608 609 610 |
# File 'lib/signet/oauth_2/client.rb', line 608 def expiry @expiry end |
#expiry=(new_expiry) ⇒ Object
Sets the number of seconds assertions are valid for Used only by the assertion grant type.
618 619 620 |
# File 'lib/signet/oauth_2/client.rb', line 618 def expiry= new_expiry @expiry = new_expiry&.to_i end |
#extension_parameters ⇒ Hash
Returns the set of extension parameters used by the client. Used only by extension access grant types.
653 654 655 |
# File 'lib/signet/oauth_2/client.rb', line 653 def extension_parameters @extension_parameters ||= {} end |
#extension_parameters=(new_extension_parameters) ⇒ Object
Sets extension parameters used by the client. Used only by extension access grant types.
663 664 665 666 667 668 669 670 |
# File 'lib/signet/oauth_2/client.rb', line 663 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
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 |
# File 'lib/signet/oauth_2/client.rb', line 1022 def fetch_access_token = {} raise ArgumentError, "Missing token endpoint URI." if token_credential_uri.nil? = deep_hash_normalize client = [:connection] ||= Faraday.default_connection url = Addressable::URI.parse token_credential_uri parameters = generate_access_token_request if client.is_a? Faraday::Connection if [:use_basic_auth] # The Basic Auth middleware usage differs before and after Faraday v2 if Gem::Version.new(Faraday::VERSION).segments.first >= 2 client.request :authorization, :basic, client_id, client_secret else client.request :basic_auth, client_id, client_secret end end response = client.post url.normalize.to_s, Addressable::URI.form_encode(parameters), "Content-Type" => "application/x-www-form-urlencoded" status = response.status.to_i body = response.body content_type = response.headers["Content-type"] else # Hurley if [:use_basic_auth] url.user = client_id url.password = client_secret end response = client.post url.normalize.to_s, parameters status = response.status_code.to_i body = response.body content_type = response.header[:content_type] end = " Server message:\n#{response.body.to_s.strip}" unless body.to_s.strip.empty? if [400, 401, 403].include? status = "Authorization failed.#{}" raise ::Signet::AuthorizationError.new , response: response elsif status.to_s[0] == "5" = "Remote server error.#{}" raise ::Signet::RemoteServerError, elsif status != 200 = "Unexpected status code: #{response.status}.#{}" raise ::Signet::UnexpectedStatusError, end # status == 200 parsed_response = ::Signet::OAuth2.parse_credentials body, content_type parsed_response["granted_scopes"] = parsed_response.delete("scope") if parsed_response parsed_response end |
#fetch_access_token!(options = {}) ⇒ Object
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 |
# File 'lib/signet/oauth_2/client.rb', line 1075 def fetch_access_token! = {} token_hash = 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 update_token! token_hash end token_hash end |
#fetch_protected_resource(options = {}) ⇒ Array
Transmits a request for a protected resource.
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 |
# File 'lib/signet/oauth_2/client.rb', line 1190 def fetch_protected_resource = {} = deep_hash_normalize [:connection] ||= Faraday.default_connection request = generate_authenticated_request request_env = request.to_env [:connection] request_env[:request] ||= request response = [:connection].app.call request_env return response unless response.status.to_i == 401 # When accessing a protected resource, we only want to raise an # error for 401 responses. = "Authorization failed." += " Server message:\n#{response.body.to_s.strip}" unless response.body.to_s.strip.empty? raise ::Signet::AuthorizationError.new( , request: request, response: response ) end |
#generate_authenticated_request(options = {}) ⇒ Faraday::Request
Generates an authenticated request for protected resources.
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 |
# File 'lib/signet/oauth_2/client.rb', line 1114 def generate_authenticated_request = {} = deep_hash_normalize raise ArgumentError, "Missing access token." if access_token.nil? = { realm: nil }.merge() if [:request].is_a? Faraday::Request request = [:request] else if [:request].is_a? Array method, uri, headers, body = [:request] else method = [:method] || :get uri = [:uri] headers = [:headers] || [] body = [:body] || "" end headers = headers.to_a if headers.is_a? 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)| raise ArgumentError, "Missing :#{key} parameter." unless value end method = method.to_s.downcase.to_sym request = [:connection].build_request method.to_s.downcase.to_sym do |req| req.url Addressable::URI.parse(uri).normalize.to_s req.headers = Faraday::Utils::Headers.new headers req.body = body end end request["Authorization"] = ::Signet::OAuth2.( access_token, [:realm] ? [["realm", [:realm]]] : nil ) request["Cache-Control"] = "no-store" request 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.
902 903 904 905 906 907 908 909 910 911 912 913 914 |
# File 'lib/signet/oauth_2/client.rb', line 902 def grant_type @grant_type ||= nil return @grant_type if @grant_type if code && redirect_uri "authorization_code" elsif refresh_token "refresh_token" elsif username && password "password" elsif issuer && signing_key "urn:ietf:params:oauth:grant-type:jwt-bearer" end end |
#grant_type=(new_grant_type) ⇒ Object
916 917 918 919 920 921 922 923 924 |
# File 'lib/signet/oauth_2/client.rb', line 916 def grant_type= new_grant_type @grant_type = case new_grant_type when "authorization_code", "refresh_token", "password", "client_credentials" new_grant_type else Addressable::URI.parse new_grant_type end end |
#granted_scopes ⇒ Array?
Returns the scopes granted by the authorization server.
835 836 837 |
# File 'lib/signet/oauth_2/client.rb', line 835 def granted_scopes @granted_scopes end |
#granted_scopes=(new_granted_scopes) ⇒ Object
Sets the scopes returned by authorization server for this client.
845 846 847 848 849 850 851 852 853 854 855 856 |
# File 'lib/signet/oauth_2/client.rb', line 845 def granted_scopes= new_granted_scopes case new_granted_scopes when Array @granted_scopes = new_granted_scopes when String @granted_scopes = new_granted_scopes.split when nil @granted_scopes = nil else raise TypeError, "Expected Array or String, got #{new_granted_scopes.class}" end end |
#id_token ⇒ String
Returns the ID token associated with this client.
732 733 734 |
# File 'lib/signet/oauth_2/client.rb', line 732 def id_token @id_token ||= nil end |
#id_token=(new_id_token) ⇒ Object
Sets the ID token associated with this client.
741 742 743 |
# File 'lib/signet/oauth_2/client.rb', line 741 def id_token= new_id_token @id_token = new_id_token end |
#issued_at ⇒ Time?
Returns the timestamp the access token was issued at.
799 800 801 |
# File 'lib/signet/oauth_2/client.rb', line 799 def issued_at @issued_at end |
#issued_at=(new_issued_at) ⇒ Object
Sets the timestamp the access token was issued at.
808 809 810 |
# File 'lib/signet/oauth_2/client.rb', line 808 def issued_at= new_issued_at @issued_at = new_issued_at end |
#issuer ⇒ String
Returns the issuer ID associated with this client. Used only by the assertion grant type.
542 543 544 |
# File 'lib/signet/oauth_2/client.rb', line 542 def issuer @issuer end |
#issuer=(new_issuer) ⇒ Object
Sets the issuer ID associated with this client. Used only by the assertion grant type.
552 553 554 |
# File 'lib/signet/oauth_2/client.rb', line 552 def issuer= new_issuer @issuer = new_issuer end |
#password ⇒ String
Returns the password associated with this client. Used only by the resource owner password credential access grant type.
523 524 525 |
# File 'lib/signet/oauth_2/client.rb', line 523 def password @password end |
#password=(new_password) ⇒ Object
Sets the password associated with this client. Used only by the resource owner password credential access grant type.
533 534 535 |
# File 'lib/signet/oauth_2/client.rb', line 533 def password= new_password @password = new_password end |
#principal ⇒ String Also known as: person
Returns the target resource owner for impersonation. Used only by the assertion grant type.
580 581 582 |
# File 'lib/signet/oauth_2/client.rb', line 580 def principal @principal end |
#principal=(new_person) ⇒ Object Also known as: person=
Sets the target resource owner for impersonation. Used only by the assertion grant type.
590 591 592 |
# File 'lib/signet/oauth_2/client.rb', line 590 def principal= new_person @principal = new_person end |
#redirect_uri ⇒ String
Returns the redirect URI for this client.
480 481 482 |
# File 'lib/signet/oauth_2/client.rb', line 480 def redirect_uri @redirect_uri end |
#redirect_uri=(new_redirect_uri) ⇒ Object
Sets the redirect URI for this client.
489 490 491 492 493 494 495 496 497 |
# File 'lib/signet/oauth_2/client.rb', line 489 def redirect_uri= new_redirect_uri new_redirect_uri = Addressable::URI.parse new_redirect_uri # TODO: - Better solution to allow google postmessage flow. For now, make an exception to the spec. unless new_redirect_uri.nil? || new_redirect_uri.absolute? || (new_redirect_uri) || uri_is_oob?(new_redirect_uri) raise ArgumentError, "Redirect URI must be an absolute URI." end @redirect_uri = new_redirect_uri end |
#refresh!(options = {}) ⇒ Object
Refresh the access token, if possible
1090 1091 1092 |
# File 'lib/signet/oauth_2/client.rb', line 1090 def refresh! = {} fetch_access_token! end |
#refresh_token ⇒ String
Returns the refresh token associated with this client.
698 699 700 |
# File 'lib/signet/oauth_2/client.rb', line 698 def refresh_token @refresh_token ||= nil end |
#refresh_token=(new_refresh_token) ⇒ Object
Sets the refresh token associated with this client.
707 708 709 |
# File 'lib/signet/oauth_2/client.rb', line 707 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.
394 395 396 |
# File 'lib/signet/oauth_2/client.rb', line 394 def scope @scope end |
#scope=(new_scope) ⇒ Object
Sets the scope for this client.
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'lib/signet/oauth_2/client.rb', line 405 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 |
#signing_algorithm ⇒ String
Algorithm used for signing JWTs
644 645 646 |
# File 'lib/signet/oauth_2/client.rb', line 644 def signing_algorithm signing_key.is_a?(String) ? "HS256" : "RS256" end |
#signing_key ⇒ String, OpenSSL::PKey
Returns the signing key associated with this client. Used only by the assertion grant type.
627 628 629 |
# File 'lib/signet/oauth_2/client.rb', line 627 def signing_key @signing_key end |
#signing_key=(new_key) ⇒ Object
Sets the signing key when issuing assertions. Used only by the assertion grant type.
637 638 639 |
# File 'lib/signet/oauth_2/client.rb', line 637 def signing_key= new_key @signing_key = new_key end |
#state ⇒ String
Returns the client's current state value.
444 445 446 |
# File 'lib/signet/oauth_2/client.rb', line 444 def state @state end |
#state=(new_state) ⇒ Object
Sets the client's current state value.
453 454 455 |
# File 'lib/signet/oauth_2/client.rb', line 453 def state= new_state @state = new_state end |
#target_audience ⇒ String
Returns the final target audience for ID tokens fetched by this client.
428 429 430 |
# File 'lib/signet/oauth_2/client.rb', line 428 def target_audience @target_audience end |
#target_audience=(new_target_audience) ⇒ Object
Sets the final target audience for ID tokens fetched by this client.
436 437 438 |
# File 'lib/signet/oauth_2/client.rb', line 436 def target_audience= new_target_audience @target_audience = new_target_audience end |
#to_json(*_args) ⇒ String
A serialized client contains sensitive information. Persist or transmit with care.
Serialize the client object to JSON.
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 |
# File 'lib/signet/oauth_2/client.rb', line 950 def to_json *_args MultiJson.dump( "authorization_uri" => &.to_s, "token_credential_uri" => token_credential_uri&.to_s, "client_id" => client_id, "client_secret" => client_secret, "scope" => scope, "target_audience" => target_audience, "state" => state, "code" => code, "redirect_uri" => redirect_uri&.to_s, "username" => username, "password" => password, "issuer" => issuer, "audience" => audience, "person" => person, "expiry" => expiry, "expires_at" => expires_at&.to_i, "signing_key" => signing_key, "refresh_token" => refresh_token, "access_token" => access_token, "id_token" => id_token, "extension_parameters" => extension_parameters, "granted_scopes" => granted_scopes ) end |
#to_jwt(options = {}) ⇒ Object
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 |
# File 'lib/signet/oauth_2/client.rb', line 926 def to_jwt = {} = deep_hash_normalize now = Time.new skew = [:skew] || 60 assertion = { "iss" => issuer, "aud" => audience, "exp" => (now + expiry).to_i, "iat" => (now - skew).to_i } assertion["scope"] = scope.join " " unless scope.nil? assertion["target_audience"] = target_audience unless target_audience.nil? assertion["prn"] = person unless person.nil? assertion["sub"] = sub unless sub.nil? JWT.encode assertion, signing_key, signing_algorithm end |
#token_credential_uri ⇒ Addressable::URI
Returns the token credential URI for this client.
315 316 317 |
# File 'lib/signet/oauth_2/client.rb', line 315 def token_credential_uri @token_credential_uri end |
#token_credential_uri=(new_token_credential_uri) ⇒ Object
Sets the token credential URI for this client.
324 325 326 |
# File 'lib/signet/oauth_2/client.rb', line 324 def token_credential_uri= new_token_credential_uri @token_credential_uri = coerce_uri new_token_credential_uri end |
#update!(options = {}) ⇒ Object
Updates an OAuth 2.0 client.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/signet/oauth_2/client.rb', line 185 def update! = {} # Normalize all keys to symbols to allow indifferent access. = deep_hash_normalize self. = [:authorization_uri] if .key? :authorization_uri self.token_credential_uri = [:token_credential_uri] if .key? :token_credential_uri self.client_id = [:client_id] if .key? :client_id self.client_secret = [:client_secret] if .key? :client_secret self.scope = [:scope] if .key? :scope self.target_audience = [:target_audience] if .key? :target_audience self.state = [:state] if .key? :state self.code = [:code] if .key? :code self.redirect_uri = [:redirect_uri] if .key? :redirect_uri self.username = [:username] if .key? :username self.password = [:password] if .key? :password self.issuer = [:issuer] if .key? :issuer self.person = [:person] if .key? :person self.sub = [:sub] if .key? :sub self.expiry = [:expiry] || 60 self.audience = [:audience] if .key? :audience self.signing_key = [:signing_key] if .key? :signing_key self.extension_parameters = [:extension_parameters] || {} self.additional_parameters = [:additional_parameters] || {} self.access_type = .fetch :access_type, :offline update_token! self end |
#update_token!(options = {}) ⇒ Object
Updates an OAuth 2.0 client.
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/signet/oauth_2/client.rb', line 241 def update_token! = {} # Normalize all keys to symbols to allow indifferent access internally = deep_hash_normalize self.expires_in = [:expires] if .key? :expires self.expires_in = [:expires_in] if .key? :expires_in self.expires_at = [:expires_at] if .key? :expires_at # 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. self.issued_at = [:issued_at] if .key? :issued_at # Special case where we want expires_at to be relative to issued_at if .key?(:issued_at) && .key?(:expires_in) set_relative_expires_at [:issued_at], [:expires_in] end self.access_token = [:access_token] if .key? :access_token self.refresh_token = [:refresh_token] if .key? :refresh_token self.id_token = [:id_token] if .key? :id_token self.granted_scopes = [:granted_scopes] if .key? :granted_scopes self end |
#username ⇒ String
Returns the username associated with this client. Used only by the resource owner password credential access grant type.
504 505 506 |
# File 'lib/signet/oauth_2/client.rb', line 504 def username @username end |
#username=(new_username) ⇒ Object
Sets the username associated with this client. Used only by the resource owner password credential access grant type.
514 515 516 |
# File 'lib/signet/oauth_2/client.rb', line 514 def username= new_username @username = new_username end |