pixnet-sso 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -15,7 +15,9 @@ Clients for PIXNET SSO
15
15
 
16
16
  gem install pixnet-sso
17
17
 
18
- ## CONFIG:
18
+ ## ENABLE PIXNET SSO:
19
+
20
+ ### CONFIG:
19
21
 
20
22
  Create a `config/initializers/pixnet-sso.rb` that looks like:
21
23
 
@@ -23,10 +25,28 @@ Create a `config/initializers/pixnet-sso.rb` that looks like:
23
25
  config.user_model = 'User'
24
26
  config.sso_key = ENV['SSO_KEY']
25
27
  config.sso_secret = ENV['SSO_SECRET']
28
+ # option: openid
29
+ config.openid_model = 'OpenidAssociate'
30
+ config.openid_enabled = true
26
31
  end
27
32
 
28
33
  And set your PIXNET SSO key pair to your ENV.
29
34
 
35
+ ### MODEL AND MIGRATIONS:
36
+
37
+ You should have models to store user/openid data.
38
+
39
+ ### LOAD SCIRPTS IN YOUR LAYOUT:
40
+
41
+ Add `<%= pixnet_sso_scripts %>` in your `layout/application.html.erb` to enable PIXNET SSO.
42
+
43
+ ### ADD CONTROLLER METHODS IN YOUR APPLCATION
44
+
45
+ Add code in your `app/controllers/application_controller`:
46
+
47
+ include Pixnet::SSO::ControllerMethods
48
+ include Pixnet::SSO::OpenidControllerMethods
49
+
30
50
  ## LICENSE:
31
51
 
32
52
  Apache License 2.0
@@ -8,7 +8,7 @@ class PixauthController < ApplicationController
8
8
 
9
9
  if info["info"]
10
10
  self.current_user = sso.get_user(info["msg"]["user_name"])
11
- #self.current_openid_user = get_openid_user(info)
11
+ self.current_openid_user = sso.get_openid_user(info) if Pixnet::SSO::Config.openid_enabled
12
12
 
13
13
  redirect_to done_site
14
14
  else
