Add API Authorization

This commit is contained in:
Saurabh Bhatia 2014-02-11 18:14:45 +08:00
parent 66ebd5e622
commit 702b46f416
7 changed files with 72 additions and 2 deletions

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -1,2 +1,3 @@
store_settings:
url: "http://store.tp.rulingcom.com"
api_key: "cc6ae8d0aa0a730792cf519225c40099"

View File

@ -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

9
lib/store.rb Normal file
View File

@ -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