pixnet-sso 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 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/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # pixnet-sso
2
+
3
+ by Manic <http://tech.manic.tw>
4
+
5
+ ## LINKS:
6
+
7
+ * [github](https://github.com/manic/pixnet-sso)
8
+ * [rubygems](http://rubygems.org/gems/pixnet-sso)
9
+
10
+ ## DESCRIPTION:
11
+
12
+ Clients for PIXNET SSO
13
+
14
+ ## INSTALLATION:
15
+
16
+ gem install pixnet-sso
17
+
18
+ ## CONFIG:
19
+
20
+ Create a `config/initializers/pixnet-sso.rb` that looks like:
21
+
22
+ PIXNET::SSO.config do |config|
23
+ config.user_model = 'User'
24
+ config.sso_key = ENV['SSO_KEY']
25
+ config.sso_secret = ENV['SSO_SECRET']
26
+ end
27
+
28
+ And set your PIXNET SSO key pair to your ENV.
29
+
30
+ ## LICENSE:
31
+
32
+ Apache License 2.0
33
+
34
+ Copyright (c) 2011, PIXNET
35
+
36
+ Licensed under the Apache License, Version 2.0 (the "License");
37
+ you may not use this file except in compliance with the License.
38
+ You may obtain a copy of the License at
39
+
40
+ <http://www.apache.org/licenses/LICENSE-2.0>
41
+
42
+ Unless required by applicable law or agreed to in writing, software
43
+ distributed under the License is distributed on an "AS IS" BASIS,
44
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
45
+ See the License for the specific language governing permissions and
46
+ limitations under the License.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'PixnetSso'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
@@ -0,0 +1,20 @@
1
+ class PixauthController < ApplicationController
2
+ def index
3
+ done_site = params[:done]
4
+ redirect_site = "http://www.pixnet.net/?done=#{CGI::escape(done_site)}"
5
+
6
+ sso = Pixnet::SSO::App.new
7
+ info = sso.decode_process(params[:token])
8
+
9
+ if info["info"]
10
+ self.current_user = sso.get_user(info["msg"]["user_name"])
11
+ #self.current_openid_user = get_openid_user(info)
12
+
13
+ redirect_to done_site
14
+ else
15
+ #Rails.logger.info(info["msg"])
16
+ redirect_to redirect_site
17
+ end
18
+ end
19
+
20
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ match "/api/checklogin" => "pixauth#index", :as => :api_checklogin
3
+ end
data/lib/pixnet-sso.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'rails'
2
+
3
+ require 'pixnet-sso/config'
4
+ require 'pixnet-sso/helper'
5
+ require 'pixnet-sso/railtie' if defined?(Rails)
6
+ require 'pixnet-sso/app'
7
+
8
+ module Pixnet
9
+ module SSO
10
+
11
+ autoload :ControllerMethods, 'pixnet-sso/controller_methods'
12
+
13
+ def self.config
14
+ yield Pixnet::SSO::Config
15
+ end
16
+
17
+ class Engine < ::Rails::Engine
18
+ config.before_eager_load { |app| app.reload_routes! }
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+ require 'openssl'
3
+
4
+ module Pixnet
5
+ module SSO
6
+ class App
7
+ def initialize
8
+ @error_message = ""
9
+ end
10
+
11
+ def decode_process(token)
12
+ signature = crate_signature(token)
13
+ message = parse_json_request(token, signature)
14
+ return check_status(message)
15
+ end
16
+
17
+ def get_user(user_name)
18
+ user = Pixnet::SSO::Config.user_klass.find_or_initialize_by_login(user_name)
19
+ if user_name.blank?
20
+ return false
21
+ else
22
+ if user.new_record?
23
+ user.save(false)
24
+ end
25
+
26
+ return user
27
+ end
28
+ end
29
+
30
+ def show_error
31
+ return @error_message
32
+ end
33
+
34
+ protected
35
+
36
+ def crate_signature(token)
37
+ digest = OpenSSL::Digest::Digest.new('sha256')
38
+ return OpenSSL::HMAC.hexdigest(digest, Pixnet::SSO::Config.sso_secret, "#{Pixnet::SSO::Config.sso_key}#{token}").to_s
39
+ end
40
+
41
+ def parse_json_request(token, signature)
42
+ message_url = "https://api.pixnet.cc/api/getssodata.php?key=#{CGI::escape(Pixnet::SSO::Config.sso_key)}&token=#{CGI::escape(token)}&sig=#{CGI::escape(signature)}"
43
+ json_object = open(message_url).read
44
+ message = JSON::parse(json_object)
45
+ return message
46
+ end
47
+
48
+ def check_status(message)
49
+ if message["code"] == 200
50
+ return {"info" => true, "msg" => message["data"]}
51
+ elsif message["code"] == 403
52
+ @error_message = message["data"]
53
+ return {"info" => false, "msg" => message["data"]}
54
+ end
55
+ end
56
+
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,19 @@
1
+ module Pixnet
2
+ module SSO
3
+ module Config
4
+ # Name of the User class
5
+ mattr_accessor :user_model
6
+ @@user_model = "User"
7
+
8
+ # SSO Key
9
+ mattr_accessor :sso_key
10
+
11
+ # SSO Secret
12
+ mattr_accessor :sso_secret
13
+
14
+ def self.user_klass
15
+ user_model.to_s.constantize
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,39 @@
1
+ module Pixnet
2
+ module SSO
3
+ module ControllerMethods
4
+ def logged_in?
5
+ !!current_user
6
+ end
7
+
8
+ def current_user
9
+ @current_user ||= login_from_session unless @current_user == false
10
+ end
11
+
12
+ def current_user=(new_user)
13
+ session[:user_id] = new_user ? new_user.id : nil
14
+ @current_user = new_user || false
15
+ end
16
+
17
+ def login_from_session
18
+ self.current_user = User.find(session[:user_id]) if session[:user_id]
19
+ end
20
+
21
+ def login_required
22
+ if logged_in?
23
+ return true
24
+ else
25
+ redirect_to root_path
26
+ return false
27
+ end
28
+ end
29
+
30
+
31
+ def self.included(base)
32
+ base.helper_method :logged_in?
33
+ base.helper_method :current_user
34
+ base.helper_method :login_required
35
+ base.helper_method :redirect_back_or_default
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+ module Pixnet
3
+ module SSO
4
+ module Helper
5
+ def pixnet_sso_scripts
6
+ now = Time.now.to_i.to_s
7
+ unique = Zlib.crc32(UUID.generate).to_s
8
+ user_name = current_user.blank? ? "" : current_user.login
9
+ login_name = "#{user_name}.pixnet.net#{unique}#{now}"
10
+ html = <<MSG
11
+ <script type="text/javascript">
12
+ <!--
13
+ var pix = pix || {};
14
+ pix.apisite = 'api.pixnet.cc';
15
+ pix.login_name = "#{Digest::MD5.hexdigest(login_name)}";
16
+ pix.server_name = "#{URI::parse(request.host)}";
17
+ pix.checklogin_version = 2;
18
+ pix.checklogin_callback = '#{api_checklogin_path}';
19
+ -->
20
+ </script>
21
+ <script src="//api.pixnet.cc/api/checklogin.php?js=1&amp;unique=#{unique}&amp;timestamp=#{now}&amp;type=2" type="text/javascript"></script>
22
+ <script src="http://s.pixfs.net/js/pixnet/checklogin.js?v=20110519" type="text/javascript"></script>
23
+ MSG
24
+ return html.html_safe
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,11 @@
1
+ require "pixnet-sso"
2
+
3
+ module Pixnet
4
+ module SSO
5
+ class Railtie < Rails::Railtie
6
+ initializer "pixnet-sso.view_helpers" do
7
+ ActionView::Base.send :include, Pixnet::SSO::Helper
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :pixnet-sso do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pixnet-sso
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Manic Chuang
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-11-09 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ version_requirements: &id001 !ruby/object:Gem::Requirement
22
+ none: false
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ hash: 7
27
+ segments:
28
+ - 3
29
+ - 0
30
+ - 0
31
+ version: 3.0.0
32
+ requirement: *id001
33
+ prerelease: false
34
+ type: :runtime
35
+ name: rails
36
+ - !ruby/object:Gem::Dependency
37
+ version_requirements: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ requirement: *id002
47
+ prerelease: false
48
+ type: :runtime
49
+ name: uuid
50
+ - !ruby/object:Gem::Dependency
51
+ version_requirements: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ requirement: *id003
61
+ prerelease: false
62
+ type: :runtime
63
+ name: json
64
+ description: Client for PIXNET SSO
65
+ email:
66
+ - manic@pixnet.tw
67
+ executables: []
68
+
69
+ extensions: []
70
+
71
+ extra_rdoc_files: []
72
+
73
+ files:
74
+ - app/controllers/pixauth_controller.rb
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
+ - lib/pixnet-sso/controller_methods.rb
81
+ - lib/pixnet-sso/app.rb
82
+ - lib/pixnet-sso/config.rb
83
+ - MIT-LICENSE
84
+ - Rakefile
85
+ - README.md
86
+ homepage: https://github.com/manic/pixnet-sso
87
+ licenses: []
88
+
89
+ post_install_message:
90
+ rdoc_options: []
91
+
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ hash: 3
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ hash: 3
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ requirements: []
113
+
114
+ rubyforge_project:
115
+ rubygems_version: 1.8.10
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: Client for PIXNET SSO
119
+ test_files: []
120
+