From 702b46f416d99bb110601859478265b6cf74752a Mon Sep 17 00:00:00 2001 From: Saurabh Bhatia Date: Tue, 11 Feb 2014 18:14:45 +0800 Subject: [PATCH] Add API Authorization --- .../admin/omniauth_callbacks_controller.rb | 10 +++++++ app/controllers/admin/sites_controller.rb | 7 +++++ app/controllers/application_controller.rb | 9 +++++++ app/models/site.rb | 12 +++++++-- config/store_config.yml | 1 + lib/omniauth/strategies/doorkeeper.rb | 26 +++++++++++++++++++ lib/store.rb | 9 +++++++ 7 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 app/controllers/admin/omniauth_callbacks_controller.rb create mode 100644 lib/omniauth/strategies/doorkeeper.rb create mode 100644 lib/store.rb diff --git a/app/controllers/admin/omniauth_callbacks_controller.rb b/app/controllers/admin/omniauth_callbacks_controller.rb new file mode 100644 index 0000000..f034740 --- /dev/null +++ b/app/controllers/admin/omniauth_callbacks_controller.rb @@ -0,0 +1,10 @@ +class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController + def doorkeeper + oauth_data = request.env["omniauth.auth"] + @user = User.find_or_create_for_doorkeeper_oauth(oauth_data) + @user.update_doorkeeper_credentials(oauth_data) + @user.save + + sign_in_and_redirect @user + end +end \ No newline at end of file diff --git a/app/controllers/admin/sites_controller.rb b/app/controllers/admin/sites_controller.rb index 2e492dd..31ed337 100644 --- a/app/controllers/admin/sites_controller.rb +++ b/app/controllers/admin/sites_controller.rb @@ -147,6 +147,13 @@ class Admin::SitesController < OrbitBackendController render :text => "success" end + def generate_site_token + begin + self.site_token = SecureRandom.uuid.gsub('-','') + self.save + end + end + protected def update_design(design) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c8fcfe2..f44590e 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -479,4 +479,13 @@ class ApplicationController < ActionController::Base end end + def check_central_server_connection + site = Site.first + if site.site_token? + flash[:notice]="Connected to the Store" + else + flash[:notice]="To Access the Store Please Connect It" + end + end + end diff --git a/app/models/site.rb b/app/models/site.rb index a37cbbb..f1de713 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -46,14 +46,15 @@ class Site field :address field :phone_number, :type => Array,:default=>[] field :mobile_bar_color, :type => Array, :default=>[] - - + field :site_token field :mobile_on, :type => Boolean, :default => false belongs_to :design has_many :site_metas, :autosave => true, :dependent => :destroy validate :in_use_locales, :minimum_enabled_locales + index({ access_token: 1}, { unique: true }) + def minimum_enabled_locales size = self.in_use_locales.length if size < 1 @@ -95,5 +96,12 @@ class Site fetch_meta.save end end + + def generate_site_token + begin + self.site_token = SecureRandom.uuid.gsub('-','') + self.save + end + end end diff --git a/config/store_config.yml b/config/store_config.yml index cd0fd3b..def0151 100644 --- a/config/store_config.yml +++ b/config/store_config.yml @@ -1,2 +1,3 @@ store_settings: url: "http://store.tp.rulingcom.com" + api_key: "cc6ae8d0aa0a730792cf519225c40099" diff --git a/lib/omniauth/strategies/doorkeeper.rb b/lib/omniauth/strategies/doorkeeper.rb new file mode 100644 index 0000000..16bdb93 --- /dev/null +++ b/lib/omniauth/strategies/doorkeeper.rb @@ -0,0 +1,26 @@ +module OmniAuth + module Strategies + class Doorkeeper < OmniAuth::Strategies::OAuth2 + option :name, :doorkeeper + + option :client_options, { + :site => "http://localhost:8000", + :authorize_path => "/oauth/authorize" + } + + uid do + raw_info["id"] + end + + info do + { + :email => raw_info["email"] + } + end + + def raw_info + @raw_info ||= access_token.get('/api/v1/me.json').parsed + end + end + end +end \ No newline at end of file diff --git a/lib/store.rb b/lib/store.rb new file mode 100644 index 0000000..773454c --- /dev/null +++ b/lib/store.rb @@ -0,0 +1,9 @@ +class Store + include HTTParty + base_uri 'http://localhost:3000' + + def post(text) + options = { :body => {:status => text}, :token => @auth } + self.class.post('/api/clients', options) + end +end \ No newline at end of file