├── Gemfile ├── Gemfile.lock ├── LICENSE ├── README.md ├── bin ├── bundle ├── htmldiff ├── httparty ├── ldiff └── rspec ├── intuit-oauth.gemspec ├── lib ├── intuit-oauth.rb └── intuit-oauth │ ├── base.rb │ ├── client.rb │ ├── constants.rb │ ├── exception.rb │ ├── flow │ ├── code.rb │ ├── openid.rb │ └── token.rb │ ├── migration.rb │ ├── response.rb │ ├── transport.rb │ └── utils.rb └── views ├── PRSWidget.txt └── SDK.png /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gem 'rspec' 4 | gem 'json' 5 | gem 'httparty' -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | diff-lcs (1.3) 5 | httparty (0.17.0) 6 | mime-types (~> 3.0) 7 | multi_xml (>= 0.5.2) 8 | json (2.1.0) 9 | mime-types (3.2.2) 10 | mime-types-data (~> 3.2015) 11 | mime-types-data (3.2019.0331) 12 | multi_xml (0.6.0) 13 | rspec (3.8.0) 14 | rspec-core (~> 3.8.0) 15 | rspec-expectations (~> 3.8.0) 16 | rspec-mocks (~> 3.8.0) 17 | rspec-core (3.8.0) 18 | rspec-support (~> 3.8.0) 19 | rspec-expectations (3.8.2) 20 | diff-lcs (>= 1.2.0, < 2.0) 21 | rspec-support (~> 3.8.0) 22 | rspec-mocks (3.8.0) 23 | diff-lcs (>= 1.2.0, < 2.0) 24 | rspec-support (~> 3.8.0) 25 | rspec-support (3.8.0) 26 | 27 | PLATFORMS 28 | ruby 29 | 30 | DEPENDENCIES 31 | httparty 32 | json 33 | rspec 34 | 35 | BUNDLED WITH 36 | 1.16.2 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2018 Intuit, Inc. 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![SDK Banner](views/SDK.png)](https://help.developer.intuit.com/s/SDKFeedback?cid=1130) 2 | 3 | # oauth-rubyclient 4 | 5 | [![Gem Version](https://badge.fury.io/rb/intuit-oauth.svg)](https://badge.fury.io/rb/intuit-oauth) 6 | 7 | Intuit OAuth Ruby Client 8 | ========================== 9 | 10 | This tutorial describes how to use Intuit Ruby OAuth Client Library to generate access tokens for your QuickBooks Online Companies data. The Ruby OAuth Client Library provides a set of methods that make it easier to work with Intuit’s OAuth and OpenID protocol: 11 | 12 | - Generating Authorization URL 13 | - Getting OAuth2 Bearer Token 14 | - Getting User Info 15 | - Validating OpenID token 16 | - Refreshing OAuth2 Token 17 | - Revoking OAuth2 Token 18 | - Migrating tokens from OAuth1.0 to OAuth2 19 | 20 | If you are not familiar with Intuit OAuth protocol, please refer to [Authentication and authorization page](https://developer.intuit.com/app/developer/qbo/docs/develop/authentication-and-authorization/oauth-2.0) for general information on OAuth. 21 | 22 | The Ruby OAuth client would require Ruby version >= 1.9.0, and RubyGem version >= 1.3.5 23 | 24 | 25 | ### Installation 26 | 27 | The Ruby OAuth library use gem for installation. To install the library, run: 28 | 29 | $ gem install 'intuit-oauth' 30 | 31 | You can also download the source code and run: 32 | 33 | $ gem build intuit-oauth.gemspec 34 | 35 | to build your own gem if you want to modify a certain functions in the library 36 | 37 | ### Create client instance 38 | 39 | In order to start using the library, the first step is to create a client object. Instantiate the IntuitOAuth object with app’s ClientID, ClientSecret, Redirect URL and the right environment. Valid values for environment include sandbox and production. redirect_uri should be set in your Intuit Developer app’s Keys tab under the right environment. 40 | 41 | ```ruby 42 | require 'intuit-oauth' 43 | 44 | oauth_client = IntuitOAuth::Client.new('client_id', 'client_secret', 'redirectUrl', 'environment') 45 | ``` 46 | 47 | ### General Authorization Code URL 48 | 49 | After the client is created, use the client object to generate authorization URL by specifying scopes. It is shown below in code: 50 | 51 | ```ruby 52 | scopes = [ 53 | IntuitOAuth::Scopes::ACCOUNTING 54 | ] 55 | 56 | authorizationCodeUrl = oauth_client.code.get_auth_uri(scopes) 57 | # => https://appcenter.intuit.com/connect/oauth2? client_id=clientId&redirect_uri=redirectUrl&response_type=code&scope=com.intuit.quickbooks.accounting&state=rMwcoDITc2N6FJsUGGO9 58 | ``` 59 | Redirect your users to the authorizationCodeUrl, and an authorization code will be sent to the Redirect URL defined. The authorization code will be used to exchange for an oAuth 2.0 access token later. 60 | 61 | ### Exchange Authorization Code for OAuth 2.0 Token 62 | 63 | Once the user has authorized your app, an authorization code will be sent to your RedirectURL defined in your client. Exchange the authorization code for an OAuth 2.0 token object. 64 | 65 | result = oauth_client.token.get_bearer_token('The_authorization_code') 66 | 67 | ### Refresh Token 68 | 69 | Your app must keep track of when a stored access token can be used and when the token must be refreshed. Use the refresh method to refresh the token when the token expired. ALWAYS STORE THE LATEST REFRESH TOKEN RETURNED. Below is an exanmple how to use the refresh method to refresh token: 70 | 71 | newToken = oauth_client.token.refresh_tokens('Your_refresh_token') 72 | 73 | ### Revoke Token 74 | 75 | If your app is disconnected by the user, you would need to revoke the token. Use revoke_tokens method to revoke the token: 76 | 77 | trueOrFalse = oauth_client.token.revoke_tokens('the_token_you_want_to_revoke') 78 | 79 | 80 | ### Get User Info 81 | 82 | If OpenID scope is set when you generate the authorization URL, you can use get_user_info to get the user information: 83 | 84 | result=oauth_client.openid.get_user_info('accessToken') 85 | 86 | 87 | 88 | ### Call migration method 89 | 90 | If you have migrated your OAuth 1.0 app to OAuth 2.0 app, and want to exchange your OAuth 1.0 token to OAuth 2.0 token, use migrate_tokens method 91 | 92 | result=oauth_client.migration.migrate_tokens(consumer_key, consumer_secret, access_token, access_secret, scopes) 93 | 94 | ### A Complete Usage Example for Creating OAuth 2.0 token 95 | 96 | The below example tells how to construct the IntuitOAuth Client and use it to generate an OAuth 2 token. 97 | 98 | ```ruby 99 | require 'intuit-oauth' 100 | 101 | oauth_client = IntuitOAuth::Client.new('client_id', 'client_secret', 'redirectUrl', 'environment') 102 | scopes = [ 103 | IntuitOAuth::Scopes::ACCOUNTING 104 | ] 105 | 106 | authorizationCodeUrl = oauth_client.code.get_auth_uri(scopes) 107 | # => https://appcenter.intuit.com/connect/oauth2?client_id=clientId&redirect_uri=redirectUrl&response_type=code&scope=com.intuit.quickbooks.accounting&state=rMwcoDITc2N6FJsUGGO9 108 | 109 | oauth2Token = oauth_client.token.get_bearer_token('the authorization code returned from authorizationCodeUrl') 110 | # => # 111 | 112 | ``` 113 | 114 | Issues and Contributions 115 | ------------------------ 116 | 117 | Please open an [issue](https://github.com/intuit/oauth-rubyclient/issues) on GitHub if you have a problem, suggestion, or other comment. 118 | 119 | Pull requests are welcome and encouraged! Any contributions should include new or updated unit tests as necessary to maintain thorough test coverage. 120 | 121 | License 122 | ------- 123 | 124 | Intuit-oauth Ruby gem is provided under Apache 2.0 License 125 | -------------------------------------------------------------------------------- /bin/bundle: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # frozen_string_literal: true 3 | 4 | # 5 | # This file was generated by Bundler. 6 | # 7 | # The application 'bundle' is installed as part of a gem, and 8 | # this file is here to facilitate running it. 9 | # 10 | 11 | require "rubygems" 12 | 13 | m = Module.new do 14 | module_function 15 | 16 | def invoked_as_script? 17 | File.expand_path($0) == File.expand_path(__FILE__) 18 | end 19 | 20 | def env_var_version 21 | ENV["BUNDLER_VERSION"] 22 | end 23 | 24 | def cli_arg_version 25 | return unless invoked_as_script? # don't want to hijack other binstubs 26 | return unless "update".start_with?(ARGV.first || " ") # must be running `bundle update` 27 | bundler_version = nil 28 | update_index = nil 29 | ARGV.each_with_index do |a, i| 30 | if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN 31 | bundler_version = a 32 | end 33 | next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/ 34 | bundler_version = $1 || ">= 0.a" 35 | update_index = i 36 | end 37 | bundler_version 38 | end 39 | 40 | def gemfile 41 | gemfile = ENV["BUNDLE_GEMFILE"] 42 | return gemfile if gemfile && !gemfile.empty? 43 | 44 | File.expand_path("../../Gemfile", __FILE__) 45 | end 46 | 47 | def lockfile 48 | lockfile = 49 | case File.basename(gemfile) 50 | when "gems.rb" then gemfile.sub(/\.rb$/, gemfile) 51 | else "#{gemfile}.lock" 52 | end 53 | File.expand_path(lockfile) 54 | end 55 | 56 | def lockfile_version 57 | return unless File.file?(lockfile) 58 | lockfile_contents = File.read(lockfile) 59 | return unless lockfile_contents =~ /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/ 60 | Regexp.last_match(1) 61 | end 62 | 63 | def bundler_version 64 | @bundler_version ||= begin 65 | env_var_version || cli_arg_version || 66 | lockfile_version || "#{Gem::Requirement.default}.a" 67 | end 68 | end 69 | 70 | def load_bundler! 71 | ENV["BUNDLE_GEMFILE"] ||= gemfile 72 | 73 | # must dup string for RG < 1.8 compatibility 74 | activate_bundler(bundler_version.dup) 75 | end 76 | 77 | def activate_bundler(bundler_version) 78 | if Gem::Version.correct?(bundler_version) && Gem::Version.new(bundler_version).release < Gem::Version.new("2.0") 79 | bundler_version = "< 2" 80 | end 81 | gem_error = activation_error_handling do 82 | gem "bundler", bundler_version 83 | end 84 | return if gem_error.nil? 85 | require_error = activation_error_handling do 86 | require "bundler/version" 87 | end 88 | return if require_error.nil? && Gem::Requirement.new(bundler_version).satisfied_by?(Gem::Version.new(Bundler::VERSION)) 89 | warn "Activating bundler (#{bundler_version}) failed:\n#{gem_error.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_version}'`" 90 | exit 42 91 | end 92 | 93 | def activation_error_handling 94 | yield 95 | nil 96 | rescue StandardError, LoadError => e 97 | e 98 | end 99 | end 100 | 101 | m.load_bundler! 102 | 103 | if m.invoked_as_script? 104 | load Gem.bin_path("bundler", "bundle") 105 | end 106 | -------------------------------------------------------------------------------- /bin/htmldiff: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # frozen_string_literal: true 3 | 4 | # 5 | # This file was generated by Bundler. 6 | # 7 | # The application 'htmldiff' is installed as part of a gem, and 8 | # this file is here to facilitate running it. 9 | # 10 | 11 | require "pathname" 12 | ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", 13 | Pathname.new(__FILE__).realpath) 14 | 15 | bundle_binstub = File.expand_path("../bundle", __FILE__) 16 | 17 | if File.file?(bundle_binstub) 18 | if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ 19 | load(bundle_binstub) 20 | else 21 | abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. 22 | Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") 23 | end 24 | end 25 | 26 | require "rubygems" 27 | require "bundler/setup" 28 | 29 | load Gem.bin_path("diff-lcs", "htmldiff") 30 | -------------------------------------------------------------------------------- /bin/httparty: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # frozen_string_literal: true 3 | 4 | # 5 | # This file was generated by Bundler. 6 | # 7 | # The application 'httparty' is installed as part of a gem, and 8 | # this file is here to facilitate running it. 9 | # 10 | 11 | require "pathname" 12 | ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", 13 | Pathname.new(__FILE__).realpath) 14 | 15 | bundle_binstub = File.expand_path("../bundle", __FILE__) 16 | 17 | if File.file?(bundle_binstub) 18 | if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ 19 | load(bundle_binstub) 20 | else 21 | abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. 22 | Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") 23 | end 24 | end 25 | 26 | require "rubygems" 27 | require "bundler/setup" 28 | 29 | load Gem.bin_path("httparty", "httparty") 30 | -------------------------------------------------------------------------------- /bin/ldiff: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # frozen_string_literal: true 3 | 4 | # 5 | # This file was generated by Bundler. 6 | # 7 | # The application 'ldiff' is installed as part of a gem, and 8 | # this file is here to facilitate running it. 9 | # 10 | 11 | require "pathname" 12 | ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", 13 | Pathname.new(__FILE__).realpath) 14 | 15 | bundle_binstub = File.expand_path("../bundle", __FILE__) 16 | 17 | if File.file?(bundle_binstub) 18 | if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ 19 | load(bundle_binstub) 20 | else 21 | abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. 22 | Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") 23 | end 24 | end 25 | 26 | require "rubygems" 27 | require "bundler/setup" 28 | 29 | load Gem.bin_path("diff-lcs", "ldiff") 30 | -------------------------------------------------------------------------------- /bin/rspec: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # frozen_string_literal: true 3 | 4 | # 5 | # This file was generated by Bundler. 6 | # 7 | # The application 'rspec' is installed as part of a gem, and 8 | # this file is here to facilitate running it. 9 | # 10 | 11 | require "pathname" 12 | ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", 13 | Pathname.new(__FILE__).realpath) 14 | 15 | bundle_binstub = File.expand_path("../bundle", __FILE__) 16 | 17 | if File.file?(bundle_binstub) 18 | if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ 19 | load(bundle_binstub) 20 | else 21 | abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. 22 | Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") 23 | end 24 | end 25 | 26 | require "rubygems" 27 | require "bundler/setup" 28 | 29 | load Gem.bin_path("rspec-core", "rspec") 30 | -------------------------------------------------------------------------------- /intuit-oauth.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | lib = File.expand_path('../lib', __FILE__) 4 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 5 | 6 | Gem::Specification.new do |spec| 7 | spec.add_dependency 'httparty', '>= 0.16.3' 8 | spec.add_dependency 'json', '>= 1.8.0' 9 | spec.add_dependency 'rsa-pem-from-mod-exp', '~> 0.1.0' 10 | 11 | spec.authors = ['Intuit Inc'] 12 | spec.description = 'A Ruby wrapper Intuit\'s OAuth and OpenID implementation.' 13 | spec.email = ['idgsdk@intuit.com'] 14 | spec.homepage = 'https://github.com/intuit/oauth-rubyclient' 15 | spec.licenses = ['Apache-2.0'] 16 | spec.name = 'intuit-oauth' 17 | spec.required_ruby_version = '>= 1.9.0' 18 | spec.required_rubygems_version = '>= 1.3.5' 19 | spec.summary = 'A Ruby wrapper for the OAuth 2.0 protocol.' 20 | spec.version = '1.0.3' 21 | 22 | spec.require_paths = %w[lib] 23 | spec.bindir = 'exe' 24 | spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } 25 | spec.files = `git ls-files -z`.split("\x0").reject do |f| 26 | f.match(%r{^(bin|test|spec|features)/}) 27 | end 28 | 29 | spec.add_development_dependency 'addressable', '~> 2.3' 30 | spec.add_development_dependency 'backports', '~> 3.11' 31 | spec.add_development_dependency 'bundler', '~> 1.16' 32 | spec.add_development_dependency 'coveralls', '~> 0.8' 33 | spec.add_development_dependency 'rake', '~> 11.0' 34 | spec.add_development_dependency 'rdoc', ['>= 5.0', '< 7'] 35 | spec.add_development_dependency 'rspec', '~> 3.0' 36 | spec.add_development_dependency 'wwtd', '~> 0' 37 | end 38 | -------------------------------------------------------------------------------- /lib/intuit-oauth.rb: -------------------------------------------------------------------------------- 1 | require 'intuit-oauth/client' 2 | require 'intuit-oauth/constants' 3 | require 'intuit-oauth/exception' 4 | require 'intuit-oauth/migration' 5 | require 'intuit-oauth/transport' 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /lib/intuit-oauth/base.rb: -------------------------------------------------------------------------------- 1 | module IntuitOAuth 2 | class Base 3 | attr_accessor :client 4 | 5 | def initialize(client) 6 | @client = client 7 | end 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /lib/intuit-oauth/client.rb: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2018 Intuit 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | require_relative './constants' 16 | require_relative './transport' 17 | require_relative './migration' 18 | require_relative './flow/code' 19 | require_relative './flow/token' 20 | require_relative './flow/openid' 21 | 22 | 23 | module IntuitOAuth 24 | class Client 25 | attr_reader :id, :secret, :redirect_uri, :environment, 26 | :auth_endpoint, :token_endpoint, :revoke_endpoint, :issuer_uri, :jwks_uri, :user_info_url, :state_token, :realm_id 27 | attr_writer :realm_id, :state_token 28 | 29 | def initialize(client_id, client_secret, redirect_uri, environment) 30 | @id = client_id 31 | @secret = client_secret 32 | @redirect_uri = redirect_uri 33 | @environment = environment 34 | 35 | # Discovery Doc containes endpoints required for OAuth fow 36 | @discovery_doc = get_discovery_doc(@environment) 37 | @auth_endpoint = @discovery_doc['authorization_endpoint'] 38 | @token_endpoint = @discovery_doc['token_endpoint'] 39 | @revoke_endpoint = @discovery_doc['revocation_endpoint'] 40 | @issuer_uri = @discovery_doc['issuer'] 41 | @jwks_uri = @discovery_doc['jwks_uri'] 42 | @user_info_url = @discovery_doc['userinfo_endpoint'] 43 | 44 | # optionally set realm_id 45 | @realm_id = '' 46 | @state_token = '' 47 | end 48 | 49 | def get_discovery_doc(environment) 50 | if ['production', 'prod'].include? environment.downcase 51 | url = IntuitOAuth::Config::DISCOVERY_URL_PROD 52 | else 53 | url = IntuitOAuth::Config::DISCOVERY_URL_SANDBOX 54 | end 55 | IntuitOAuth::Transport.request('GET', url) 56 | end 57 | 58 | def code 59 | IntuitOAuth::Flow::AuthCode.new(self) 60 | end 61 | 62 | def token 63 | IntuitOAuth::Flow::Token.new(self) 64 | end 65 | 66 | def openid 67 | IntuitOAuth::Flow::OpenID.new(self) 68 | end 69 | 70 | def migration 71 | IntuitOAuth::Migration::Migrate.new(self) 72 | end 73 | end 74 | end 75 | -------------------------------------------------------------------------------- /lib/intuit-oauth/constants.rb: -------------------------------------------------------------------------------- 1 | module IntuitOAuth 2 | class Config 3 | DISCOVERY_URL_SANDBOX = 'https://developer.intuit.com/.well-known/openid_sandbox_configuration/' 4 | DISCOVERY_URL_PROD = 'https://developer.intuit.com/.well-known/openid_configuration/' 5 | MIGRATION_URL_SANDBOX = 'https://developer-sandbox.api.intuit.com/v2/oauth2/tokens/migrate' 6 | MIGRATION_URL_PROD = 'https://developer.api.intuit.com/v2/oauth2/tokens/migrate' 7 | end 8 | 9 | class Scopes 10 | ACCOUNTING = 'com.intuit.quickbooks.accounting' 11 | PAYMENTS = 'com.intuit.quickbooks.payment' 12 | OPENID = 'openid' 13 | PROFILE = 'profile' 14 | EMAIL = 'email' 15 | PHONE = 'phone' 16 | ADDRESS = 'address' 17 | 18 | # whitelisted BETA apps only 19 | PAYROLL = 'com.intuit.quickbooks.payroll' 20 | PAYROLL_TIMETRACKING = 'com.intuit.quickbooks.payroll.timetracking' 21 | PAYROLL_BENEFITS = 'com.intuit.quickbooks.payroll.benefits' 22 | end 23 | 24 | class Version 25 | VERSION = '0.0.1' 26 | USER_AGENT = "Intuit-OAuthClient-Ruby#{VERSION}-#{RUBY_PLATFORM}" 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /lib/intuit-oauth/exception.rb: -------------------------------------------------------------------------------- 1 | module IntuitOAuth 2 | class OAuth2ClientException < StandardError 3 | attr_reader :status_code, :body, :headers, :intuit_tid, :timestamp 4 | 5 | def initialize(response) 6 | @status_code = response.code 7 | @body = response.body 8 | @headers = response.headers 9 | @intuit_tid = response.headers['intuit_tid'] 10 | @timestamp = response.headers['date'] 11 | super("HTTP status #{@status_code}, error message: #{@body}, intuit_tid: #{@intuit_tid} on #{@timestamp}") 12 | end 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /lib/intuit-oauth/flow/code.rb: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2018 Intuit 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | require_relative '../base' 16 | require_relative '../utils' 17 | require 'active_support/all' 18 | 19 | 20 | module IntuitOAuth 21 | module Flow 22 | class AuthCode < Base 23 | 24 | # Generate the Authorization Code URL 25 | # 26 | # @param [Scope] the Scope for the token to be generated 27 | # @param [state_token] an option state token to be passed 28 | # @return [URL] the authorization code URL 29 | def get_auth_uri(scopes, state_token=nil) 30 | if state_token.nil? 31 | state_token = IntuitOAuth::Utils.generate_random_string() 32 | end 33 | @client.state_token = state_token 34 | url_params = { 35 | client_id: @client.id, 36 | scope: IntuitOAuth::Utils.scopes_to_string(scopes), 37 | redirect_uri: @client.redirect_uri, 38 | response_type: 'code', 39 | state: state_token, 40 | } 41 | 42 | "#{@client.auth_endpoint}?#{url_params.to_param}" 43 | end 44 | end 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /lib/intuit-oauth/flow/openid.rb: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2018 Intuit 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | require 'rsa_pem' 16 | 17 | require_relative '../base' 18 | 19 | 20 | module IntuitOAuth 21 | module Flow 22 | class OpenID < Base 23 | 24 | # Get the User Info 25 | # 26 | # @param [access_token] the access token needs to access the user info 27 | # @return [Response] the response object 28 | def get_user_info(access_token) 29 | headers = { 30 | Authorization: "Bearer #{access_token}" 31 | } 32 | 33 | IntuitOAuth::Transport.request('GET', @client.user_info_url, headers=headers) 34 | end 35 | 36 | ## 37 | # If the token can be correctly validated, returns True. Otherwise, return false 38 | # The validation rules are: 39 | # 1.You have to provide the client_id value, which must match the 40 | # token's aud field 41 | # 2.The payload issuer is from Intuit 42 | # 3.The expire time is not expired. 43 | # 4.The signature is correct 44 | # 45 | # If something fails, raises an error 46 | # 47 | # @param [String] id_token 48 | # The string form of the token 49 | # 50 | # @return [Boolean] 51 | 52 | def validate_id_token(id_token) 53 | 54 | id_token_header_raw, id_token_payload_raw, id_token_signature_raw = id_token.split(".") 55 | 56 | # base 64 decode 57 | id_token_header_json = JSON.parse(Base64.decode64(id_token_header_raw.strip)) 58 | id_token_payload_json = JSON.parse(Base64.decode64(id_token_payload_raw.strip)) 59 | id_token_signature = Base64.decode64(id_token_signature_raw.strip) 60 | 61 | # 1. check if payload's issuer is from Intuit 62 | issue = id_token_payload_json.fetch('iss') 63 | unless issue.eql? @client.issuer_uri 64 | return false 65 | end 66 | 67 | # 2. check if the aud matches the client id 68 | aud = id_token_payload_json.fetch('aud').first 69 | unless aud.eql? @client.id 70 | return false 71 | end 72 | 73 | # 3. check if the expire time is not expired 74 | exp = id_token_payload_json.fetch('exp') 75 | if exp < Time.now.to_i 76 | return false 77 | end 78 | 79 | # 4. check if the signature is correct 80 | response = IntuitOAuth::Transport.request('GET', @client.jwks_uri, nil, nil, false) 81 | body = response.body 82 | # loop through all the available keys in jwks_uri & find match with kid from id_token_header_json 83 | keys_array = JSON.parse(body)['keys'] 84 | keys_array.each do |keys| 85 | standard_kid = keys.fetch('kid') 86 | kid_in_id_token = id_token_header_json.fetch('kid') 87 | 88 | return true if standard_kid.eql? kid_in_id_token 89 | end 90 | 91 | return false 92 | 93 | end 94 | end 95 | end 96 | end 97 | -------------------------------------------------------------------------------- /lib/intuit-oauth/flow/token.rb: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2018 Intuit 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | require 'uri' 16 | require 'json' 17 | require_relative '../utils' 18 | require_relative '../transport' 19 | require_relative '../base' 20 | 21 | 22 | module IntuitOAuth 23 | module Flow 24 | class Token < Base 25 | 26 | # Exchange the authorization Code for the Bearer Token 27 | # 28 | # @param [auth_code] the Code send to your redirect_uri 29 | # @param [realm_id] the company ID for the Company 30 | # @return [AccessToken] the AccessToken 31 | def get_bearer_token(auth_code, realm_id=nil) 32 | if realm_id != nil 33 | @client.realm_id = realm_id 34 | end 35 | 36 | headers = { 37 | Accept: 'application/json', 38 | "Content-Type": 'application/x-www-form-urlencoded', 39 | Authorization: IntuitOAuth::Utils.get_auth_header(@client.id, @client.secret) 40 | } 41 | 42 | body = { 43 | grant_type: 'authorization_code', 44 | code: auth_code, 45 | redirect_uri: @client.redirect_uri 46 | } 47 | 48 | IntuitOAuth::Transport.request('POST', @client.token_endpoint, headers, URI.encode_www_form(body)) 49 | end 50 | 51 | # Using the token passed to generate a new refresh token and access token 52 | # 53 | # @param [token] the refresh token used to refresh token 54 | # @return [AccessToken] the AccessToken 55 | def refresh_tokens(token) 56 | headers = { 57 | "Content-Type": 'application/x-www-form-urlencoded', 58 | Authorization: IntuitOAuth::Utils.get_auth_header(@client.id, @client.secret) 59 | } 60 | 61 | body = { 62 | grant_type: 'refresh_token', 63 | refresh_token: token 64 | } 65 | 66 | IntuitOAuth::Transport.request('POST', @client.token_endpoint, headers, URI.encode_www_form(body)) 67 | end 68 | 69 | # Revoke the specific access token or refresh token. Return true if success, false otherwise 70 | # 71 | # @param [token] the refresh token or access token to be invoked 72 | # @return [boolean] True if successfully revoked. False otherwise 73 | def revoke_tokens(token) 74 | headers = { 75 | "Content-Type": 'application/json', 76 | Authorization: IntuitOAuth::Utils.get_auth_header(@client.id, @client.secret) 77 | } 78 | 79 | body = { 80 | token: token 81 | } 82 | 83 | response = IntuitOAuth::Transport.request('POST', @client.revoke_endpoint, headers, body.to_json, false) 84 | if response.code == 200 85 | return true 86 | end 87 | 88 | return false 89 | end 90 | 91 | end 92 | end 93 | end 94 | -------------------------------------------------------------------------------- /lib/intuit-oauth/migration.rb: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2018 Intuit 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | require_relative './utils' 16 | require_relative './transport' 17 | require_relative './constants' 18 | require_relative './base' 19 | 20 | 21 | module IntuitOAuth 22 | module Migration 23 | class Migrate < Base 24 | 25 | # Exchange an OAuth 1 token for an OAuth 2 token pair. It is used for apps that are using OAuth 1.0 and want to migrate 26 | # to OAuth 2.0. 27 | # 28 | # @param [cusomer_key] the OAuth 1.0 Consumer key 29 | # @param [consumer_secret] the OAuth 1.0 Consumer Secret 30 | # @param [access_token] the OAuth 1.0 Access Token 31 | # @param [access_secret] the OAuth 1.0 Access Token Secret 32 | # @param [scopes] the scopes for the token. 33 | # @return [OAuth2Token] the OAuth2 Token object 34 | def migrate_tokens(consumer_key, consumer_secret, access_token, access_secret, scopes) 35 | if %w[production prod].include? @client.environment.downcase 36 | migration_endpoint = IntuitOAuth::Config::MIGRATION_URL_PROD 37 | else 38 | migration_endpoint = IntuitOAuth::Config::MIGRATION_URL_SANDBOX 39 | end 40 | 41 | oauth1_tokens = { 42 | consumer_key: consumer_key, 43 | consumer_secret: consumer_secret, 44 | access_token: access_token, 45 | access_secret: access_secret 46 | } 47 | oauth1_header = IntuitOAuth::Utils.get_oauth1_header('POST', migration_endpoint, oauth1_tokens) 48 | headers = { 49 | 'Content-Type': 'application/json', 50 | Accept: 'application/json', 51 | Authorization: oauth1_header 52 | } 53 | 54 | body = { 55 | scope: IntuitOAuth::Utils.scopes_to_string(scopes), 56 | redirect_uri: @client.redirect_uri, 57 | client_id: @client.id, 58 | client_secret: @client.secret 59 | } 60 | 61 | IntuitOAuth::Transport.request('POST', migration_endpoint, headers, body.to_json) 62 | end 63 | 64 | end 65 | end 66 | end 67 | -------------------------------------------------------------------------------- /lib/intuit-oauth/response.rb: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2018 Intuit 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | module IntuitOAuth 16 | class ClientResponse 17 | attr_reader :access_token, :expires_in, :refresh_token, :x_refresh_token_expires_in, 18 | :id_token, :headers, :body, :code, :realm_id 19 | 20 | def initialize(response) 21 | @access_token = response['access_token'] 22 | @expires_in = response['expires_in'] 23 | @refresh_token = response['refresh_token'] 24 | @x_refresh_token_expires_in = response['x_refresh_token_expires_in'] 25 | if response['id_token'] 26 | @id_token = response['id_token'] 27 | end 28 | if response['realmId'] 29 | @realm_id = response['realmId'] 30 | end 31 | 32 | @headers = response.headers 33 | @body = response 34 | @code = response.code 35 | end 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /lib/intuit-oauth/transport.rb: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2018 Intuit 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | require 'uri' 16 | require 'json' 17 | require 'httparty' 18 | require_relative './utils' 19 | 20 | module IntuitOAuth 21 | class Transport 22 | include HTTParty 23 | ssl_version :TLSv1_2 24 | 25 | def self.request(method, url, headers=nil, body=nil, isBuildResponse=true) 26 | uri = URI(url) 27 | 28 | user_agent_header = { 29 | 'User-Agent': IntuitOAuth::Version::USER_AGENT 30 | } 31 | req_headers = headers.nil? ? user_agent_header : user_agent_header.merge!(headers) 32 | 33 | if method == 'GET' 34 | response = get(url, 35 | headers: req_headers 36 | ) 37 | 38 | elsif method == 'POST' 39 | response = post(url, 40 | headers: req_headers, 41 | body: body 42 | ) 43 | end 44 | 45 | if isBuildResponse == true 46 | IntuitOAuth::Utils.build_response_object(response) 47 | else 48 | response 49 | end 50 | end 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /lib/intuit-oauth/utils.rb: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2018 Intuit 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | require 'base64' 16 | require 'active_support/all' 17 | require 'cgi' 18 | require 'openssl' 19 | require_relative './response' 20 | require_relative './exception' 21 | 22 | module IntuitOAuth 23 | class Utils 24 | def self.scopes_to_string(scopes) 25 | scopes_str = scopes.join(' ') 26 | scopes_str.chomp(' ') 27 | end 28 | 29 | def self.get_auth_header(client_id, client_secret) 30 | encoded = Base64.strict_encode64("#{client_id}:#{client_secret}") 31 | "Basic #{encoded}" 32 | end 33 | 34 | def self.generate_random_string(length=20) 35 | Array.new(length){[*'A'..'Z', *'0'..'9', *'a'..'z'].sample}.join 36 | end 37 | 38 | def self.get_oauth1_header(method, uri, oauth1_tokens, uri_params={}) 39 | params = { 40 | oauth_consumer_key: oauth1_tokens[:consumer_key], 41 | oauth_token: oauth1_tokens[:access_token], 42 | oauth_signature_method: 'HMAC-SHA1', 43 | oauth_timestamp: Time.now.getutc.to_i.to_s, 44 | oauth_nonce: generate_random_string(7), 45 | oauth_version: '1.0' 46 | } 47 | 48 | all_params = params.merge(uri_params).sort.to_h 49 | base_string = "#{method.upcase}&#{CGI.escape(uri)}&#{CGI.escape(all_params.to_param)}" 50 | key = "#{oauth1_tokens[:consumer_secret]}&#{oauth1_tokens[:access_secret]}" 51 | 52 | signature = CGI.escape(Base64.strict_encode64(OpenSSL::HMAC.digest('sha1', key, base_string).to_s)) 53 | params[:oauth_signature] = signature 54 | "OAuth #{format_string_delimiter(params, ',', true)}" 55 | end 56 | 57 | def self.format_string_delimiter(params, delimiter, with_quotes=false) 58 | if with_quotes 59 | return params.map { |k, v| "#{k}=\"#{v}\"" }.join(delimiter) 60 | end 61 | params.map { |k, v| "#{k}=#{v}" }.join(delimiter) 62 | end 63 | 64 | def self.build_response_object(response) 65 | url = response.request.last_uri.to_s 66 | if response.code != 200 67 | raise OAuth2ClientException.new(response) 68 | elsif url['openid_sandbox_configuration'] || url['openid_configuration'] || url['openid_connect/userinfo'] 69 | response 70 | else 71 | IntuitOAuth::ClientResponse.new(response) 72 | end 73 | end 74 | end 75 | end 76 | -------------------------------------------------------------------------------- /views/PRSWidget.txt: -------------------------------------------------------------------------------- 1 | This is the folder for PRS view image 2 | -------------------------------------------------------------------------------- /views/SDK.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intuit/oauth-rubyclient/c783b5f95e1008bc60f594f343e73ad842394fbd/views/SDK.png --------------------------------------------------------------------------------