@@ -0,0 +1,52 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module PixnetSso
5
+ class InstallGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+
10
+ # Implement the required interface for Rails::Generators::Migration.
11
+ # taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
12
+ def self.next_migration_number(dirname) #:nodoc:
13
+ if ActiveRecord::Base.timestamped_migrations
14
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
15
+ else
16
+ "%.3d" % (current_migration_number(dirname) + 1)
17
+ end
18
+ end
19
+
20
+ def create_migration_file
21
+ template 'user.rb', "app/models/#{user_model.singularize.underscore}.rb"
22
+ migration_template 'migration.rb', "db/migrate/create_#{user_model.tableize}.rb"
23
+
24
+
25
+ if openid_enabled?
26
+ template 'openid_associate.rb', "app/models/#{openid_model.singularize.underscore}.rb"
27
+ migration_template 'migration_openid.rb', "db/migrate/create_#{openid_model.tableize}.rb"
28
+ end
29
+
30
+ template 'config.rb', 'config/initializers/pixnet-sso.rb'
31
+ end
32
+
33
+ def user_model
34
+ @user_model ||= ask('What is your user model name?').classify
35
+ end
36
+
37
+ def openid_model
38
+ return '' unless openid_enabled?
39
+ @openid_model ||= ask('What is your openid model name?').classify
40
+ end
41
+
42
+ def openid_enabled
43
+ @openid_enabled ||= ask("Would you want to enable OpenID ?(y/N)")
44
+ end
45
+
46
+ def openid_enabled?
47
+ openid_enabled.downcase == 'y'
48
+ end
49
+ end
50
+ end
51
+
52
+
@@ -0,0 +1,9 @@
1
+ Pixnet::SSO.config do |config|
2
+ config.user_model = '<%= user_model.classify %>'
3
+ config.sso_key = ENV['SSO_KEY']
4
+ config.sso_secret = ENV['SSO_SECRET']
5
+ <% if openid_enabled? -%>
6
+ config.openid_model = '<%= openid_model.classify %>'
7
+ config.openid_enabled = <%= openid_enabled? ? 'true' : 'false' %>
8
+ <% end -%>
9
+ end
@@ -0,0 +1,15 @@
1
+ class Create<%= user_model %>s < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :<%= user_model.tableize %> do |t|
4
+ t.string :login, :limit => 32
5
+ t.timestamps
6
+ end
7
+
8
+ add_index :<%= user_model.tableize %>, [:login], :unique => true
9
+ end
10
+
11
+ def self.down
12
+ drop_table :<%= user_model.tableize %>
13
+ end
14
+ end
15
+
@@ -0,0 +1,19 @@
1
+ class Create<%= openid_model %>s < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :<%= openid_model.tableize %> do |t|
4
+ t.string "openid"
5
+ t.string "provider", :limit => 32
6
+ t.text "extra"
7
+ t.datetime "created_at"
8
+ t.datetime "updated_at"
9
+ end
10
+
11
+ add_index :<%= openid_model.tableize %>, ["openid"], :unique => true
12
+ add_index :<%= openid_model.tableize %>, ["provider"]
13
+ end
14
+
15
+ def self.down
16
+ drop_table :<%= openid_model.tableize %>
17
+ end
18
+ end
19
+
@@ -0,0 +1,32 @@
1
+ class <%= openid_model %> < ActiveRecord::Base
2
+
3
+ def email
4
+ extra_data['value.email']
5
+ end
6
+
7
+ def fullname
8
+ extra_data['value.fullname'] or "#{extra_data['value.firstname']} #{extra_data['value.lastname']}"
9
+ end
10
+
11
+ def avatar
12
+ extra_data['value.avatar'] or "http://s.pixfs.net/f.pixnet.net/comment/images/avatar-#{provider.downcase}.jpg"
13
+ end
14
+
15
+ def nick
16
+ extra_data['value.nickname'] or fullname
17
+ end
18
+
19
+ def link
20
+ case provider
21
+ when "facebook"
22
+ return extra_data['value.facebook']
23
+ else
24
+ return false
25
+ end
26
+ return false
27
+ end
28
+
29
+ def extra_data
30
+ return JSON.parse(extra)
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ class <%= user_model %> < ActiveRecord::Base
2
+
3
+ end
@@ -1,5 +1,6 @@
1
1
  # encoding: utf-8
2
2
  require 'openssl'
3
+ require 'open-uri'
3
4
 
4
5
  module Pixnet
5
6
  module SSO
@@ -27,6 +28,17 @@ module Pixnet
27
28
  end
28
29
  end
29
30
 
31
+ def get_openid_user(info)
32
+ return false if info['msg']['openid'].blank?
33
+ openid_user = Pixnet::SSO::Config.openid_klass.find_or_initialize_by_openid(info['msg']['openid'])
34
+ if openid_user.new_record?
35
+ openid_user.provider = info['msg']['openid_type']
36
+ openid_user.extra = info['msg']['openid_data'].to_json
37
+ openid_user.save
38
+ end
39
+ return openid_user
40
+ end
41
+
30
42
  def show_error
31
43
  return @error_message
32
44
  end
@@ -5,6 +5,12 @@ module Pixnet
5
5
  mattr_accessor :user_model
6
6
  @@user_model = "User"
7
7
 
8
+ mattr_accessor :openid_model
9
+ @@openid_model = "OpenidAssociate"
10
+
11
+ mattr_accessor :openid_enabled
12
+ @@openid_enabled = false
13
+
8
14
  # SSO Key
9
15
  mattr_accessor :sso_key
10
16
 
@@ -14,6 +20,10 @@ module Pixnet
14
20
  def self.user_klass
15
21
  user_model.to_s.constantize
16
22
  end
23
+
24
+ def self.openid_klass
25
+ openid_model.to_s.constantize
26
+ end
17
27
  end
18
28
  end
19
29
  end
@@ -15,7 +15,7 @@ module Pixnet
15
15
  end
16
16
 
