hatenaapiauth 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,29 @@
1
+ = Hatena::API::Auth -- Hatena Authentication API
2
+
3
+ == SYNOPSIS
4
+
5
+ require 'rubygems'
6
+ require 'hatena/api/auth'
7
+ hatena_auth = Hatena::API::Auth.new(:api_key => YOUR_API_KEY, :secret => YOUR_SECRET_KEY)
8
+
9
+ # login_uri generate
10
+ hatena_auth.uri_to_login
11
+ #=> http://auth.hatena.ne.jp/auth?api_sig=09f68bcc0127f0eafebae1c6370c2bf0&api_key=d93fffa627f03c2c641353110e4d1316
12
+
13
+ # login check
14
+ hatena_auth.login(cert)
15
+
16
+ == NOTES
17
+ see examples/example.cgi.
18
+
19
+ == Installation
20
+
21
+ # gem install hatenaapiauth
22
+
23
+ == License
24
+
25
+ Hatena::API::Auth is released under the MIT license.
26
+
27
+ == Author
28
+
29
+ secondlife@no.spam@hatena.ne.jp
@@ -0,0 +1,83 @@
1
+ require 'rake/gempackagetask'
2
+
3
+ require 'rbconfig'
4
+ include Config
5
+
6
+ PKG_NAME = 'hatenaapiauth'
7
+ PKG_VERSION = File.read('VERSION').chomp
8
+ PKG_FILES = FileList["**/*"].exclude(".svn").exclude("pkg")
9
+
10
+ desc "Installing library"
11
+ task :install do
12
+ ruby 'install.rb'
13
+ end
14
+
15
+ desc "Testing library"
16
+ task :test do
17
+ ruby 'test/method_test.rb'
18
+ end
19
+
20
+ desc "Removing generated files"
21
+ task :clean do
22
+ rm_rf 'doc'
23
+ end
24
+
25
+ # Create RDOC documentation.
26
+ task :doc do
27
+ sh 'rdoc -d -S -o doc README lib/hatena/api/auth.rb'
28
+ end
29
+
30
+ spec = Gem::Specification.new do |s|
31
+ #### Basic information.
32
+
33
+ s.name = 'hatenaapiauth'
34
+ s.version = PKG_VERSION
35
+ s.summary = "Hatena Authentication API."
36
+ s.description = ""
37
+
38
+ #### Dependencies and requirements.
39
+
40
+ s.add_dependency('json', '>= 0.4.1')
41
+ s.requirements << "json"
42
+
43
+ s.files = PKG_FILES
44
+
45
+ #### C code extensions.
46
+
47
+ #s.extensions << "ext/extconf.rb"
48
+
49
+ #### Load-time details: library and application (you will need one or both).
50
+
51
+ s.require_path = 'lib' # Use these for libraries.
52
+ s.autorequire = 'hatena/api/auth'
53
+
54
+ # s.bindir = "bin" # Use these for applications.
55
+ # s.executables = ["hatenaapiauth.rb"]
56
+ # s.default_executable = "hatenaapiauth.rb"
57
+
58
+ #### Documentation and testing.
59
+
60
+ s.has_rdoc = true
61
+ #s.extra_rdoc_files = rd.rdoc_files.reject { |fn| fn =~ /\.rb$/ }.to_a
62
+ s.extra_rdoc_files = 'README'
63
+ s.rdoc_options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object'
64
+ #s.rdoc_options <<
65
+ # '--title' << 'Rake -- Ruby Make' <<
66
+ # '--main' << 'README' <<
67
+ # '--line-numbers'
68
+ s.test_files << 'test/method_test.rb'
69
+
70
+ #### Author and project details.
71
+
72
+ s.author = "Yuichi Tateno"
73
+ s.email = "secondlife@no.spam@hatena.ne.jp"
74
+ s.homepage = "http://hatenaapiauth.rubyforge.net"
75
+ s.rubyforge_project = "hatenaapiauth"
76
+ end
77
+
78
+ Rake::GemPackageTask.new(spec) do |pkg|
79
+ pkg.need_tar = true
80
+ pkg.package_files += PKG_FILES
81
+ end
82
+
83
+ task :release => [ :clean, :package ]
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'hatena/api/auth'
5
+ require 'erb'
6
+ require 'cgi'
7
+
8
+ API_KEY = 'your api key'
9
+ SECRET = 'your secret key'
10
+
11
+ cgi = CGI.new
12
+ hatena_auth = Hatena::API::Auth.new(:api_key => API_KEY, :secret => SECRET)
13
+
14
+ if cgi.has_key?('cert')
15
+ begin
16
+ user = hatena_auth.login(cgi['cert'])
17
+ message = "Welcome #{ERB::Util.h user['name']}!"
18
+ rescue Hatena::API::AuthError => e
19
+ message = "Authorization Failed"
20
+ end
21
+ else
22
+ login_uri = hatena_auth.uri_to_login
23
+ end
24
+ print cgi.header, ERB.new(DATA.read).result
25
+
26
+ __END__
27
+ <html>
28
+ <body>
29
+ <h1>Hatena API Auth Sample</h1>
30
+ <p>
31
+ <% if message %>
32
+ <%= message %>
33
+ <% else %>
34
+ <a href="<%= ERB::Util.h login_uri %>">login</a>
35
+ <% end %>
36
+ </p>
37
+ </body>
38
+ </html>
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rbconfig'
4
+ include Config
5
+ require 'fileutils'
6
+ include FileUtils::Verbose
7
+
8
+ destdir = "#{ENV["DESTDIR"]}"
9
+ libdir = CONFIG["sitelibdir"]
10
+ dest = destdir + File.join(libdir, 'hatena/api')
11
+ mkdir_p dest
12
+ install('lib/hatena/api/auth.rb', dest)
@@ -0,0 +1,72 @@
1
+ require 'open-uri'
2
+ require 'uri'
3
+
4
+ begin
5
+ require 'md5'
6
+ rescue LoadError
7
+ end
8
+
9
+ begin
10
+ require 'json'
11
+ rescue LoadError
12
+ require 'rubygems'
13
+ require 'json'
14
+ end
15
+
16
+ module Hatena
17
+ module API
18
+ class AuthError < RuntimeError;end
19
+ class Auth
20
+ BASE_URI = 'http://auth.hatena.ne.jp/'
21
+ PATH = {
22
+ :auth => '/auth',
23
+ :json => '/api/auth.json'
24
+ }.freeze
25
+ VERSION = '0.1.0'
26
+
27
+ attr_accessor :api_key, :secret
28
+ def initialize(options = {})
29
+ @api_key = options[:api_key]
30
+ @secret = options[:secret]
31
+ end
32
+
33
+ def uri_to_login
34
+ uri = URI.parse BASE_URI
35
+ uri.path = PATH[:auth]
36
+ uri.query = query_with_api_sig
37
+ uri
38
+ end
39
+
40
+ def login(cert)
41
+ result = JSON.parse open(
42
+ uri_to_authcheck(cert),
43
+ 'User-Agent' => "#{self.class}/#{VERSION} - Ruby"
44
+ ).read
45
+
46
+ if result['has_error']
47
+ raise AuthError.new(result['error']['message'])
48
+ else
49
+ result['user']
50
+ end
51
+ end
52
+
53
+ private
54
+ def api_sig(hash)
55
+ Digest::MD5.hexdigest(@secret + hash.to_a.map.sort_by {|i| i.first.to_s}.join)
56
+ end
57
+
58
+ def uri_to_authcheck(cert)
59
+ uri = URI.parse BASE_URI
60
+ uri.path = PATH[:json]
61
+ uri.query = query_with_api_sig(:cert => cert)
62
+ uri
63
+ end
64
+
65
+ def query_with_api_sig(request = {})
66
+ query = request.update(:api_key => @api_key)
67
+ query[:api_sig] = api_sig(query)
68
+ query.map {|i| i.join '=' }.join('&')
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,18 @@
1
+ $LOAD_PATH << File.dirname(__FILE__) + "/../lib"
2
+
3
+ require 'test/unit'
4
+ require 'hatena/api/auth'
5
+
6
+ class AuthTest < Test::Unit::TestCase
7
+ def setup
8
+ @auth = Hatena::API::Auth.new({
9
+ :api_key => 'test',
10
+ :secret => 'hoge',
11
+ })
12
+ end
13
+
14
+ def test_uri_to_login
15
+ assert_equal 'http://auth.hatena.ne.jp/auth?api_sig=8d03c299aa049c9e47e4f99e03f2df53&api_key=test', @auth.uri_to_login.to_s
16
+ end
17
+
18
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11
3
+ specification_version: 1
4
+ name: hatenaapiauth
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2006-04-25 00:00:00 +09:00
8
+ summary: Hatena Authentication API.
9
+ require_paths:
10
+ - lib
11
+ email: secondlife@no.spam@hatena.ne.jp
12
+ homepage: http://hatenaapiauth.rubyforge.net
13
+ rubyforge_project: hatenaapiauth
14
+ description: ""
15
+ autorequire: hatena/api/auth
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
+ authors:
29
+ - Yuichi Tateno
30
+ files:
31
+ - install.rb
32
+ - Rakefile
33
+ - test
34
+ - README
35
+ - examples
36
+ - lib
37
+ - VERSION
38
+ - test/method_test.rb
39
+ - examples/example.cgi
40
+ - lib/hatena
41
+ - lib/hatena/api
42
+ - lib/hatena/api/auth.rb
43
+ test_files:
44
+ - test/method_test.rb
45
+ rdoc_options:
46
+ - --line-numbers
47
+ - --inline-source
48
+ - -A cattr_accessor=object
49
+ extra_rdoc_files:
50
+ - README
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ requirements:
56
+ - json
57
+ dependencies:
58
+ - !ruby/object:Gem::Dependency
59
+ name: json
60
+ version_requirement:
61
+ version_requirements: !ruby/object:Gem::Version::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 0.4.1
66
+ version: