klap_p8push 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +31 -0
- data/LICENSE +22 -0
- data/README.md +22 -0
- data/Rakefile +10 -0
- data/lib/klap_p8push.rb +3 -0
- data/lib/p8push/client.rb +73 -0
- data/lib/p8push/notification.rb +121 -0
- data/lib/p8push/version.rb +3 -0
- data/p8push.gemspec +25 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 31baa4fe872c3af5ba2c5db40f48a1dad8600fdeb79143585cc8294e79989f77
|
4
|
+
data.tar.gz: 7c5b8ee2e2391097474f905b084c6fb5785b2392a7020ec9ce8e5962544050e2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0d859db75d76493c96e41b85188fa44a343824cc0349c1dddd6a241662bffcde9015cb380700dc649dcd3ccbd1f099304daa75edb28ac9cc08fc56d201a2b641
|
7
|
+
data.tar.gz: 192d3b18356ade2ef5c57112cde78f71df3d09d177c206396c476183b8c83d644c050caba7c55e55ad564fd86244a731b210c3d2836cd72dc46a190603834286
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
klap_p8push (1.0.4)
|
5
|
+
commander (= 4.4)
|
6
|
+
json (= 2.1.0)
|
7
|
+
jwt (= 1.5.6)
|
8
|
+
net-http2 (= 0.18.2)
|
9
|
+
|
10
|
+
GEM
|
11
|
+
remote: https://rubygems.org/
|
12
|
+
specs:
|
13
|
+
commander (4.4.0)
|
14
|
+
highline (~> 1.7.2)
|
15
|
+
highline (1.7.10)
|
16
|
+
http-2 (0.10.2)
|
17
|
+
json (2.1.0)
|
18
|
+
jwt (1.5.6)
|
19
|
+
net-http2 (0.18.2)
|
20
|
+
http-2 (~> 0.10.1)
|
21
|
+
rake (13.0.1)
|
22
|
+
|
23
|
+
PLATFORMS
|
24
|
+
ruby
|
25
|
+
|
26
|
+
DEPENDENCIES
|
27
|
+
klap_p8push!
|
28
|
+
rake (~> 13.0)
|
29
|
+
|
30
|
+
BUNDLED WITH
|
31
|
+
2.1.4
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2012–2015 Mattt Thompson (http://mattt.me/)
|
4
|
+
Copyright (c) 2017 Andrew Arrow
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
14
|
+
copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# p8push
|
2
|
+
ruby gem for apple push notifications using only the new p8 format not the older pem format
|
3
|
+
|
4
|
+
add to Gemfile: `gem 'p8push'`
|
5
|
+
|
6
|
+
```
|
7
|
+
export APN_PRIVATE_KEY=/path/APNsAuthKey_ABCDE12345.p8
|
8
|
+
export APN_TEAM_ID=XYZDE99911
|
9
|
+
export APN_KEY_ID=ABCDE12345
|
10
|
+
export APN_BUNDLE_ID=com.bundle.id
|
11
|
+
```
|
12
|
+
|
13
|
+
```
|
14
|
+
APN = P8push::Client.development
|
15
|
+
token = 'GETREALTOKENFROMADEVICE'
|
16
|
+
notification = P8push::Notification.new(device: token)
|
17
|
+
notification.alert = 'Hello, World!'
|
18
|
+
notification.topic = 'com.some.other.id' # if you do not want default ENV['APN_BUNDLE_ID'] one
|
19
|
+
APN.push(notification)
|
20
|
+
```
|
21
|
+
|
22
|
+
The gem with pem format this came from is https://github.com/nomad/houston
|
data/Rakefile
ADDED
data/lib/klap_p8push.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
require 'jwt'
|
3
|
+
require 'net-http2'
|
4
|
+
|
5
|
+
module P8push
|
6
|
+
|
7
|
+
APPLE_PRODUCTION_JWT_URI = 'https://api.push.apple.com'
|
8
|
+
APPLE_DEVELOPMENT_JWT_URI = 'https://api.sandbox.push.apple.com'
|
9
|
+
|
10
|
+
class Client
|
11
|
+
attr_accessor :jwt_uri
|
12
|
+
class << self
|
13
|
+
def development
|
14
|
+
client = self.new
|
15
|
+
client.jwt_uri = APPLE_DEVELOPMENT_JWT_URI
|
16
|
+
client
|
17
|
+
end
|
18
|
+
|
19
|
+
def production
|
20
|
+
client = self.new
|
21
|
+
client.jwt_uri = APPLE_PRODUCTION_JWT_URI
|
22
|
+
client
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize
|
27
|
+
@private_key = File.read(ENV['APN_PRIVATE_KEY'])
|
28
|
+
@team_id = ENV['APN_TEAM_ID']
|
29
|
+
@key_id = ENV['APN_KEY_ID']
|
30
|
+
@timeout = Float(ENV['APN_TIMEOUT'] || 2.0)
|
31
|
+
end
|
32
|
+
|
33
|
+
def jwt_http2_post(topic, payload, token)
|
34
|
+
ec_key = OpenSSL::PKey::EC.new(@private_key)
|
35
|
+
jwt_token = JWT.encode({iss: @team_id, iat: Time.now.to_i}, ec_key, 'ES256', {kid: @key_id})
|
36
|
+
client = NetHttp2::Client.new(@jwt_uri)
|
37
|
+
h = {}
|
38
|
+
h['apns-expiration'] = '0'
|
39
|
+
h['apns-priority'] = '10'
|
40
|
+
h['apns-topic'] = topic
|
41
|
+
h['authorization'] = "bearer #{jwt_token}"
|
42
|
+
h['content-type'] = 'application/json'
|
43
|
+
res = client.call(:post, '/3/device/'+token, body: payload.to_json, timeout: @timeout,
|
44
|
+
headers: h)
|
45
|
+
client.close
|
46
|
+
return nil if res.status.to_i == 200
|
47
|
+
res.body
|
48
|
+
end
|
49
|
+
|
50
|
+
def push(*notifications)
|
51
|
+
return if notifications.empty?
|
52
|
+
|
53
|
+
notifications.flatten!
|
54
|
+
|
55
|
+
notifications.each_with_index do |notification, index|
|
56
|
+
next unless notification.kind_of?(Notification)
|
57
|
+
next if notification.sent?
|
58
|
+
next unless notification.valid?
|
59
|
+
|
60
|
+
notification.id = index
|
61
|
+
|
62
|
+
err = jwt_http2_post(notification.topic, notification.payload, notification.token)
|
63
|
+
if err == nil
|
64
|
+
notification.mark_as_sent!
|
65
|
+
else
|
66
|
+
puts err
|
67
|
+
notification.apns_error_code = err
|
68
|
+
notification.mark_as_unsent!
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module P8push
|
4
|
+
class Notification
|
5
|
+
class APNSError < RuntimeError
|
6
|
+
CODES = {
|
7
|
+
0 => 'No errors encountered',
|
8
|
+
1 => 'Processing error',
|
9
|
+
2 => 'Missing device token',
|
10
|
+
3 => 'Missing topic',
|
11
|
+
4 => 'Missing payload',
|
12
|
+
5 => 'Invalid token size',
|
13
|
+
6 => 'Invalid topic size',
|
14
|
+
7 => 'Invalid payload size',
|
15
|
+
8 => 'Invalid token',
|
16
|
+
10 => 'Shutdown',
|
17
|
+
255 => 'Unknown error'
|
18
|
+
}
|
19
|
+
|
20
|
+
attr_reader :code
|
21
|
+
|
22
|
+
def initialize(code)
|
23
|
+
raise ArgumentError unless CODES.include?(code)
|
24
|
+
super(CODES[code])
|
25
|
+
@code = code
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
MAXIMUM_PAYLOAD_SIZE = 2048
|
30
|
+
|
31
|
+
attr_accessor :topic, :token, :alert, :badge, :sound, :category, :content_available, :mutable_content,
|
32
|
+
:custom_data, :id, :expiry, :priority
|
33
|
+
attr_reader :sent_at
|
34
|
+
attr_writer :apns_error_code
|
35
|
+
|
36
|
+
alias :device :token
|
37
|
+
alias :device= :token=
|
38
|
+
|
39
|
+
def initialize(options = {})
|
40
|
+
@token = options.delete(:token) || options.delete(:device)
|
41
|
+
@alert = options.delete(:alert)
|
42
|
+
@topic = options.delete(:topic) || ENV['APN_BUNDLE_ID']
|
43
|
+
@badge = options.delete(:badge)
|
44
|
+
@sound = options.delete(:sound)
|
45
|
+
@category = options.delete(:category)
|
46
|
+
@expiry = options.delete(:expiry)
|
47
|
+
@id = options.delete(:id)
|
48
|
+
@priority = options.delete(:priority)
|
49
|
+
@content_available = options.delete(:content_available)
|
50
|
+
@mutable_content = options.delete(:mutable_content)
|
51
|
+
|
52
|
+
@custom_data = options
|
53
|
+
end
|
54
|
+
|
55
|
+
def payload
|
56
|
+
json = {}.merge(@custom_data || {}).inject({}) { |h, (k, v)| h[k.to_s] = v; h }
|
57
|
+
|
58
|
+
json['aps'] ||= {}
|
59
|
+
json['aps']['alert'] = @alert if @alert
|
60
|
+
json['aps']['badge'] = @badge.to_i rescue 0 if @badge
|
61
|
+
json['aps']['sound'] = @sound if @sound
|
62
|
+
json['aps']['category'] = @category if @category
|
63
|
+
json['aps']['content-available'] = 1 if @content_available
|
64
|
+
json['aps']['mutable-content'] = 1 if @mutable_content
|
65
|
+
|
66
|
+
json
|
67
|
+
end
|
68
|
+
|
69
|
+
def message
|
70
|
+
data = [device_token_item,
|
71
|
+
payload_item,
|
72
|
+
identifier_item,
|
73
|
+
expiration_item,
|
74
|
+
priority_item].compact.join
|
75
|
+
[2, data.bytes.count, data].pack('cNa*')
|
76
|
+
end
|
77
|
+
|
78
|
+
def mark_as_sent!
|
79
|
+
@sent_at = Time.now
|
80
|
+
end
|
81
|
+
|
82
|
+
def mark_as_unsent!
|
83
|
+
@sent_at = nil
|
84
|
+
end
|
85
|
+
|
86
|
+
def sent?
|
87
|
+
!!@sent_at
|
88
|
+
end
|
89
|
+
|
90
|
+
def valid?
|
91
|
+
payload.to_json.bytesize <= MAXIMUM_PAYLOAD_SIZE
|
92
|
+
end
|
93
|
+
|
94
|
+
def error
|
95
|
+
APNSError.new(@apns_error_code) if @apns_error_code && @apns_error_code.nonzero?
|
96
|
+
end
|
97
|
+
|
98
|
+
private
|
99
|
+
|
100
|
+
def device_token_item
|
101
|
+
[1, 32, @token.gsub(/[<\s>]/, '')].pack('cnH64')
|
102
|
+
end
|
103
|
+
|
104
|
+
def payload_item
|
105
|
+
json = payload.to_json
|
106
|
+
[2, json.bytes.count, json].pack('cna*')
|
107
|
+
end
|
108
|
+
|
109
|
+
def identifier_item
|
110
|
+
[3, 4, @id].pack('cnN') unless @id.nil?
|
111
|
+
end
|
112
|
+
|
113
|
+
def expiration_item
|
114
|
+
[4, 4, @expiry.to_i].pack('cnN') unless @expiry.nil?
|
115
|
+
end
|
116
|
+
|
117
|
+
def priority_item
|
118
|
+
[5, 1, @priority].pack('cnc') unless @priority.nil?
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
data/p8push.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'p8push/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'klap_p8push'
|
7
|
+
s.authors = ['Andrew Arrow','Mattt Thompson', 'Kim Laplume']
|
8
|
+
s.email = 'kl@heartforge.eu'
|
9
|
+
s.license = 'MIT'
|
10
|
+
s.homepage = 'https://heartforge.eu'
|
11
|
+
s.version = P8push::VERSION
|
12
|
+
s.platform = Gem::Platform::RUBY
|
13
|
+
s.summary = 'Send Apple Push Notifications'
|
14
|
+
s.description = 'apple push notifications using only the new p8 format not the older pem format. fork from p8push'
|
15
|
+
|
16
|
+
s.add_dependency 'jwt', '1.5.6'
|
17
|
+
s.add_dependency 'commander', '4.4'
|
18
|
+
s.add_dependency 'json', '2.1.0'
|
19
|
+
s.add_dependency 'net-http2', '0.18.2'
|
20
|
+
|
21
|
+
s.files = Dir['./**/*'].reject { |file| file =~ /\.\/(bin|log|pkg|script|spec|test|vendor)/ }
|
22
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
23
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
24
|
+
s.require_paths = ['lib']
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: klap_p8push
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Arrow
|
8
|
+
- Mattt Thompson
|
9
|
+
- Kim Laplume
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2020-09-18 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: jwt
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - '='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.5.6
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - '='
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: 1.5.6
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: commander
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - '='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '4.4'
|
36
|
+
type: :runtime
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - '='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '4.4'
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
name: json
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - '='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 2.1.0
|
50
|
+
type: :runtime
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - '='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 2.1.0
|
57
|
+
- !ruby/object:Gem::Dependency
|
58
|
+
name: net-http2
|
59
|
+
requirement: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - '='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: 0.18.2
|
64
|
+
type: :runtime
|
65
|
+
prerelease: false
|
66
|
+
version_requirements: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - '='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: 0.18.2
|
71
|
+
description: apple push notifications using only the new p8 format not the older pem
|
72
|
+
format. fork from p8push
|
73
|
+
email: kl@heartforge.eu
|
74
|
+
executables: []
|
75
|
+
extensions: []
|
76
|
+
extra_rdoc_files: []
|
77
|
+
files:
|
78
|
+
- "./Gemfile"
|
79
|
+
- "./Gemfile.lock"
|
80
|
+
- "./LICENSE"
|
81
|
+
- "./README.md"
|
82
|
+
- "./Rakefile"
|
83
|
+
- "./lib/klap_p8push.rb"
|
84
|
+
- "./lib/p8push/client.rb"
|
85
|
+
- "./lib/p8push/notification.rb"
|
86
|
+
- "./lib/p8push/version.rb"
|
87
|
+
- "./p8push.gemspec"
|
88
|
+
homepage: https://heartforge.eu
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubygems_version: 3.1.2
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Send Apple Push Notifications
|
111
|
+
test_files: []
|