horse_power 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/generators/horse_power/make_admin/make_admin_generator.rb +1 -1
- data/lib/generators/horse_power/model/model_generator.rb +2 -2
- data/lib/generators/horse_power/user/templates/admin.rb.erb +2 -1
- data/lib/generators/horse_power/user/templates/authorization.rb.erb +6 -0
- data/lib/generators/horse_power/user/templates/controller.rb.erb +27 -1
- data/lib/generators/horse_power/user/templates/facebook.rb +48 -0
- data/lib/generators/horse_power/user/templates/model.rb.erb +28 -0
- data/lib/generators/horse_power/user/templates/serializer.rb.erb +5 -0
- data/lib/generators/horse_power/user/templates/specs/factory.rb.erb +1 -0
- data/lib/generators/horse_power/user/templates/specs/requests.rb.erb +4 -0
- data/lib/generators/horse_power/user/templates/specs/routing.rb.erb +6 -0
- data/lib/generators/horse_power/user/user_generator.rb +8 -1
- data/lib/horse_power/version.rb +1 -1
- data/lib/horse_power.rb +9 -9
- metadata +3 -3
- data/README.rdoc +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b866611178f0a0657301cf04b18e7a3290535d14
|
4
|
+
data.tar.gz: 75ad42579bd6ce7b95227c2a4fa987f8274ed17b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ca5504904cb6589fa094060e0cf621d29661d9d13e325b421cc8ddcf91090fff00c57cccc170d9aa588ef6b3472dd7f0f0d90cddde67036b88f8d9bc619ad6a1
|
7
|
+
data.tar.gz: 7d533ea51ac0da6ac99abe719c7a38fd061b45b477a0ad1877833b3fc036a616e95f49cd2153528a4d3724c4d5c84976586046c86e9676c24a6ff72ce9ce6220
|
@@ -10,8 +10,8 @@ module HorsePower
|
|
10
10
|
# We only need this to create the migration
|
11
11
|
if api_version == "1"
|
12
12
|
run "rails g model #{HorsePower.get_singular(resource_name)} #{attributes.join(" ")} --no-fixture --skip"
|
13
|
-
run "rm -rf spec/factories
|
14
|
-
run "rm -rf spec/models
|
13
|
+
run "rm -rf spec/factories/#{HorsePower.get_plural(resource_name)}.rb"
|
14
|
+
run "rm -rf spec/models/#{HorsePower.get_singular(resource_name)}_spec.rb"
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
@@ -1,5 +1,5 @@
|
|
1
1
|
ActiveAdmin.register User do
|
2
|
-
permit_params :username, :email, :password, :password_confirmation
|
2
|
+
permit_params :username, :email, :password, :password_confirmation, :fb_user_id
|
3
3
|
|
4
4
|
config.per_page = 50
|
5
5
|
|
@@ -11,6 +11,7 @@ ActiveAdmin.register User do
|
|
11
11
|
f.input :email
|
12
12
|
f.input :password
|
13
13
|
f.input :password_confirmation
|
14
|
+
f.input :fb_user_id
|
14
15
|
f.input :updated_at
|
15
16
|
f.input :created_at
|
16
17
|
end
|
@@ -62,6 +62,12 @@ module Authorization
|
|
62
62
|
return true
|
63
63
|
end
|
64
64
|
|
65
|
+
def self.include_fb_user_id?(current_user,user_object,options)
|
66
|
+
action = options[:url_options][:_recall][:action]
|
67
|
+
controller = options[:url_options][:_recall][:controller]
|
68
|
+
return true
|
69
|
+
end
|
70
|
+
|
65
71
|
def self.include_password_digest?(current_user,user_object,options)
|
66
72
|
action = options[:url_options][:_recall][:action]
|
67
73
|
controller = options[:url_options][:_recall][:controller]
|
@@ -3,7 +3,7 @@ require 'authorization'
|
|
3
3
|
|
4
4
|
class Api::V1::UsersController < Api::V1::ApplicationController
|
5
5
|
|
6
|
-
skip_before_filter :authenticate_user, :only => [:register, :login]
|
6
|
+
skip_before_filter :authenticate_user, :only => [:register, :login, :facebook_login]
|
7
7
|
before_action :set_user, only: [:show, :update, :destroy]
|
8
8
|
before_action :index_authorize, only: [:index]
|
9
9
|
before_action :show_authorize, only: [:show]
|
@@ -61,6 +61,17 @@ class Api::V1::UsersController < Api::V1::ApplicationController
|
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
|
+
# POST /api/1/users/facebook_login
|
65
|
+
def facebook_login
|
66
|
+
objHash = ::User.facebook_login(params)
|
67
|
+
if objHash.nil?
|
68
|
+
objHash = {error: "Could not retrieve the facebook user_id from your token"}
|
69
|
+
render :json => objHash, status: :unauthorized
|
70
|
+
else
|
71
|
+
render :json => objHash
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
64
75
|
# POST /api/1/users/logout
|
65
76
|
def logout
|
66
77
|
current_user.logout(current_token)
|
@@ -89,6 +100,21 @@ class Api::V1::UsersController < Api::V1::ApplicationController
|
|
89
100
|
render :json => {user: userHash,token: tokenHash}
|
90
101
|
end
|
91
102
|
|
103
|
+
def get_id(fb_api,long_token,short_token)
|
104
|
+
response = fb_api.get_user_id(long_token)
|
105
|
+
if response.code == 200
|
106
|
+
@token = long_token
|
107
|
+
return JSON.parse(response.body)["id"]
|
108
|
+
end
|
109
|
+
#Lets try it with the short token now
|
110
|
+
response = fb_api.get_user_id(short_token)
|
111
|
+
if response.code == 200
|
112
|
+
@token = short_token
|
113
|
+
return JSON.parse(response.body)["id"]
|
114
|
+
end
|
115
|
+
return nil
|
116
|
+
end
|
117
|
+
|
92
118
|
# Authorizations below here
|
93
119
|
|
94
120
|
def index_authorize
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
|
3
|
+
class Facebook
|
4
|
+
include ::HTTParty
|
5
|
+
base_uri 'https://graph.facebook.com/v2.1'
|
6
|
+
#format :json
|
7
|
+
#debug_output $stdout
|
8
|
+
|
9
|
+
def initialize()
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_long_token(short_token)
|
13
|
+
params = {
|
14
|
+
grant_type: "fb_exchange_token",
|
15
|
+
client_id: ENV["FB_APP_ID1"],
|
16
|
+
client_secret: ENV["FB_APP_SECRET1"],
|
17
|
+
fb_exchange_token: short_token
|
18
|
+
}
|
19
|
+
headers = { 'Content-Type' => 'application/json' }
|
20
|
+
response = self.class.get("/oauth/access_token", query: params, headers: headers)
|
21
|
+
if response.code == 200
|
22
|
+
return parse_token(response.body)
|
23
|
+
else
|
24
|
+
return ""
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def get_user_id(token)
|
29
|
+
options = { query:{
|
30
|
+
fields: "id",
|
31
|
+
access_token: token
|
32
|
+
}
|
33
|
+
}
|
34
|
+
response = self.class.get("/me", options)
|
35
|
+
return response
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def parse_token(response)
|
41
|
+
#access_token={access-token}&expires={seconds-til-expiration}
|
42
|
+
index = response.index('&')
|
43
|
+
start_index = "access_token=".size
|
44
|
+
length = index - start_index
|
45
|
+
return response.slice(start_index,length)
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
@@ -36,6 +36,8 @@ class User < ActiveRecord::Base
|
|
36
36
|
return user
|
37
37
|
end
|
38
38
|
|
39
|
+
#Should use one or the other for login
|
40
|
+
|
39
41
|
def self.login(user_params)
|
40
42
|
email = user_params[:email]
|
41
43
|
if email
|
@@ -49,6 +51,32 @@ class User < ActiveRecord::Base
|
|
49
51
|
end
|
50
52
|
end
|
51
53
|
|
54
|
+
def self.facebook_login(params)
|
55
|
+
short_token = params[:token]
|
56
|
+
fb_api = Facebook.new
|
57
|
+
long_token = fb_api.get_long_token(short_token)
|
58
|
+
fb_user_id = get_id(fb_api,long_token,short_token)
|
59
|
+
if fb_user_id.nil?
|
60
|
+
#Couldn't retrieve the facebook user_id from token
|
61
|
+
return nil;
|
62
|
+
end
|
63
|
+
@user = User.find_by(fb_user_id: fb_user_id)
|
64
|
+
if @user.nil?
|
65
|
+
#Create new user
|
66
|
+
params = {username: "user#{fb_user_id}", email: "#{fb_user_id}@email.com",
|
67
|
+
fb_user_id: fb_user_id, password: "password"}
|
68
|
+
@user = User.create(params)
|
69
|
+
@api_token = @user.tokens[0]
|
70
|
+
else
|
71
|
+
#Log in a user
|
72
|
+
@api_token = @user.tokens.create
|
73
|
+
end
|
74
|
+
::Arcadex::Create.set_token(@api_token,20000,request,nil)
|
75
|
+
userHash = {id: @user.id, username: @user.username, fb_user_id: @user.fb_user_id}
|
76
|
+
objHash = {user: userHash, api_token: @api_token.auth_token, fb_token: @token}
|
77
|
+
return objHash
|
78
|
+
end
|
79
|
+
|
52
80
|
def logout(token)
|
53
81
|
token.destroy
|
54
82
|
end
|
@@ -6,6 +6,7 @@ class V1::UserSerializer < ActiveModel::Serializer
|
|
6
6
|
attributes :id
|
7
7
|
attributes :email
|
8
8
|
attributes :username
|
9
|
+
attributes :fb_user_id
|
9
10
|
attributes :password_digest
|
10
11
|
attributes :created_at
|
11
12
|
attributes :updated_at
|
@@ -25,6 +26,10 @@ class V1::UserSerializer < ActiveModel::Serializer
|
|
25
26
|
return ::Authorization::V1::User.include_username?(current_user,object,@options)
|
26
27
|
end
|
27
28
|
|
29
|
+
def include_fb_user_id?
|
30
|
+
return ::Authorization::V1::User.include_fb_user_id?(current_user,object,@options)
|
31
|
+
end
|
32
|
+
|
28
33
|
def include_password_digest?
|
29
34
|
return ::Authorization::V1::User.include_password_digest?(current_user,object,@options)
|
30
35
|
end
|
@@ -199,6 +199,10 @@ RSpec.describe User, :type => :request do
|
|
199
199
|
#Errors need to be returned
|
200
200
|
expect(json["errors"]).to_not eq(nil)
|
201
201
|
end
|
202
|
+
# post /api/1/users/facebook_login
|
203
|
+
it "checks response of a valid facebook_login request" do
|
204
|
+
|
205
|
+
end
|
202
206
|
end
|
203
207
|
end
|
204
208
|
RSpec.describe User, :type => :request do
|
@@ -49,6 +49,12 @@ RSpec.describe "Users controller rest routing", :type => :routing do
|
|
49
49
|
:action => "login"
|
50
50
|
)
|
51
51
|
end
|
52
|
+
it "routes create to facebook_login" do
|
53
|
+
expect(:post => "/api/1/users/facebook_login").to route_to(
|
54
|
+
:controller => "api/v1/users",
|
55
|
+
:action => "facebook_login"
|
56
|
+
)
|
57
|
+
end
|
52
58
|
it "routes create to logout" do
|
53
59
|
expect(:post => "/api/1/users/logout").to route_to(
|
54
60
|
:controller => "api/v1/users",
|
@@ -11,12 +11,18 @@ module HorsePower
|
|
11
11
|
create_admin
|
12
12
|
create_tests
|
13
13
|
create_routes
|
14
|
+
create_facebook
|
14
15
|
end
|
15
16
|
|
16
17
|
private
|
17
18
|
|
19
|
+
def create_facebook
|
20
|
+
run "mkdir app/facebook"
|
21
|
+
template "facebook.rb", "app/facebook/facebook.rb"
|
22
|
+
end
|
23
|
+
|
18
24
|
def create_migration
|
19
|
-
run "rails g model user username:string:uniq email:string:uniq password_digest:string --no-fixture --skip"
|
25
|
+
run "rails g model user username:string:uniq email:string:uniq fb_user_id:integer:uniq password_digest:string --no-fixture --skip"
|
20
26
|
run "rm -rf app/models/user.rb"
|
21
27
|
run "rm -rf spec/models/user_spec.rb"
|
22
28
|
run "rm -rf spec/factories/users.rb"
|
@@ -56,6 +62,7 @@ module HorsePower
|
|
56
62
|
collection do
|
57
63
|
post 'register', to: "api/v1/users#register"
|
58
64
|
post 'login', to: "api/v1/users#login"
|
65
|
+
post 'facebook_login', to: "api/v1/users#facebook_login"
|
59
66
|
post 'logout', to: "api/v1/users#logout"
|
60
67
|
end
|
61
68
|
end
|
data/lib/horse_power/version.rb
CHANGED
data/lib/horse_power.rb
CHANGED
@@ -2,15 +2,15 @@ require "horse_power/engine"
|
|
2
2
|
|
3
3
|
module HorsePower
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
5
|
+
def self.params_list(attributes)
|
6
|
+
params = []
|
7
|
+
attributes.each do |pair|
|
8
|
+
elem = pair.split(/:/)[0]
|
9
|
+
field = ":#{elem}"
|
10
|
+
params << field
|
11
|
+
end
|
12
|
+
return params.join(",")
|
13
|
+
end
|
14
14
|
|
15
15
|
def self.get_column(pair)
|
16
16
|
return pair.split(/:/)[0]
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: horse_power
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cleophus Robinson IV
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-03-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -55,7 +55,6 @@ extensions: []
|
|
55
55
|
extra_rdoc_files: []
|
56
56
|
files:
|
57
57
|
- MIT-LICENSE
|
58
|
-
- README.rdoc
|
59
58
|
- Rakefile
|
60
59
|
- app/assets/javascripts/horse_power/application.js
|
61
60
|
- app/assets/stylesheets/horse_power/application.css
|
@@ -104,6 +103,7 @@ files:
|
|
104
103
|
- lib/generators/horse_power/user/templates/admin.rb.erb
|
105
104
|
- lib/generators/horse_power/user/templates/authorization.rb.erb
|
106
105
|
- lib/generators/horse_power/user/templates/controller.rb.erb
|
106
|
+
- lib/generators/horse_power/user/templates/facebook.rb
|
107
107
|
- lib/generators/horse_power/user/templates/model.rb.erb
|
108
108
|
- lib/generators/horse_power/user/templates/serializer.rb.erb
|
109
109
|
- lib/generators/horse_power/user/templates/specs/factory.rb.erb
|
data/README.rdoc
DELETED