web_push 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e36118da84646cf33221743efe9ea48146da3af4
4
+ data.tar.gz: 0172ddbecfece1a9b5fa1f96e1d2215b49360593
5
+ SHA512:
6
+ metadata.gz: 81eefa2ac1f2f941caf17535f087a275de6e03da06dc96fdba72853c831cb9ad52cb550b861eecb83d74ecf51cd9c4253a8a27f3522da034914dd02fbc425afc
7
+ data.tar.gz: b1c9318be0545037d38c99faa2168fb81640643e3755b6f2c1a20ef8ae5b7d6f54745eeb101b48e6694a27a30076db0096b0a2d65c0bb0d7b234fcd794a6774c
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in web_push.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Yuki MIYAUCHI
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # WebPush
2
+
3
+ Similar to https://github.com/web-push-libs/web-push
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'web_push'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install web_push
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ subscription = {
25
+ endpoint: 'https://example.com/...',
26
+ keys: {
27
+ p256dh: 'URL-Safe Base64 String',
28
+ auth: 'URL-Safe Base64 String',
29
+ }
30
+ }
31
+ webpush = WebPush.new subscription
32
+ webpush.set_vapid_details('mailto:sender@example.com',
33
+ 'VAPID Public key(URL-Safe Base64 String)',
34
+ 'VAPID Private key(URL-Safe Base64 String)')
35
+ webpush.send_notification("Payload String")
36
+ ```
37
+
38
+ ## Development
39
+
40
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
41
+
42
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
43
+
44
+ ## Contributing
45
+
46
+ Bug reports and pull requests are welcome on GitHub at https://github.com/miyucy/web_push.
47
+
48
+
49
+ ## License
50
+
51
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "web_push"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ class WebPush
2
+ VERSION = "0.1.0"
3
+ end
data/lib/web_push.rb ADDED
@@ -0,0 +1,132 @@
1
+ require 'base64'
2
+ require 'net/https'
3
+ require 'openssl'
4
+ require 'securerandom'
5
+ require 'uri'
6
+ require 'ece'
7
+ require 'jwt'
8
+ require 'web_push/version'
9
+
10
+ class WebPush
11
+ GROUP_NAME = 'prime256v1'
12
+ DEFAULT_TTL = 60 * 60 * 24 * 7 * 4 # 4 weeks
13
+ DEFAULT_EXP = 60 * 60 * 24 # 1 day
14
+
15
+ module Utils
16
+ # Save:
17
+ # pkey = generate_vapid_pkey
18
+ # File.write 'server.pem', pkey.to_pem
19
+ # Load:
20
+ # OpenSSL::PKey.read File.read('server.pem')
21
+ def self.generate_vapid_pkey
22
+ OpenSSL::PKey::EC.new(GROUP_NAME).tap(&:generate_key)
23
+ end
24
+ end
25
+
26
+ def initialize(subscription, ttl: DEFAULT_TTL, exp: DEFAULT_EXP)
27
+ @subscription = subscription
28
+ @endpoint = @subscription[:endpoint]
29
+ @p256dh = @subscription[:keys][:p256dh]
30
+ @auth = @subscription[:keys][:auth]
31
+ @ttl = ttl
32
+ @exp = exp
33
+ end
34
+
35
+ def send_notification(payload)
36
+ params = generate_http_request(payload)
37
+
38
+ uri = params[:uri]
39
+ https = Net::HTTP.new uri.host, uri.port
40
+ https.use_ssl = true
41
+
42
+ request = Net::HTTP::Post.new uri.request_uri, params[:headers]
43
+ request.body = params[:body]
44
+
45
+ https.request request
46
+ end
47
+
48
+ def generate_http_request(payload)
49
+ {
50
+ method: 'POST',
51
+ headers: {
52
+ 'TTL' => @ttl.to_s,
53
+ 'Content-Type' => 'application/octet-stream',
54
+ 'Content-Encoding' => 'aesgcm',
55
+ },
56
+ body: nil,
57
+ uri: URI(@endpoint),
58
+ }.tap do |request|
59
+ encrypted = encrypt(@p256dh, @auth, payload)
60
+ request[:headers]['Content-Length'] = encrypted[:cipher].bytesize.to_s
61
+ request[:headers]['Encryption'] = 'keyid=p256dh;salt=' + urlsafe_encode64(encrypted[:salt])
62
+ request[:headers]['Crypto-Key'] = 'keyid=p256dh;dh=' + urlsafe_encode64(encrypted[:server_public_key])
63
+ request[:body] = encrypted[:cipher]
64
+
65
+ audience = request[:uri].scheme + "://" + request[:uri].host
66
+ vapid_headers = generate_vapid_headers audience
67
+ request[:headers]['Authorization'] = vapid_headers['Authorization']
68
+ request[:headers]['Crypto-Key'] += ';' + vapid_headers['Crypto-Key']
69
+ end
70
+ end
71
+
72
+ def encrypt(p256dh, auth, payload)
73
+ user_public_key = urlsafe_decode64 p256dh
74
+ user_auth = urlsafe_decode64 auth
75
+
76
+ local_curve = Utils.generate_vapid_pkey
77
+ user_public_key_point = OpenSSL::PKey::EC::Point.new(local_curve.group, OpenSSL::BN.new(user_public_key, 2))
78
+
79
+ key = local_curve.dh_compute_key(user_public_key_point)
80
+ server_public_key = local_curve.public_key.to_bn.to_s(2)
81
+
82
+ params = {
83
+ key: key,
84
+ salt: SecureRandom.random_bytes(16),
85
+ server_public_key: server_public_key,
86
+ user_public_key: user_public_key,
87
+ auth: user_auth
88
+ }
89
+ params[:cipher] = ECE.encrypt(payload, params)
90
+
91
+ params
92
+ end
93
+
94
+ def generate_vapid_headers(audience, sub: @vapid_subject, exp: @exp)
95
+ jwt = JWT.encode({
96
+ aud: audience,
97
+ exp: Time.now.to_i + exp,
98
+ sub: sub,
99
+ }, @vapid_pkey, 'ES256')
100
+ {
101
+ 'Authorization' => 'WebPush ' + jwt,
102
+ 'Crypto-Key' => 'p256ecdsa=' + urlsafe_encode64(@vapid_public_key_bn),
103
+ }
104
+ end
105
+
106
+ def set_vapid_details(subject, public_key, private_key)
107
+ @vapid_subject = subject
108
+ @vapid_public_key = public_key
109
+ @vapid_private_key = private_key
110
+ @vapid_pkey = generate_vapid_pkey(@vapid_public_key, @vapid_private_key)
111
+ @vapid_public_key_bn = @vapid_pkey.public_key.to_bn.to_s(2)
112
+ end
113
+
114
+ def generate_vapid_pkey(public_key, private_key)
115
+ pvtbn = OpenSSL::BN.new(urlsafe_decode64(private_key), 2)
116
+ pubbn = OpenSSL::BN.new(urlsafe_decode64(public_key), 2)
117
+
118
+ pkey = Utils.generate_vapid_pkey
119
+ pkey.private_key = pvtbn
120
+ pkey.public_key = OpenSSL::PKey::EC::Point.new(pkey.group, pubbn)
121
+
122
+ pkey
123
+ end
124
+
125
+ def urlsafe_encode64(bin)
126
+ Base64.urlsafe_encode64(bin).delete('=')
127
+ end
128
+
129
+ def urlsafe_decode64(bin)
130
+ Base64.urlsafe_decode64(bin)
131
+ end
132
+ end
data/web_push.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'web_push/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "web_push"
8
+ spec.version = WebPush::VERSION
9
+ spec.authors = ["miyucy"]
10
+ spec.email = ["fistfvck@gmail.com"]
11
+
12
+ spec.summary = %q{Send web push}
13
+ spec.description = %q{Send web push}
14
+ spec.homepage = "https://github.com/miyucy/web_push"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_runtime_dependency 'ece'
25
+ spec.add_runtime_dependency 'jwt'
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.13"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: web_push
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - miyucy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ece
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: jwt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: Send web push
84
+ email:
85
+ - fistfvck@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/web_push.rb
100
+ - lib/web_push/version.rb
101
+ - web_push.gemspec
102
+ homepage: https://github.com/miyucy/web_push
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.6.7
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Send web push
126
+ test_files: []