nayutaya-wsse 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,36 @@
1
+
2
+ require "rake/testtask"
3
+
4
+ task :default => [:test]
5
+
6
+ Rake::TestTask.new do |test|
7
+ test.libs << "test"
8
+ test.test_files = Dir.glob("test/**/*_test.rb")
9
+ test.verbose = true
10
+ end
11
+
12
+ desc "Generate gemspec file from template"
13
+ task :gemspec do
14
+ require "erb"
15
+ require "lib/wsse/version"
16
+
17
+ name = "nayutaya-wsse"
18
+ src = File.open("#{name}.gemspec.erb", "rb") { |file| file.read }
19
+ erb = ERB.new(src, nil, "-")
20
+
21
+ version = Wsse::VERSION
22
+ date = Time.now.strftime("%Y-%m-%d")
23
+
24
+ files = Dir.glob("**/*").
25
+ select { |path| File.file?(path) }.
26
+ reject { |path| /^nbproject\// =~ path }.
27
+ sort
28
+
29
+ test_files = Dir.glob("test/**.rb").
30
+ select { |path| File.file?(path) }.
31
+ sort
32
+
33
+ File.open("#{name}.gemspec", "wb") { |file|
34
+ file.write(erb.result(binding))
35
+ }
36
+ end
data/lib/wsse/core.rb ADDED
@@ -0,0 +1,6 @@
1
+ # coding: utf-8
2
+
3
+ require "wsse/version"
4
+
5
+ module Wsse
6
+ end
@@ -0,0 +1 @@
1
+ # coding: utf-8
@@ -0,0 +1,42 @@
1
+ # coding: utf-8
2
+
3
+ require "time"
4
+ require "digest/sha1"
5
+
6
+ module Wsse
7
+ module UsernameTokenBuilder
8
+ def self.create_created_time(time = Time.now)
9
+ return time.utc.iso8601
10
+ end
11
+
12
+ def self.create_nonce
13
+ return 20.times.map { rand(256) }.pack("C*")
14
+ end
15
+
16
+ def self.create_token_params(username, password, nonce = nil, created = nil)
17
+ nonce ||= self.create_nonce
18
+ created ||= self.create_created_time
19
+ digest = Digest::SHA1.digest(nonce + created + password)
20
+ return {
21
+ "Username" => username,
22
+ "PasswordDigest" => [digest].pack("m").chomp,
23
+ "Nonce" => [nonce].pack("m").chomp,
24
+ "Created" => created,
25
+ }
26
+ end
27
+
28
+ def self.format_token(params)
29
+ return format(
30
+ %|UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"|,
31
+ (params["Username"] || raise(ArgumentError)),
32
+ (params["PasswordDigest"] || raise(ArgumentError)),
33
+ (params["Nonce"] || raise(ArgumentError)),
34
+ (params["Created"] || raise(ArgumentError)))
35
+ end
36
+
37
+ def self.create_token(username, password, nonce = nil, created = nil)
38
+ params = self.create_token_params(username, password, nonce, created)
39
+ return self.format_token(params)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+
3
+ module Wsse
4
+ module UsernameTokenParser
5
+ def self.parse(value)
6
+ if /\AUsernameToken (.+?=".+?"(?:, .+?=".+?")*)\z/ =~ value.to_s
7
+ return $1.scan(/(?:\A|, )(.+?)="(.+?)"/).inject({}) { |memo, (key, value)|
8
+ memo[key] = value
9
+ memo
10
+ }
11
+ else
12
+ return nil
13
+ end
14
+ end
15
+
16
+ def self.parse_time(value)
17
+ if /\A(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)Z\z/ =~ value.to_s
18
+ return Time.utc(*($~.captures.map { |s| s.to_i }))
19
+ else
20
+ return nil
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ # coding: utf-8
2
+
3
+ module Wsse
4
+ VERSION = "0.0.0"
5
+ end
data/lib/wsse.rb ADDED
@@ -0,0 +1,3 @@
1
+ # coding: utf-8
2
+
3
+ require "wsse/core"
@@ -0,0 +1,45 @@
1
+
2
+ Gem::Specification.new do |s|
3
+ s.specification_version = 2
4
+ s.required_rubygems_version = Gem::Requirement.new(">= 0")
5
+ s.required_ruby_version = Gem::Requirement.new(">= 1.8.6")
6
+
7
+ s.name = "nayutaya-wsse"
8
+ s.version = "0.0.0"
9
+ s.date = "2009-10-27"
10
+
11
+ s.authors = ["Yuya Kato"]
12
+ s.email = "yuyakato@gmail.com"
13
+
14
+ s.summary = "X-WSSE header builder and parser"
15
+ s.description = "X-WSSE header builder and parser."
16
+ s.homepage = "http://github.com/nayutaya/wsse/"
17
+
18
+ s.rubyforge_project = nil
19
+ s.has_rdoc = false
20
+ s.require_paths = ["lib"]
21
+
22
+ s.files = [
23
+ "README",
24
+ "Rakefile",
25
+ "lib/wsse.rb",
26
+ "lib/wsse/core.rb",
27
+ "lib/wsse/http_header.rb",
28
+ "lib/wsse/username_token_builder.rb",
29
+ "lib/wsse/username_token_parser.rb",
30
+ "lib/wsse/version.rb",
31
+ "nayutaya-wsse.gemspec",
32
+ "nayutaya-wsse.gemspec.erb",
33
+ "test/test_helper.rb",
34
+ "test/wsse_http_header_test.rb",
35
+ "test/wsse_username_token_builder_test.rb",
36
+ "test/wsse_username_token_parser_test.rb",
37
+ ]
38
+ s.test_files = [
39
+ "test/test_helper.rb",
40
+ "test/wsse_http_header_test.rb",
41
+ "test/wsse_username_token_builder_test.rb",
42
+ "test/wsse_username_token_parser_test.rb",
43
+ ]
44
+ s.extra_rdoc_files = []
45
+ end
@@ -0,0 +1,33 @@
1
+
2
+ Gem::Specification.new do |s|
3
+ s.specification_version = 2
4
+ s.required_rubygems_version = Gem::Requirement.new(">= 0")
5
+ s.required_ruby_version = Gem::Requirement.new(">= 1.8.6")
6
+
7
+ s.name = "nayutaya-wsse"
8
+ s.version = <%= version.dump %>
9
+ s.date = <%= date.dump %>
10
+
11
+ s.authors = ["Yuya Kato"]
12
+ s.email = "yuyakato@gmail.com"
13
+
14
+ s.summary = "X-WSSE header builder and parser"
15
+ s.description = "X-WSSE header builder and parser."
16
+ s.homepage = "http://github.com/nayutaya/wsse/"
17
+
18
+ s.rubyforge_project = nil
19
+ s.has_rdoc = false
20
+ s.require_paths = ["lib"]
21
+
22
+ s.files = [
23
+ <%- files.each { |path| -%>
24
+ <%= path.dump %>,
25
+ <%- } -%>
26
+ ]
27
+ s.test_files = [
28
+ <%- test_files.each { |path| -%>
29
+ <%= path.dump %>,
30
+ <%- } -%>
31
+ ]
32
+ s.extra_rdoc_files = []
33
+ end
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+
3
+ require "test/unit"
4
+ require "rubygems"
5
+ require "kagemusha"
6
+ require "kagemusha/datetime"
7
+
8
+ begin
9
+ require "redgreen"
10
+ rescue LoadError
11
+ # nop
12
+ end
13
+
14
+ begin
15
+ require "win32console" if /win32/ =~ RUBY_PLATFORM
16
+ rescue LoadError
17
+ # nop
18
+ end
19
+
20
+ $:.unshift(File.dirname(__FILE__) + "/../lib")
@@ -0,0 +1,12 @@
1
+ # coding: utf-8
2
+
3
+ require File.dirname(__FILE__) + "/test_helper"
4
+
5
+ class WsseHttpHeaderTest < Test::Unit::TestCase
6
+ def setup
7
+ end
8
+
9
+ def test_true
10
+ assert true
11
+ end
12
+ end
@@ -0,0 +1,115 @@
1
+ # coding: utf-8
2
+
3
+ require File.dirname(__FILE__) + "/test_helper"
4
+ require "wsse/username_token_builder"
5
+
6
+ class WsseUsernameTokenBuilderTest < Test::Unit::TestCase
7
+ def setup
8
+ @mod = Wsse::UsernameTokenBuilder
9
+ @musha = Kagemusha.new(@mod)
10
+ end
11
+
12
+ def test_create_created_time
13
+ assert_equal("2000-01-01T00:00:00Z", @mod.create_created_time(Time.utc(2000, 1, 1, 0, 0, 0)))
14
+ assert_equal("2001-12-31T23:59:59Z", @mod.create_created_time(Time.utc(2001, 12, 31, 23, 59, 59)))
15
+ Kagemusha::DateTime.at(Time.utc(2009, 1, 2, 3, 4, 5)) {
16
+ assert_equal("2009-01-02T03:04:05Z", @mod.create_created_time())
17
+ }
18
+ end
19
+
20
+ def test_create_nonce
21
+ srand(0)
22
+ assert_equal(20, @mod.create_nonce.size)
23
+ assert_equal(10, 10.times.map { @mod.create_nonce }.uniq.size)
24
+ end
25
+
26
+ def test_create_token_params
27
+ username = "username"
28
+ password = "password"
29
+ nonce = "nonce"
30
+ created = "2000-01-01T00:00:00Z"
31
+ expected = {
32
+ "Username" => username,
33
+ "PasswordDigest" => [Digest::SHA1.digest("#{nonce}#{created}#{password}")].pack("m").chomp,
34
+ "Nonce" => [nonce].pack("m").chomp,
35
+ "Created" => created,
36
+ }
37
+ assert_equal(expected, @mod.create_token_params(username, password, nonce, created))
38
+ end
39
+
40
+ def test_create_token_params__default_created
41
+ username = "username1"
42
+ password = "password1"
43
+ nonce = "nonce"
44
+ created = "2001-02-03T04:05:06Z"
45
+ expected = {
46
+ "Username" => username,
47
+ "PasswordDigest" => [Digest::SHA1.digest("#{nonce}#{created}#{password}")].pack("m").chomp,
48
+ "Nonce" => [nonce].pack("m").chomp,
49
+ "Created" => created,
50
+ }
51
+ @musha.defs(:create_created_time) { created }
52
+ @musha.swap {
53
+ assert_equal(expected, @mod.create_token_params(username, password, nonce))
54
+ }
55
+ end
56
+
57
+ def test_create_token_params__default_nonce
58
+ username = "username2"
59
+ password = "password2"
60
+ nonce = "foobarbaz"
61
+ created = "2001-02-03T04:05:06Z"
62
+ expected = {
63
+ "Username" => username,
64
+ "PasswordDigest" => [Digest::SHA1.digest("#{nonce}#{created}#{password}")].pack("m").chomp,
65
+ "Nonce" => [nonce].pack("m").chomp,
66
+ "Created" => created,
67
+ }
68
+ @musha.defs(:create_nonce) { nonce }
69
+ @musha.defs(:create_created_time) { created }
70
+ @musha.swap {
71
+ assert_equal(expected, @mod.create_token_params(username, password))
72
+ }
73
+ end
74
+
75
+ def test_format_token
76
+ basic = {
77
+ "Username" => "username",
78
+ "PasswordDigest" => "digest",
79
+ "Nonce" => "nonce",
80
+ "Created" => "created",
81
+ }
82
+ assert_equal(
83
+ %|UsernameToken Username="username", PasswordDigest="digest", Nonce="nonce", Created="created"|,
84
+ @mod.format_token(basic))
85
+ assert_raise(ArgumentError) { @mod.format_token(basic.merge("Username" => nil)) }
86
+ assert_raise(ArgumentError) { @mod.format_token(basic.merge("PasswordDigest" => nil)) }
87
+ assert_raise(ArgumentError) { @mod.format_token(basic.merge("Nonce" => nil)) }
88
+ assert_raise(ArgumentError) { @mod.format_token(basic.merge("Created" => nil)) }
89
+ end
90
+
91
+ def test_create_token
92
+ assert_equal(
93
+ %|UsernameToken Username="username", PasswordDigest="DzunnEf/2CKuhInsnmEHonW5qQs=", Nonce="bm9uY2U=", Created="2000-01-01T00:00:00Z"|,
94
+ @mod.create_token("username", "password", "nonce", "2000-01-01T00:00:00Z"))
95
+ end
96
+
97
+ def test_create_token__default_created
98
+ @musha.defs(:create_created_time) { "2001-02-03T04:05:06Z" }
99
+ @musha.swap {
100
+ assert_equal(
101
+ %|UsernameToken Username="username1", PasswordDigest="lTTtMZC0IJyrV8aITFjLgdDzXzw=", Nonce="bm9uY2U=", Created="2001-02-03T04:05:06Z"|,
102
+ @mod.create_token("username1", "password1", "nonce"))
103
+ }
104
+ end
105
+
106
+ def test_create_token__default_nonce
107
+ @musha.defs(:create_nonce) { "foobarbaz" }
108
+ @musha.defs(:create_created_time) { "2001-02-03T04:05:06Z" }
109
+ @musha.swap {
110
+ assert_equal(
111
+ %|UsernameToken Username="username2", PasswordDigest="zmw1lW05c8tDlXsnU1IypUIt8uA=", Nonce="Zm9vYmFyYmF6", Created="2001-02-03T04:05:06Z"|,
112
+ @mod.create_token("username2", "password2"))
113
+ }
114
+ end
115
+ end
@@ -0,0 +1,66 @@
1
+ # coding: utf-8
2
+
3
+ require File.dirname(__FILE__) + "/test_helper"
4
+ require "wsse/username_token_parser"
5
+
6
+ class WsseUsernameTokenParserTest < Test::Unit::TestCase
7
+ def setup
8
+ @mod = Wsse::UsernameTokenParser
9
+ end
10
+
11
+ def test_parse__1
12
+ # http://www.teria.com/~koseki/tools/wssegen/
13
+ # UserName: test, Password: test, Nonce: auto, Created: auto
14
+ value = %|UsernameToken Username="test", PasswordDigest="XYuMkeTAdwEwKhyU/4uw/pbvqrc=", Nonce="Mzg5ODU5MDExYTljODQ5Yg==", Created="2009-10-08T02:31:57Z"|
15
+ expected = {
16
+ "Username" => "test",
17
+ "PasswordDigest" => "XYuMkeTAdwEwKhyU/4uw/pbvqrc=",
18
+ "Nonce" => "Mzg5ODU5MDExYTljODQ5Yg==",
19
+ "Created" => "2009-10-08T02:31:57Z",
20
+ }
21
+ assert_equal(expected, @mod.parse(value))
22
+ end
23
+
24
+ def test_parse__2
25
+ # http://d.hatena.ne.jp/keyword/%A4%CF%A4%C6%A4%CA%A5%D5%A5%A9%A5%C8%A5%E9%A5%A4%A5%D5AtomAPI
26
+ value = %|UsernameToken Username="hatena", PasswordDigest="ZCNaK2jrXr4+zsCaYK/YLUxImZU=", Nonce="Uh95NQlviNpJQR1MmML+zq6pFxE=", Created="2005-01-18T03:20:15Z"|
27
+ expected = {
28
+ "Username" => "hatena",
29
+ "PasswordDigest" => "ZCNaK2jrXr4+zsCaYK/YLUxImZU=",
30
+ "Nonce" => "Uh95NQlviNpJQR1MmML+zq6pFxE=",
31
+ "Created" => "2005-01-18T03:20:15Z",
32
+ }
33
+ assert_equal(expected, @mod.parse(value))
34
+ end
35
+
36
+ def test_parse__3
37
+ # http://www.witha.jp/blog/archives/2004/06/_atom_api.html
38
+ value = %|UsernameToken Username="Melody", PasswordDigest="VfJavTaTy3BhKkeY/WVu9L6cdVA=", Created="2004-01-20T01:09:39Z", Nonce="7c19aeed85b93d35ba42e357f10ca19bf314d622"|
39
+ expected = {
40
+ "Username" => "Melody",
41
+ "PasswordDigest" => "VfJavTaTy3BhKkeY/WVu9L6cdVA=",
42
+ "Nonce" => "7c19aeed85b93d35ba42e357f10ca19bf314d622",
43
+ "Created" => "2004-01-20T01:09:39Z",
44
+ }
45
+ assert_equal(expected, @mod.parse(value))
46
+ end
47
+
48
+ def test_parse__nil
49
+ assert_equal(nil, @mod.parse(nil))
50
+ end
51
+
52
+ def test_parse__empty
53
+ assert_equal(nil, @mod.parse(""))
54
+ end
55
+
56
+ def test_parse_time
57
+ assert_equal(Time.utc(2009, 1, 1, 0, 0, 0), @mod.parse_time("2009-01-01T00:00:00Z"))
58
+ assert_equal(Time.utc(2009, 12, 31, 23, 59, 59), @mod.parse_time("2009-12-31T23:59:59Z"))
59
+ assert_equal(Time.utc(2001, 2, 3, 4, 5, 6), @mod.parse_time("2001-02-03T04:05:06Z"))
60
+ end
61
+
62
+ def test_parse_time__empty
63
+ assert_equal(nil, @mod.parse_time(nil))
64
+ assert_equal(nil, @mod.parse_time(""))
65
+ end
66
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nayutaya-wsse
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuya Kato
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-27 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: X-WSSE header builder and parser.
17
+ email: yuyakato@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README
26
+ - Rakefile
27
+ - lib/wsse.rb
28
+ - lib/wsse/core.rb
29
+ - lib/wsse/http_header.rb
30
+ - lib/wsse/username_token_builder.rb
31
+ - lib/wsse/username_token_parser.rb
32
+ - lib/wsse/version.rb
33
+ - nayutaya-wsse.gemspec
34
+ - nayutaya-wsse.gemspec.erb
35
+ - test/test_helper.rb
36
+ - test/wsse_http_header_test.rb
37
+ - test/wsse_username_token_builder_test.rb
38
+ - test/wsse_username_token_parser_test.rb
39
+ has_rdoc: true
40
+ homepage: http://github.com/nayutaya/wsse/
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.8.6
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.5
64
+ signing_key:
65
+ specification_version: 2
66
+ summary: X-WSSE header builder and parser
67
+ test_files:
68
+ - test/test_helper.rb
69
+ - test/wsse_http_header_test.rb
70
+ - test/wsse_username_token_builder_test.rb
71
+ - test/wsse_username_token_parser_test.rb