passportist 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +27 -0
- data/Rakefile +1 -0
- data/app/controllers/passportist/application_controller.rb +4 -0
- data/app/controllers/passportist/sessions_controller.rb +10 -0
- data/app/controllers/passportist/users_controller.rb +48 -0
- data/config/routes.rb +7 -0
- data/lib/passportist/engine.rb +12 -0
- data/lib/passportist/helpers.rb +24 -0
- data/lib/passportist.rb +6 -0
- metadata +76 -0
data/README.markdown
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# Passportist
|
2
|
+
|
3
|
+
Add it to your Gemfile
|
4
|
+
|
5
|
+
gem 'passportist', github: 'evrone/passportist'
|
6
|
+
|
7
|
+
You should have those attributes in your `User` model: `uid`, `token`, `email`, `name`, and `nickname`. After that, add this line to your `user.rb` file:
|
8
|
+
|
9
|
+
attr_accessible :uid, :token, :email, :name, :nickname, as: :passportist
|
10
|
+
|
11
|
+
Please, use this `as: :passportist` for better security.
|
12
|
+
|
13
|
+
Add this to your `config/routes` file:
|
14
|
+
|
15
|
+
mount Passportist::Engine, at: '/passportist'
|
16
|
+
|
17
|
+
Then create an initializer in `config/initializers/passportist.rb`:
|
18
|
+
|
19
|
+
Passportist.access_token = 'YOUR VERY SECRET ACCESS TOKEN'
|
20
|
+
|
21
|
+
And that's it! You should create a new application at http://passport.evrone.ru now.
|
22
|
+
|
23
|
+
For authentication through Passport use something like this:
|
24
|
+
|
25
|
+
= link_to 'Sign in', 'http://passport.evrone.ru/authenticate'
|
26
|
+
|
27
|
+
You can use `button_to`, too: we can get both GET and POST for this route.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Passportist
|
2
|
+
class UsersController < ApplicationController
|
3
|
+
|
4
|
+
before_filter :restrict_access
|
5
|
+
before_filter :load_user, only: [:update, :destroy]
|
6
|
+
|
7
|
+
respond_to :json
|
8
|
+
|
9
|
+
def create
|
10
|
+
respond_with User.create(params[:user], as: :passportist), location: nil
|
11
|
+
end
|
12
|
+
|
13
|
+
def sync
|
14
|
+
params[:user].each do |user_data|
|
15
|
+
user = User.find_or_initialize_by_uid(user_data['uid'])
|
16
|
+
|
17
|
+
user.name = user_data['name']
|
18
|
+
user.nickname = user_data['nickname']
|
19
|
+
user.email = user_data['email']
|
20
|
+
user.token = user_data['token']
|
21
|
+
user.save!
|
22
|
+
end
|
23
|
+
|
24
|
+
render nothing: true
|
25
|
+
end
|
26
|
+
|
27
|
+
def update
|
28
|
+
respond_with @user.update_attributes(params[:user], as: :passportist)
|
29
|
+
end
|
30
|
+
|
31
|
+
def destroy
|
32
|
+
respond_with User.destroy(@user)
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def restrict_access
|
38
|
+
authenticate_or_request_with_http_token do |token, options|
|
39
|
+
token == Passportist.access_token
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def load_user
|
44
|
+
@user ||= User.find_by_token(params[:id])
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
data/config/routes.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module Passportist
|
2
|
+
module Helpers
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
helper_method :current_user, :logged_in?
|
7
|
+
end
|
8
|
+
|
9
|
+
def current_user
|
10
|
+
@current_user ||= User.find_by_token!(cookies[:auth_token]) if cookies[:auth_token]
|
11
|
+
rescue ActiveRecord::RecordNotFound
|
12
|
+
cookies.delete(:auth_token); nil
|
13
|
+
end
|
14
|
+
|
15
|
+
def logged_in?
|
16
|
+
!! current_user
|
17
|
+
end
|
18
|
+
|
19
|
+
def authenticate!
|
20
|
+
redirect_to welcome_url unless logged_in?
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
data/lib/passportist.rb
ADDED
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: passportist
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Andrey Ognevsky
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: ''
|
31
|
+
email:
|
32
|
+
- a.ognevsky@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- app/controllers/passportist/application_controller.rb
|
38
|
+
- app/controllers/passportist/sessions_controller.rb
|
39
|
+
- app/controllers/passportist/users_controller.rb
|
40
|
+
- config/routes.rb
|
41
|
+
- lib/passportist/engine.rb
|
42
|
+
- lib/passportist/helpers.rb
|
43
|
+
- lib/passportist.rb
|
44
|
+
- Rakefile
|
45
|
+
- README.markdown
|
46
|
+
homepage: ''
|
47
|
+
licenses: []
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
hash: -2457031029490591551
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
hash: -2457031029490591551
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.8.23
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: ''
|
76
|
+
test_files: []
|