17
17
  def login_from_session
18
- self.current_user = User.find(session[:user_id]) if session[:user_id]
18
+ self.current_user = Pixnet::SSO::Config.user_klass.find(session[:user_id]) if session[:user_id]
19
19
  end
20
20
 
21
21
  def login_required
@@ -27,7 +27,6 @@ module Pixnet
27
27
  end
28
28
  end
29
29
 
30
-
31
30
  def self.included(base)
32
31
  base.helper_method :logged_in?
33
32
  base.helper_method :current_user
@@ -1,4 +1,7 @@
1
1
  # encoding: utf-8
2
+
3
+ require 'uuid'
4
+
2
5
  module Pixnet
3
6
  module SSO
4
7
  module Helper
@@ -7,6 +10,10 @@ module Pixnet
7
10
  unique = Zlib.crc32(UUID.generate).to_s
8
11
  user_name = current_user.blank? ? "" : current_user.login
9
12
  login_name = "#{user_name}.pixnet.net#{unique}#{now}"
13
+
14
+ if Pixnet::SSO::Config.openid_enabled and current_openid_user
15
+ login_name = "#{login_name}#{current_openid_user.openid}"
16
+ end
10
17
  html = <<MSG
11
18
  <script type="text/javascript">
12
19
  <!--
@@ -0,0 +1,22 @@
1
+ module Pixnet
2
+ module SSO
3
+ module OpenidControllerMethods
4
+ def current_openid_user=(openid_user)
5
+ session[:openid_user_id] = openid_user ? openid_user.id : nil
6
+ @current_openid_user = openid_user || false
7
+ end
8
+
9
+ def current_openid_user
10
+ @current_openid_user ||= openid_login_from_session unless @current_openid_user == false
11
+ end
12
+
13
+ def openid_login_from_session
14
+ self.current_openid_user = Pixnet::SSO::Config.openid_klass.find(session[:openid_user_id]) if session[:openid_user_id]
15
+ end
16
+
17
+ def self.included(base)
18
+ base.helper_method :current_openid_user
19
+ end
20
+ end
21
+ end
22
+ end
data/lib/pixnet-sso.rb CHANGED
@@ -9,6 +9,7 @@ module Pixnet
9
9
  module SSO
10
10
 
11
11
  autoload :ControllerMethods, 'pixnet-sso/controller_methods'
12
+ autoload :OpenidControllerMethods, 'pixnet-sso/openid_controller_methods'
12
13
 
13
14
  def self.config
14
15
  yield Pixnet::SSO::Config
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pixnet-sso
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Manic Chuang
@@ -73,13 +73,20 @@ extra_rdoc_files: []
73
73
  files:
74
74
  - app/controllers/pixauth_controller.rb
75
75
  - config/routes.rb
76
- - lib/tasks/pixnet-sso_tasks.rake
77
- - lib/pixnet-sso.rb
78
- - lib/pixnet-sso/helper.rb
79
- - lib/pixnet-sso/railtie.rb
80
76
  - lib/pixnet-sso/controller_methods.rb
81
- - lib/pixnet-sso/app.rb
77
+ - lib/pixnet-sso/railtie.rb
78
+ - lib/pixnet-sso/openid_controller_methods.rb
82
79
  - lib/pixnet-sso/config.rb
80
+ - lib/pixnet-sso/helper.rb
81
+ - lib/pixnet-sso/app.rb
82
+ - lib/pixnet-sso.rb
83
+ - lib/generators/pixnet_sso/install_generator.rb
84
+ - lib/generators/pixnet_sso/templates/config.rb
85
+ - lib/generators/pixnet_sso/templates/openid_associate.rb
86
+ - lib/generators/pixnet_sso/templates/migration_openid.rb
87
+ - lib/generators/pixnet_sso/templates/user.rb
88
+ - lib/generators/pixnet_sso/templates/migration.rb
89
+ - lib/tasks/pixnet-sso_tasks.rake
83
90
  - MIT-LICENSE
84
91
  - Rakefile
85
92
  - README.md