fbuser 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +34 -0
  4. data/app/assets/javascripts/fbuser/application.js +13 -0
  5. data/app/assets/javascripts/fbuser/authentication.js +2 -0
  6. data/app/assets/stylesheets/fbuser/application.css +15 -0
  7. data/app/assets/stylesheets/fbuser/authentication.css +4 -0
  8. data/app/controllers/fbuser/api/v1/application_controller.rb +5 -0
  9. data/app/controllers/fbuser/api/v1/authentication_controller.rb +56 -0
  10. data/app/controllers/fbuser/api/v1/users_controller.rb +100 -0
  11. data/app/controllers/fbuser/application_controller.rb +4 -0
  12. data/app/facebook/fbuser/facebook.rb +50 -0
  13. data/app/helpers/fbuser/application_helper.rb +4 -0
  14. data/app/helpers/fbuser/authentication_helper.rb +4 -0
  15. data/app/models/fbuser/user.rb +4 -0
  16. data/app/models/fbuser/v1/user.rb +32 -0
  17. data/app/serializers/fbuser/v1/user_serializer.rb +41 -0
  18. data/app/views/layouts/fbuser/default/application.html.erb +14 -0
  19. data/config/routes.rb +27 -0
  20. data/db/migrate/20141026184428_create_fbuser_users.rb +11 -0
  21. data/lib/fbuser.rb +4 -0
  22. data/lib/fbuser/engine.rb +5 -0
  23. data/lib/fbuser/version.rb +3 -0
  24. data/lib/generators/fbuser/admin/USAGE +8 -0
  25. data/lib/generators/fbuser/admin/admin_generator.rb +16 -0
  26. data/lib/generators/fbuser/admin/templates/active_admin.rb +84 -0
  27. data/lib/generators/fbuser/all/USAGE +8 -0
  28. data/lib/generators/fbuser/all/all_generator.rb +17 -0
  29. data/lib/generators/fbuser/authorizations/USAGE +8 -0
  30. data/lib/generators/fbuser/authorizations/authorizations_generator.rb +19 -0
  31. data/lib/generators/fbuser/authorizations/templates/user.rb +66 -0
  32. data/lib/generators/fbuser/tests/USAGE +8 -0
  33. data/lib/generators/fbuser/tests/templates/authentication_requests.rb +48 -0
  34. data/lib/generators/fbuser/tests/templates/authentication_routing.rb +20 -0
  35. data/lib/generators/fbuser/tests/templates/fbuser_user_1_factory.rb +18 -0
  36. data/lib/generators/fbuser/tests/templates/model_user_spec.rb +53 -0
  37. data/lib/generators/fbuser/tests/templates/requests_user_spec.rb +138 -0
  38. data/lib/generators/fbuser/tests/templates/routing_user_spec.rb +47 -0
  39. data/lib/generators/fbuser/tests/tests_generator.rb +21 -0
  40. data/lib/tasks/fbuser_tasks.rake +4 -0
  41. data/test/dummy/README.rdoc +28 -0
  42. data/test/dummy/Rakefile +6 -0
  43. data/test/dummy/app/assets/javascripts/application.js +13 -0
  44. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  45. data/test/dummy/app/controllers/application_controller.rb +5 -0
  46. data/test/dummy/app/helpers/application_helper.rb +2 -0
  47. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  48. data/test/dummy/bin/bundle +3 -0
  49. data/test/dummy/bin/rails +4 -0
  50. data/test/dummy/bin/rake +4 -0
  51. data/test/dummy/config.ru +4 -0
  52. data/test/dummy/config/application.rb +23 -0
  53. data/test/dummy/config/boot.rb +5 -0
  54. data/test/dummy/config/database.yml +25 -0
  55. data/test/dummy/config/environment.rb +5 -0
  56. data/test/dummy/config/environments/development.rb +37 -0
  57. data/test/dummy/config/environments/production.rb +78 -0
  58. data/test/dummy/config/environments/test.rb +39 -0
  59. data/test/dummy/config/initializers/assets.rb +8 -0
  60. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  61. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  62. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  63. data/test/dummy/config/initializers/inflections.rb +16 -0
  64. data/test/dummy/config/initializers/mime_types.rb +4 -0
  65. data/test/dummy/config/initializers/session_store.rb +3 -0
  66. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  67. data/test/dummy/config/locales/en.yml +23 -0
  68. data/test/dummy/config/routes.rb +4 -0
  69. data/test/dummy/config/secrets.yml +22 -0
  70. data/test/dummy/public/404.html +67 -0
  71. data/test/dummy/public/422.html +67 -0
  72. data/test/dummy/public/500.html +66 -0
  73. data/test/dummy/public/favicon.ico +0 -0
  74. data/test/fbuser_test.rb +7 -0
  75. data/test/fixtures/fbuser/users.yml +9 -0
  76. data/test/helpers/fbuser/authentication_helper_test.rb +6 -0
  77. data/test/integration/navigation_test.rb +10 -0
  78. data/test/lib/generators/fbuser/admin_generator_test.rb +16 -0
  79. data/test/lib/generators/fbuser/all_generator_test.rb +16 -0
  80. data/test/lib/generators/fbuser/authorizations_generator_test.rb +16 -0
  81. data/test/lib/generators/fbuser/tests_generator_test.rb +16 -0
  82. data/test/models/fbuser/user_test.rb +9 -0
  83. data/test/test_helper.rb +15 -0
  84. metadata +281 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ff2a3cdad2b3d309c9c49805f5804a3cfb0aa470
4
+ data.tar.gz: 1cfb86e1113423f72603a86a168c47ede724c339
5
+ SHA512:
6
+ metadata.gz: 843b471c715160c690c791ed02ffc75992d0af11fbb3216655273b47521aac35f5b9945eaa93c4059eed9434b327f05443fe04c54d669613542e23fe012398dc
7
+ data.tar.gz: 1a0eb50f992c46d49fb634b81483f2f7cd84731fb63ea15e7decd28c5087bd97282954f7ee5dab59517914ac56afb38e3601ef362438977c4318c40865c40e40
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Fbuser'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,5 @@
1
+ module Fbuser
2
+ class Api::V1::ApplicationController < ::Api::V1::ApiController
3
+
4
+ end
5
+ end
@@ -0,0 +1,56 @@
1
+ require_dependency "fbuser/api/v1/application_controller"
2
+
3
+ module Fbuser
4
+ class Api::V1::AuthenticationController < Api::V1::ApplicationController
5
+ skip_before_filter :authenticate_user, only: [:login]
6
+
7
+ def login
8
+ short_token = params[:token]
9
+ fb_api = ::Fbuser::Facebook.new
10
+ long_token = fb_api.get_long_token(short_token)
11
+ fb_user_id = get_id(fb_api,long_token,short_token)
12
+ if fb_user_id.nil?
13
+ #Couldn't retrieve the facebook user_id from token
14
+ render :json => {error: "Could not retrieve the facebook user_id from your token"}
15
+ return
16
+ end
17
+ @user = ::Fbuser::V1::User.find_by(fb_user_id: fb_user_id)
18
+ if @user.nil?
19
+ #Create new user
20
+ params = {username: "user#{fb_user_id}", fb_user_id: fb_user_id}
21
+ @user = ::Fbuser::V1::User.create(params)
22
+ @api_token = @user.tokens[0]
23
+ else
24
+ #Log in a user
25
+ @api_token = @user.tokens.create
26
+ end
27
+ ::Arcadex::Create.set_token(@api_token,20000,request,nil)
28
+ userHash = {id: @user.id, username: @user.username, fb_user_id: @user.fb_user_id}
29
+ objHash = {user: userHash, api_token: @api_token.auth_token, fb_token: @token}
30
+ render :json => objHash
31
+ end
32
+
33
+ def logout
34
+ current_token.destroy
35
+ render :json => {}
36
+ end
37
+
38
+ private
39
+
40
+ def get_id(fb_api,long_token,short_token)
41
+ response = fb_api.get_user_id(long_token)
42
+ if response.code == 200
43
+ @token = long_token
44
+ return JSON.parse(response.body)["id"]
45
+ end
46
+ #Lets try it with the short token now
47
+ response = fb_api.get_user_id(short_token)
48
+ if response.code == 200
49
+ @token = short_token
50
+ return JSON.parse(response.body)["id"]
51
+ end
52
+ return nil
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,100 @@
1
+ require_dependency "fbuser/api/v1/application_controller"
2
+ require 'authorization'
3
+
4
+ module Fbuser
5
+ class Api::V1::UsersController < Api::V1::ApplicationController
6
+ before_action :set_user, only: [:show, :update, :destroy]
7
+ before_action :index_authorize, only: [:index]
8
+ before_action :show_authorize, only: [:show]
9
+ before_action :create_authorize, only: [:create]
10
+ before_action :update_authorize, only: [:update]
11
+ before_action :destroy_authorize, only: [:destroy]
12
+
13
+ # GET /api/1/users
14
+ def index
15
+ @users = ::Fbuser::V1::User.all
16
+ render json: @users
17
+ end
18
+
19
+ # GET /api/1/users/1
20
+ def show
21
+ render json: @user
22
+ end
23
+
24
+ =begin
25
+ # POST /api/1/users
26
+ def create
27
+ @user = ::Fbuser::V1::User.new(user_params)
28
+ if @user.save
29
+ render json: @user
30
+ else
31
+ render :json => {errors: @user.errors.full_messages}, status: :unprocessable_entity
32
+ end
33
+ end
34
+ =end
35
+
36
+ # PATCH/PUT /api/1/users/1
37
+ def update
38
+ if @user.update(user_params)
39
+ render json: @user
40
+ else
41
+ render :json => {errors: @user.errors.full_messages}, status: :unprocessable_entity
42
+ end
43
+ end
44
+
45
+ # DELETE /api/1/users/1
46
+ def destroy
47
+ @user.destroy
48
+ render json: {}
49
+ end
50
+
51
+ private
52
+
53
+ # Use callbacks to share common setup or constraints between actions.
54
+
55
+ def set_user
56
+ @user = ::Fbuser::V1::User.find_by_id(params[:id])
57
+ if @user.nil?
58
+ render :json => {errors: "User was not found"}, status: :not_found
59
+ end
60
+ end
61
+
62
+ # Only allow a trusted parameter "white list" through.
63
+
64
+ def user_params
65
+ params.require(:user).permit(:username,:fb_user_id)
66
+ end
67
+
68
+ # Authorizations below here
69
+
70
+ def index_authorize
71
+ if !::Authorization::Fbuser::V1::User.index?(current_user)
72
+ render :json => {errors: "User is not authorized for this action"}, status: :forbidden
73
+ end
74
+ end
75
+
76
+ def show_authorize
77
+ if !::Authorization::Fbuser::V1::User.show?(@user,current_user)
78
+ render :json => {errors: "User is not authorized for this action"}, status: :forbidden
79
+ end
80
+ end
81
+
82
+ def create_authorize
83
+ if !::Authorization::Fbuser::V1::User.create?(user_params,current_user)
84
+ render :json => {errors: "User is not authorized for this action"}, status: :forbidden
85
+ end
86
+ end
87
+
88
+ def update_authorize
89
+ if !::Authorization::Fbuser::V1::User.update?(@user,user_params,current_user)
90
+ render :json => {errors: "User is not authorized for this action"}, status: :forbidden
91
+ end
92
+ end
93
+
94
+ def destroy_authorize
95
+ if !::Authorization::Fbuser::V1::User.destroy?(@user,current_user)
96
+ render :json => {errors: "User is not authorized for this action"}, status: :forbidden
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,4 @@
1
+ module Fbuser
2
+ class ApplicationController < ::ApplicationController
3
+ end
4
+ end
@@ -0,0 +1,50 @@
1
+ require 'httparty'
2
+
3
+ module Fbuser
4
+ class Facebook
5
+ include ::HTTParty
6
+ base_uri 'https://graph.facebook.com/v2.1'
7
+ #format :json
8
+ #debug_output $stdout
9
+
10
+ def initialize()
11
+ end
12
+
13
+ def get_long_token(short_token)
14
+ params = {
15
+ grant_type: "fb_exchange_token",
16
+ client_id: ENV["FB_APP_ID1"],
17
+ client_secret: ENV["FB_APP_SECRET1"],
18
+ fb_exchange_token: short_token
19
+ }
20
+ headers = { 'Content-Type' => 'application/json' }
21
+ response = self.class.get("/oauth/access_token", query: params, headers: headers)
22
+ if response.code == 200
23
+ return parse_token(response.body)
24
+ else
25
+ return ""
26
+ end
27
+ end
28
+
29
+ def get_user_id(token)
30
+ options = { query:{
31
+ fields: "id",
32
+ access_token: token
33
+ }
34
+ }
35
+ response = self.class.get("/me", options)
36
+ return response
37
+ end
38
+
39
+ private
40
+
41
+ def parse_token(response)
42
+ #access_token={access-token}&expires={seconds-til-expiration}
43
+ index = response.index('&')
44
+ start_index = "access_token=".size
45
+ length = index - start_index
46
+ return response.slice(start_index,length)
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,4 @@
1
+ module Fbuser
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Fbuser
2
+ module AuthenticationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Fbuser
2
+ class User < ActiveRecord::Base
3
+ end
4
+ end
@@ -0,0 +1,32 @@
1
+ require 'type_cartographer'
2
+
3
+ module Fbuser
4
+ class V1::User < ActiveRecord::Base
5
+ self.table_name = 'fbuser_users'
6
+
7
+ before_validation :sanitize_attributes
8
+ after_create :setup_user
9
+
10
+ has_many :tokens, :as => :imageable, :class_name => "::Arcadex::Token", dependent: :destroy
11
+
12
+ validates :fb_user_id, presence: true
13
+
14
+ def setup_user
15
+ create_token
16
+ return true
17
+ end
18
+
19
+ def create_token
20
+ self.tokens.create!
21
+ return true
22
+ end
23
+
24
+ def sanitize_attributes
25
+ if self.username.nil? || self.username.blank?
26
+ self.username = "username"
27
+ end
28
+ return true
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,41 @@
1
+ require 'authorization'
2
+
3
+ module Fbuser
4
+ class V1::UserSerializer < ActiveModel::Serializer
5
+ embed :ids, include: true
6
+
7
+ attributes :id
8
+ attributes :username
9
+ attributes :fb_user_id
10
+ attributes :created_at
11
+ attributes :updated_at
12
+ #has_one :bulletin
13
+ #has_many :posts
14
+
15
+ def include_id?
16
+ return ::Authorization::Fbuser::V1::User.include_id?(current_user,object,@options)
17
+ end
18
+
19
+ def include_username?
20
+ return ::Authorization::Fbuser::V1::User.include_created_at?(current_user,object,@options)
21
+ end
22
+
23
+ def include_fb_user_id?
24
+ return ::Authorization::Fbuser::V1::User.include_created_at?(current_user,object,@options)
25
+ end
26
+
27
+ def include_created_at?
28
+ return ::Authorization::Fbuser::V1::User.include_created_at?(current_user,object,@options)
29
+ end
30
+
31
+ def include_updated_at?
32
+ return ::Authorization::Fbuser::V1::User.include_updated_at?(current_user,object,@options)
33
+ end
34
+
35
+ def include_associations!
36
+ #include! :bulletin if ::Authorization::Fbuser::V1::User.include_bulletin?(current_user,object,@options)
37
+ #include! :posts if ::Authorization::Fbuser::V1::User.include_posts?(current_user,object,@options)
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Fbuser</title>
5
+ <%= stylesheet_link_tag "fbuser/application", media: "all" %>
6
+ <%= javascript_include_tag "fbuser/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,27 @@
1
+ Fbuser::Engine.routes.draw do
2
+
3
+ #root to: "controller_name#index"
4
+
5
+ scope 'api' do
6
+ scope '1' do
7
+ # /api/1/users
8
+ resources :users, except: [:new, :edit, :create], controller: 'api/v1/users'
9
+ # /api/1/authentication/login
10
+ post 'authentication/login', to: "api/v1/authentication#login"
11
+ # /api/1/authentication/logout
12
+ post 'authentication/logout', to: "api/v1/authentication#logout"
13
+ post 'users', to: 'api/v1/authentication#login'
14
+ #/api/1/controller_name
15
+ #resources :controller_name, controller: 'api/v1/controller_name'
16
+ #resources :controller_name2, controller: 'api/v1/controller_name2' do
17
+ #collection do
18
+ #/api/1/controller_name2/action
19
+ #post 'action', to: "api/v1/controller_name2#action"
20
+ #end
21
+ #end
22
+ end
23
+ end
24
+
25
+ #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-
26
+
27
+ end