├── .gitignore ├── .rubocop-https---raw-githubusercontent-com-tmilewski-rubocop-defaults-master-rubocop-yml ├── .rubocop.yml ├── Gemfile ├── README.md ├── Rakefile ├── lib ├── omniauth-instagram.rb ├── omniauth-instagram │ └── version.rb └── omniauth │ └── strategies │ └── instagram.rb ├── omniauth-instagram.gemspec └── spec ├── omniauth └── strategies │ └── instagram_spec.rb └── spec_helper.rb /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | Gemfile.lock 7 | InstalledFiles 8 | _yardoc 9 | coverage 10 | doc/ 11 | lib/bundler/man 12 | /pkg 13 | rdoc 14 | spec/reports 15 | test/tmp 16 | test/version_tmp 17 | tmp 18 | -------------------------------------------------------------------------------- /.rubocop-https---raw-githubusercontent-com-tmilewski-rubocop-defaults-master-rubocop-yml: -------------------------------------------------------------------------------- 1 | # see https://github.com/bbatsov/rubocop/blob/master/config/default.yml 2 | # for default configuration 3 | 4 | AllCops: 5 | TargetRubyVersion: 2.4 6 | DisplayCopNames: true 7 | Exclude: 8 | - 'db/schema.rb' 9 | - 'bin/*' 10 | - 'vendor/**/*' 11 | 12 | Rails: 13 | Enabled: true 14 | 15 | Documentation: 16 | Enabled: false 17 | 18 | Metrics/LineLength: 19 | Max: 125 20 | Exclude: 21 | - 'db/migrate/*' 22 | - 'config/initializers/devise.rb' 23 | 24 | Metrics/MethodLength: 25 | CountComments: false 26 | Max: 10 27 | Exclude: 28 | - 'db/migrate/*' 29 | 30 | Metrics/ModuleLength: 31 | CountComments: false 32 | Max: 100 33 | 34 | Metrics/AbcSize: 35 | # The ABC size is a calculated magnitude, so this number can be a Fixnum or a Float. 36 | # http://c2.com/cgi/wiki?AbcMetric 37 | Max: 15 38 | 39 | Metrics/CyclomaticComplexity: 40 | # http://www.rubydoc.info/github/bbatsov/rubocop/Rubocop/Cop/Style/CyclomaticComplexity 41 | Max: 6 42 | 43 | Metrics/PerceivedComplexity: 44 | # http://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Metrics/PerceivedComplexity 45 | Max: 7 46 | 47 | Metrics/ClassLength: 48 | CountComments: false 49 | Max: 100 50 | 51 | Style/MultilineMethodCallIndentation: 52 | EnforcedStyle: indented 53 | 54 | Style/FrozenStringLiteralComment: 55 | Enabled: false 56 | 57 | Style/ClassAndModuleChildren: 58 | Enabled: false 59 | -------------------------------------------------------------------------------- /.rubocop.yml: -------------------------------------------------------------------------------- 1 | inherit_from: 2 | - https://raw.githubusercontent.com/tmilewski/rubocop-defaults/master/rubocop.yml 3 | 4 | Style/FileName: 5 | Enabled: false 6 | 7 | AllCops: 8 | Exclude: 9 | - 'tmp/**/*' 10 | 11 | Metrics/MethodLength: 12 | CountComments: false 13 | Max: 12 14 | 15 | Metrics/AbcSize: 16 | Max: 20.35 17 | 18 | Style/FormatString: 19 | Enabled: false 20 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'http://rubygems.org' 2 | 3 | gem 'rake' 4 | 5 | group :development do 6 | gem 'rubocop', '>= 0.48.1' 7 | end 8 | 9 | group :test do 10 | gem 'webmock', '~> 3.13' 11 | gem 'rack-test' 12 | gem 'rspec', '~> 3.6.0' # '~> 3.6.0' 13 | gem 'simplecov', require: false 14 | end 15 | 16 | gemspec 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OmniAuth Instagram 2 | 3 | This is the unofficial OmniAuth strategy for authenticating to Instagram. To 4 | use it, you'll need to sign up for an OAuth2 Application ID and Secret 5 | on the [Instagram Developer website](http://instagram.com/developer/). 6 | 7 | ## Basic Usage 8 | 9 | use OmniAuth::Builder do 10 | provider :instagram, ENV['INSTAGRAM_ID'], ENV['INSTAGRAM_SECRET'], scope: 'basic+media+public_content+follower_list+comments+relationships+likes' 11 | end 12 | 13 | ## Notes: 14 | - For more information on scopes: https://www.instagram.com/developer/authorization/ 15 | - Instagram has started enforcing signed requests for its API. If you have enabled `Enforce signed requests` in your app then, you can pass `enforce_signed_requests: true` in the above configuration. More info: https://instagram.com/developer/secure-api-requests/ 16 | 17 | ## License 18 | 19 | Copyright (c) 2011-2017 Mihai Anca and Intridea, Inc. 20 | 21 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 22 | 23 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env rake 2 | require 'bundler/gem_tasks' 3 | require 'rspec/core/rake_task' 4 | RSpec::Core::RakeTask.new(:spec) 5 | 6 | task test: :spec 7 | 8 | begin 9 | require 'rubocop/rake_task' 10 | RuboCop::RakeTask.new 11 | rescue LoadError 12 | task :rubocop do 13 | $stderr.puts 'Rubocop is disabled' 14 | end 15 | end 16 | 17 | task default: %i[spec rubocop] 18 | -------------------------------------------------------------------------------- /lib/omniauth-instagram.rb: -------------------------------------------------------------------------------- 1 | require 'omniauth-instagram/version' 2 | require 'omniauth/strategies/instagram' 3 | -------------------------------------------------------------------------------- /lib/omniauth-instagram/version.rb: -------------------------------------------------------------------------------- 1 | module OmniAuth 2 | module Instagram 3 | VERSION = '2.0'.freeze 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /lib/omniauth/strategies/instagram.rb: -------------------------------------------------------------------------------- 1 | require 'omniauth-oauth2' 2 | 3 | module OmniAuth 4 | module Strategies 5 | class Instagram < OmniAuth::Strategies::OAuth2 6 | option :client_options, site: 'https://api.instagram.com', 7 | authorize_url: 'https://api.instagram.com/oauth/authorize', 8 | token_url: 'https://api.instagram.com/oauth/access_token' 9 | 10 | def callback_url 11 | full_host + script_name + callback_path 12 | end 13 | 14 | def request_phase 15 | options[:scope] ||= 'instagram_basic' 16 | options[:response_type] ||= 'code' 17 | options[:enforce_signed_requests] ||= false 18 | options[:extra_data] ||= false 19 | super 20 | end 21 | 22 | uid { raw_info['id'] } 23 | 24 | info do 25 | { 26 | 'nickname' => raw_info['username'], 27 | 'name' => raw_info['full_name'], 28 | 'image' => raw_info['profile_picture'], 29 | 'bio' => raw_info['bio'], 30 | 'website' => raw_info['website'], 31 | 'is_business' => raw_info['is_business'] 32 | } 33 | end 34 | 35 | extra do 36 | hash = {} 37 | hash['raw_info'] = raw_info 38 | hash 39 | end 40 | 41 | def raw_info 42 | if options[:extra_data] 43 | endpoint = '/users/self' 44 | params = {} 45 | access_token.options[:mode] = :query 46 | access_token.options[:param_name] = 'access_token' 47 | params['sig'] = generate_sig(endpoint, 'access_token' => access_token.token) if options[:enforce_signed_requests] 48 | @data ||= access_token.get("/v1#{endpoint}", params: params).parsed['data'] || {} 49 | else 50 | @data ||= access_token.params['user'] 51 | end 52 | @data 53 | end 54 | 55 | # You can pass +scope+ params to the auth request, if you need to set them dynamically. 56 | # You can also set these options in the OmniAuth config :authorize_params option. 57 | # 58 | # For example: /auth/instagram?scope=likes+photos 59 | def authorize_params 60 | super.tap do |params| 61 | %w[scope].each do |v| 62 | params[v.to_sym] = request.params[v] if request.params[v] 63 | if params[v.to_sym] 64 | params[v.to_sym] = Array(params[v.to_sym]).join(' ') 65 | end 66 | end 67 | end 68 | end 69 | 70 | def generate_sig(endpoint, params) 71 | require 'openssl' 72 | require 'base64' 73 | sig = endpoint 74 | secret = options[:client_secret] 75 | params.sort.map do |key, val| 76 | sig += '|%s=%s' % [key, val] 77 | end 78 | digest = OpenSSL::Digest.new('sha256') 79 | OpenSSL::HMAC.hexdigest(digest, secret, sig) 80 | end 81 | end 82 | end 83 | end 84 | -------------------------------------------------------------------------------- /omniauth-instagram.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | 3 | require File.expand_path('../lib/omniauth-instagram/version', __FILE__) 4 | 5 | Gem::Specification.new do |gem| 6 | gem.authors = ['Mihai Anca'] 7 | gem.email = ['mihai@mihaia.com'] 8 | gem.description = 'OmniAuth strategy for Instagram.' 9 | gem.summary = 'OmniAuth strategy for Instagram.' 10 | gem.homepage = 'https://github.com/ropiku/omniauth-instagram' 11 | 12 | gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) } 13 | gem.files = `git ls-files`.split("\n") 14 | gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") 15 | gem.name = 'omniauth-instagram' 16 | gem.require_paths = ['lib'] 17 | gem.version = OmniAuth::Instagram::VERSION 18 | 19 | gem.add_dependency 'omniauth', '~> 2.0' 20 | gem.add_dependency 'omniauth-oauth2', '~> 1.7' 21 | 22 | # s.add_development_dependency 'dotenv', '>= 2.0' 23 | # s.add_development_dependency 'sinatra', '>= 2.0' 24 | end 25 | -------------------------------------------------------------------------------- /spec/omniauth/strategies/instagram_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe OmniAuth::Strategies::Instagram do 4 | context 'client options' do 5 | subject do 6 | OmniAuth::Strategies::Instagram.new({}) 7 | end 8 | 9 | it 'should have the correct site' do 10 | expect(subject.options.client_options.site).to eq('https://api.instagram.com') 11 | end 12 | 13 | it 'should have the correct authorize url' do 14 | expect(subject.options.client_options.authorize_url).to eq('https://api.instagram.com/oauth/authorize') 15 | end 16 | 17 | it 'should have the correct token url' do 18 | expect(subject.options.client_options.token_url).to eq('https://api.instagram.com/oauth/access_token') 19 | end 20 | end 21 | end 22 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | $LOAD_PATH.unshift File.expand_path('..', __FILE__) 2 | $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) 3 | require 'simplecov' 4 | SimpleCov.start 5 | require 'rspec' 6 | require 'rack/test' 7 | require 'webmock/rspec' 8 | require 'omniauth' 9 | require 'omniauth-instagram' 10 | 11 | RSpec.configure do |config| 12 | config.include WebMock::API 13 | config.include Rack::Test::Methods 14 | config.extend OmniAuth::Test::StrategyMacros, type: :strategy 15 | end 16 | --------------------------------------------------------------------------------