From 5c603bf8ec2dfe0b51a232c89eca3bfbaaa3bbe9 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Thu, 25 Feb 2016 01:21:59 +0000 Subject: [PATCH 1/2] Added Instagram login method --- Gemfile | 1 + .../discourse/models/login_method.js | 1 + .../common/components/buttons.css.scss | 6 +++ .../common/foundation/variables.scss | 1 + .../users/omniauth_callbacks_controller.rb | 5 +- app/models/instagram_user_info.rb | 5 ++ config/locales/client.en.yml | 3 ++ config/locales/server.en.yml | 4 ++ config/site_settings.yml | 9 ++++ ...60224033122_create_instagram_user_infos.rb | 11 +++++ lib/auth.rb | 1 + lib/auth/instagram_authenticator.rb | 49 +++++++++++++++++++ 12 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 app/models/instagram_user_info.rb create mode 100644 db/migrate/20160224033122_create_instagram_user_infos.rb create mode 100644 lib/auth/instagram_authenticator.rb diff --git a/Gemfile b/Gemfile index 3f5a46fb761..752bfbbe960 100644 --- a/Gemfile +++ b/Gemfile @@ -78,6 +78,7 @@ gem 'omniauth-openid' gem 'openid-redis-store' gem 'omniauth-facebook' gem 'omniauth-twitter' +gem 'omniauth-instagram' # forked while https://github.com/intridea/omniauth-github/pull/41 is being upstreamd gem 'omniauth-github-discourse', require: 'omniauth-github' diff --git a/app/assets/javascripts/discourse/models/login_method.js b/app/assets/javascripts/discourse/models/login_method.js index 9411cfee9e4..dff01f0317a 100644 --- a/app/assets/javascripts/discourse/models/login_method.js +++ b/app/assets/javascripts/discourse/models/login_method.js @@ -37,6 +37,7 @@ Discourse.LoginMethod.reopenClass({ "cas", "twitter", "yahoo", + "instagram", "github" ].forEach(function(name){ if (Discourse.SiteSettings["enable_" + name + "_logins"]) { diff --git a/app/assets/stylesheets/common/components/buttons.css.scss b/app/assets/stylesheets/common/components/buttons.css.scss index bf42ff0f088..a0b606291e8 100644 --- a/app/assets/stylesheets/common/components/buttons.css.scss +++ b/app/assets/stylesheets/common/components/buttons.css.scss @@ -129,6 +129,12 @@ content: $fa-var-google; } } + &.instagram { + background: $instagram; + &:before { + content: $fa-var-instagram; + } + } &.facebook { background: $facebook; &:before { diff --git a/app/assets/stylesheets/common/foundation/variables.scss b/app/assets/stylesheets/common/foundation/variables.scss index b09dbc2893a..b06099e3b5f 100644 --- a/app/assets/stylesheets/common/foundation/variables.scss +++ b/app/assets/stylesheets/common/foundation/variables.scss @@ -13,6 +13,7 @@ $large-width: 1110px !default; // -------------------------------------------------- $google: #5b76f7 !default; +$instagram: #125688 !default; $facebook: #3b5998 !default; $cas: #70BA61 !default; $twitter: #00bced !default; diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb index a7fc7aaf493..dd1a3cef800 100644 --- a/app/controllers/users/omniauth_callbacks_controller.rb +++ b/app/controllers/users/omniauth_callbacks_controller.rb @@ -10,7 +10,8 @@ class Users::OmniauthCallbacksController < ApplicationController Auth::GoogleOAuth2Authenticator.new, Auth::OpenIdAuthenticator.new("yahoo", "https://me.yahoo.com", trusted: true), Auth::GithubAuthenticator.new, - Auth::TwitterAuthenticator.new + Auth::TwitterAuthenticator.new, + Auth::InstagramAuthenticator.new ] skip_before_filter :redirect_to_login_if_required @@ -18,7 +19,7 @@ class Users::OmniauthCallbacksController < ApplicationController layout false def self.types - @types ||= Enum.new(:facebook, :twitter, :google, :yahoo, :github, :persona, :cas) + @types ||= Enum.new(:facebook, :instagram, :twitter, :google, :yahoo, :github, :persona, :cas) end # need to be able to call this diff --git a/app/models/instagram_user_info.rb b/app/models/instagram_user_info.rb new file mode 100644 index 00000000000..c16a432934f --- /dev/null +++ b/app/models/instagram_user_info.rb @@ -0,0 +1,5 @@ +class InstagramUserInfo < ActiveRecord::Base + + belongs_to :user + +end diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 8e434f3bd32..92c5f38202e 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -882,6 +882,9 @@ en: twitter: title: "with Twitter" message: "Authenticating with Twitter (make sure pop up blockers are not enabled)" + instagram: + title: "with Instagram" + message: "Authenticating with Instagram (make sure pop up blockers are not enabled)" facebook: title: "with Facebook" message: "Authenticating with Facebook (make sure pop up blockers are not enabled)" diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 431b7d7f659..b0d028d41df 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -939,6 +939,10 @@ en: twitter_consumer_key: "Consumer key for Twitter authentication, registered at http://dev.twitter.com" twitter_consumer_secret: "Consumer secret for Twitter authentication, registered at http://dev.twitter.com" + enable_instagram_logins: "Enable Instagram authentication, requires instagram_consumer_key and instagram_consumer_secret" + instagram_consumer_key: "Consumer key for Instagram authentication" + instagram_consumer_secret: "Consumer secret Instagram authentication" + enable_facebook_logins: "Enable Facebook authentication, requires facebook_app_id and facebook_app_secret" facebook_app_id: "App id for Facebook authentication, registered at https://developers.facebook.com/apps" facebook_app_secret: "App secret for Facebook authentication, registered at https://developers.facebook.com/apps" diff --git a/config/site_settings.yml b/config/site_settings.yml index 64990a3e2e6..955f0e557a5 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -234,6 +234,15 @@ login: twitter_consumer_secret: default: '' regex: "^[a-zA-Z0-9_+-]+$" + enable_instagram_logins: + client: true + default: false + instagram_consumer_key: + default: '' + regex: "^[a-z0-9]+$" + instagram_consumer_secret: + default: '' + regex: "^[a-z0-9]+$" enable_facebook_logins: client: true default: false diff --git a/db/migrate/20160224033122_create_instagram_user_infos.rb b/db/migrate/20160224033122_create_instagram_user_infos.rb new file mode 100644 index 00000000000..ef3633f8926 --- /dev/null +++ b/db/migrate/20160224033122_create_instagram_user_infos.rb @@ -0,0 +1,11 @@ +class CreateInstagramUserInfos < ActiveRecord::Migration + def change + create_table :instagram_user_infos do |t| + t.integer :user_id + t.string :screen_name + t.integer :instagram_user_id + + t.timestamps null: false + end + end +end diff --git a/lib/auth.rb b/lib/auth.rb index a218c43f28c..f94ba5cf43c 100644 --- a/lib/auth.rb +++ b/lib/auth.rb @@ -7,3 +7,4 @@ require_dependency 'auth/open_id_authenticator' require_dependency 'auth/github_authenticator' require_dependency 'auth/twitter_authenticator' require_dependency 'auth/google_oauth2_authenticator' +require_dependency 'auth/instagram_authenticator' diff --git a/lib/auth/instagram_authenticator.rb b/lib/auth/instagram_authenticator.rb new file mode 100644 index 00000000000..e510529bce1 --- /dev/null +++ b/lib/auth/instagram_authenticator.rb @@ -0,0 +1,49 @@ +class Auth::InstagramAuthenticator < Auth::Authenticator + + def name + "instagram" + end + + # TODO twitter provides all sorts of extra info, like website/bio etc. + # it may be worth considering pulling some of it in. + def after_authenticate(auth_token) + + result = Auth::Result.new + + data = auth_token[:info] + + result.username = screen_name = data["nickname"] + result.name = name = data["name"] + instagram_user_id = auth_token["uid"] + + result.extra_data = { + instagram_user_id: instagram_user_id, + instagram_screen_name: screen_name + } + + user_info = InstagramUserInfo.find_by(instagram_user_id: instagram_user_id) + + result.user = user_info.try(:user) + + result + end + + def after_create_account(user, auth) + data = auth[:extra_data] + InstagramUserInfo.create( + user_id: user.id, + screen_name: data[:instagram_screen_name], + instagram_user_id: data[:instagram_user_id] + ) + end + + def register_middleware(omniauth) + omniauth.provider :instagram, + :setup => lambda { |env| + strategy = env["omniauth.strategy"] + strategy.options[:client_id] = SiteSetting.instagram_consumer_key + strategy.options[:client_secret] = SiteSetting.instagram_consumer_secret + } + end + +end From 70b287a053be849d34f47133f80023d2f9687bac Mon Sep 17 00:00:00 2001 From: Mark Biegel Date: Fri, 26 Feb 2016 11:37:48 +1000 Subject: [PATCH 2/2] Update instagram_authenticator.rb --- lib/auth/instagram_authenticator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/auth/instagram_authenticator.rb b/lib/auth/instagram_authenticator.rb index e510529bce1..ba2dd7f1bb0 100644 --- a/lib/auth/instagram_authenticator.rb +++ b/lib/auth/instagram_authenticator.rb @@ -13,7 +13,7 @@ class Auth::InstagramAuthenticator < Auth::Authenticator data = auth_token[:info] result.username = screen_name = data["nickname"] - result.name = name = data["name"] + result.name = name = data["name"].slice!(0) instagram_user_id = auth_token["uid"] result.extra_data = {