livedoorauth 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ +++ 0.0.1 2007-04-21
2
+
3
+ + 1 major enhancement:
4
+ + Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,9 @@
1
+ Manifest.txt
2
+ README.txt
3
+ History.txt
4
+ Rakefile
5
+ lib/livedoorauth.rb
6
+ lib/livedoor/api/auth.rb
7
+ lib/livedoor/api/version.rb
8
+ test/test_auth.rb
9
+ test/template.parameters.rb
data/README.txt ADDED
@@ -0,0 +1,3 @@
1
+ README for livedoorauth
2
+ ================
3
+ read test/test_auth.rb
data/Rakefile ADDED
@@ -0,0 +1,82 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/clean'
4
+ require 'rake/testtask'
5
+ require 'rake/packagetask'
6
+ require 'rake/gempackagetask'
7
+ require 'rake/rdoctask'
8
+ require 'rake/contrib/rubyforgepublisher'
9
+ require 'fileutils'
10
+ require 'hoe'
11
+ include FileUtils
12
+ require File.join(File.dirname(__FILE__), 'lib', 'livedoor', 'api', 'version')
13
+
14
+ AUTHOR = 'zorio' # can also be an array of Authors
15
+ EMAIL = "zoriorz@gmail.com"
16
+ DESCRIPTION = "livedoor auth api"
17
+ GEM_NAME = 'livedoorauth' # what ppl will type to install your gem
18
+ RUBYFORGE_PROJECT = 'livedoorauth' # The unix name for your project
19
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
20
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
21
+
22
+ NAME = "livedoorauth"
23
+ REV = nil # UNCOMMENT IF REQUIRED: File.read(".svn/entries")[/committed-rev="(d+)"/, 1] rescue nil
24
+ VERS = Livedoor::API::Auth::VERSION::STRING + (REV ? ".#{REV}" : "")
25
+ CLEAN.include ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store']
26
+ RDOC_OPTS = ['--quiet', '--title', 'livedoorauth documentation',
27
+ "--opname", "index.html",
28
+ "--line-numbers",
29
+ "--main", "README",
30
+ "--inline-source"]
31
+
32
+ class Hoe
33
+ def extra_deps
34
+ @extra_deps.reject { |x| Array(x).first == 'hoe' }
35
+ end
36
+ end
37
+
38
+ # Generate all the Rake tasks
39
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
40
+ hoe = Hoe.new(GEM_NAME, VERS) do |p|
41
+ p.author = AUTHOR
42
+ p.description = DESCRIPTION
43
+ p.email = EMAIL
44
+ p.summary = DESCRIPTION
45
+ p.url = HOMEPATH
46
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
47
+ p.test_globs = ["test/**/test_*.rb"]
48
+ p.clean_globs = CLEAN #An array of file patterns to delete on clean.
49
+
50
+ # == Optional
51
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
52
+ #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
53
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
54
+ end
55
+
56
+
57
+ desc 'Upload website files to rubyforge'
58
+ task :website_upload do
59
+ config = YAML.load(File.read(File.expand_path("~/.rubyforge/user-config.yml")))
60
+ host = "#{config["username"]}@rubyforge.org"
61
+ remote_dir = "/var/www/gforge-projects/#{RUBYFORGE_PROJECT}/"
62
+ # remote_dir = "/var/www/gforge-projects/#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
63
+ local_dir = 'website'
64
+ sh %{rsync -av #{local_dir}/ #{host}:#{remote_dir}}
65
+ end
66
+
67
+ desc 'Generate and upload website files'
68
+ task :website => [:website_generate, :website_upload]
69
+
70
+ desc 'Release the website and new gem version'
71
+ task :deploy => [:check_version, :website, :release]
72
+
73
+ task :check_version do
74
+ unless ENV['VERSION']
75
+ puts 'Must pass a VERSION=x.y.z release version'
76
+ exit
77
+ end
78
+ unless ENV['VERSION'] == VERS
79
+ puts "Please update your version.rb to match the release version, currently #{VERS}"
80
+ exit
81
+ end
82
+ end
@@ -0,0 +1,108 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'cgi'
4
+
5
+ begin
6
+ require 'openssl'
7
+ rescue LoadError
8
+ end
9
+
10
+ begin
11
+ require 'json'
12
+ rescue LoadError
13
+ require 'rubygems'
14
+ require 'json'
15
+ end
16
+
17
+ module Livedoor
18
+ module API
19
+ class AuthError < RuntimeError;end
20
+ class Auth
21
+ BASE_URI = 'http://auth.livedoor.com/'
22
+ PATH = {
23
+ :auth => '/login/',
24
+ :rpc_auth => '/rpc/auth'
25
+ }.freeze
26
+ API_VERSION = '1.0'
27
+ TIMEOUT = 60 * 10
28
+
29
+ attr_accessor :app_key, :secret
30
+
31
+ def initialize(options = {})
32
+ @app_key = options[:app_key]
33
+ @secret = options[:secret]
34
+ end
35
+
36
+ def uri_to_login(request = {})
37
+ uri = URI.parse BASE_URI
38
+ uri.path = PATH[:auth]
39
+ request[:perms] = 'userhash' unless request.key?(:perms)
40
+ uri.query = query_with_api_sig(request)
41
+ uri
42
+ end
43
+
44
+ def validate_response(query)
45
+ param = {}
46
+ pr = CGI.parse(query)
47
+ pr.each do |k,v|
48
+ param[k.to_sym] = v.first
49
+ end
50
+ if param[:sig] == sig(param)
51
+ raise 'LOCAL TIMEOUT' if param[:t].to_i + TIMEOUT < Time.now.to_i
52
+ else
53
+ raise 'INVALID SIG'
54
+ end
55
+ {:userdata => param[:userdata], :userhash => param[:userhash], :token => param[:token]}
56
+ end
57
+
58
+ def get_livedoor_id(param)
59
+ uri = uri_to_auth_rpc(param)
60
+ result = nil
61
+ Net::HTTP.start(uri.host) do |http|
62
+ result = JSON.parse(
63
+ http.post(uri.path, uri.query,
64
+ {"Content-type" => "application/x-www-form-urlencoded"}).body)
65
+ end
66
+ if result['error'].to_i == 0
67
+ result['user']['livedoor_id']
68
+ else
69
+ raise AuthError.new(result['message'])
70
+ end
71
+ end
72
+
73
+
74
+ private
75
+ def sig(hash)
76
+ OpenSSL::HMAC::hexdigest(OpenSSL::Digest::SHA1.new, @secret,
77
+ hash.reject{|k,v| k == :sig}.to_a.sort_by {|i| i.first.to_s}.join)
78
+ end
79
+
80
+ def uri_to_authcheck(cert)
81
+ uri = URI.parse BASE_URI
82
+ uri.path = PATH[:json]
83
+ uri.query = query_with_api_sig(:cert => cert)
84
+ uri
85
+ end
86
+
87
+ def query_with_api_sig(request = {})
88
+ query = request.update(:app_key => @app_key)
89
+ query[:t] = Time.now.to_i
90
+ query[:v] = API_VERSION
91
+ query[:sig] = sig(query)
92
+ query.map {|i| i.join '=' }.join('&')
93
+ end
94
+
95
+ def uri_to_auth_rpc(param)
96
+ query = {
97
+ :format => 'json',
98
+ :token => param[:token]
99
+ }
100
+ uri = URI.parse BASE_URI
101
+ uri.path = PATH[:rpc_auth]
102
+ uri.query = query_with_api_sig(query)
103
+ uri
104
+ end
105
+
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,13 @@
1
+ module Livedoor #:nodoc:
2
+ module API
3
+ class Auth
4
+ module VERSION #:nodoc:
5
+ MAJOR = 0
6
+ MINOR = 0
7
+ TINY = 1
8
+
9
+ STRING = [MAJOR, MINOR, TINY].join('.')
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1 @@
1
+ Dir[File.join(File.dirname(__FILE__), 'livedoor/**/*.rb')].sort.each { |lib| require lib }
@@ -0,0 +1,7 @@
1
+ # This file is a template for parameters.rb
2
+ # Copy this file and change variables below.
3
+ APP_KEY =
4
+ SECRET =
5
+ #your livedoor id for testing
6
+ ACCOUNT =
7
+ PASSWORD =
data/test/test_auth.rb ADDED
@@ -0,0 +1,41 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/livedoorauth'
3
+
4
+ require "parameters"
5
+ require "rubygems"
6
+ require "mechanize"
7
+ require "logger"
8
+
9
+ class TestLivedoorAuth < Test::Unit::TestCase
10
+
11
+ def setup
12
+ end
13
+
14
+ def test_truth
15
+ assert true
16
+ end
17
+
18
+ def test_get_login
19
+ agent = WWW::Mechanize.new do |a| a.log = Logger.new("livedoor.log") end
20
+
21
+ #login to livedoor
22
+ page = agent.get("https://member.livedoor.com/login/")
23
+ form = page.forms[0]
24
+ form["livedoor_id"] = ACCOUNT
25
+ form["password"] = PASSWORD
26
+ page = agent.submit(form)
27
+
28
+ #get auth info
29
+ agent.redirect_ok = false
30
+ auth = Livedoor::API::Auth.new(:app_key => APPKEY, :secret => SECRET)
31
+ uri = auth.uri_to_login(:perms=>'id')
32
+ page = agent.get(uri.to_s)
33
+ uri = URI.parse(page.response['location'])
34
+ user = auth.validate_response(uri.query)
35
+ assert_not_nil(user[:userhash])
36
+
37
+ #get livedoor id
38
+ id = auth.get_livedoor_id(user)
39
+ assert_equal(ACCOUNT, id)
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: livedoorauth
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2007-04-21 00:00:00 +09:00
8
+ summary: livedoor auth api
9
+ require_paths:
10
+ - lib
11
+ email: zoriorz@gmail.com
12
+ homepage: http://livedoorauth.rubyforge.org
13
+ rubyforge_project: livedoorauth
14
+ description: livedoor auth api
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - zorio
31
+ files:
32
+ - Manifest.txt
33
+ - README.txt
34
+ - History.txt
35
+ - Rakefile
36
+ - lib/livedoorauth.rb
37
+ - lib/livedoor/api/auth.rb
38
+ - lib/livedoor/api/version.rb
39
+ - test/test_auth.rb
40
+ - test/template.parameters.rb
41
+ test_files:
42
+ - test/test_auth.rb
43
+ rdoc_options: []
44
+
45
+ extra_rdoc_files: []
46
+
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ requirements: []
52
+
53
+ dependencies: []
54
+