ts_p8push 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6d32a8f34ba8a667ee1b4d736c82e1731b2bcb82e0a7f93383e7d67912092475
4
+ data.tar.gz: d7ddaa9a8f768298966d4e33ed7d3cbba6a2185fe0b21745e4453004d1670930
5
+ SHA512:
6
+ metadata.gz: 268abdeba968f435a6da77fe8e2a4b15f273617c94341f7dead9b6b1d11fad7a77554f94a63dd8dec6019726d0f2ead72ee8880df1b0e0d9263d40faf1fbd338
7
+ data.tar.gz: f4d4431dfeb4fe65d575ae0eac03e12d5222a1a518dd2e8638c7931637d352716d4be4aaa1d93a981694039ebc31a35a27a74ecda926bbdb16e0c72cab4cc62a
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem "rake", "~> 13.0"
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,29 @@
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 'ts_p8push'`
5
+
6
+ ```
7
+ [OPTIONAL]
8
+ export APN_PRIVATE_KEY=/path/APNsAuthKey_ABCDE12345.p8
9
+ export APN_TEAM_ID=XYZDE99911
10
+ export APN_KEY_ID=ABCDE12345
11
+ export APN_BUNDLE_ID=com.bundle.id
12
+ ```
13
+
14
+ ```
15
+ Environment can be intiated either via exports and use below
16
+ APN = P8push::Client.development
17
+
18
+ OR
19
+ Environment can be intiated by providing the configs on the go
20
+ APN = P8push::Client.development(private_key: 'YOUR_PRIVATE_KEY_P8_CONTENTS', team_id: 'YOUR_TEAM_ID', key_id: 'YOUR_KEY_ID', timeout: 2.0)
21
+
22
+ token = 'GETREALTOKENFROMADEVICE'
23
+ notification = P8push::Notification.new(device: token)
24
+ notification.alert = 'Hello, World!'
25
+ notification.topic = 'com.some.other.id' # if you do not want default ENV['APN_BUNDLE_ID'] one
26
+ APN.push(notification)
27
+ ```
28
+
29
+ The gem with pem format this came from is https://github.com/nomad/houston
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,3 @@
1
+ require 'p8push/version'
2
+ require 'p8push/client'
3
+ require 'p8push/notification'
@@ -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(private_key: nil, team_id: nil, key_id: nil, timeout: nil)
14
+ client = self.new(private_key: private_key, team_id: team_id, key_id: key_id, timeout: timeout)
15
+ client.jwt_uri = APPLE_DEVELOPMENT_JWT_URI
16
+ client
17
+ end
18
+
19
+ def production(private_key: nil, team_id: nil, key_id: nil, timeout: nil)
20
+ client = self.new(private_key: private_key, team_id: team_id, key_id: key_id, timeout: timeout)
21
+ client.jwt_uri = APPLE_PRODUCTION_JWT_URI
22
+ client
23
+ end
24
+ end
25
+
26
+ def initialize(private_key: nil, team_id: nil, key_id: nil, timeout: nil)
27
+ @private_key = private_key || File.read(ENV['APN_PRIVATE_KEY'])
28
+ @team_id = team_id || ENV['APN_TEAM_ID']
29
+ @key_id = key_id || ENV['APN_KEY_ID']
30
+ @timeout = 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
@@ -0,0 +1,3 @@
1
+ module P8push
2
+ VERSION = '1.0.7'
3
+ 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 = 'ts_p8push'
7
+ s.authors = ['Andrew Arrow','Mattt Thompson', 'Kim Laplume', 'Talha Shoaib']
8
+ s.email = 'talhashoaib27@gmail.com'
9
+ s.license = 'MIT'
10
+ s.homepage = 'https://talhashoaib.me'
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 1klap/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,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ts_p8push
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.7
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Arrow
8
+ - Mattt Thompson
9
+ - Kim Laplume
10
+ - Talha Shoaib
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2021-06-02 00:00:00.000000000 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: jwt
18
+ requirement: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - '='
21
+ - !ruby/object:Gem::Version
22
+ version: 1.5.6
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.5.6
30
+ - !ruby/object:Gem::Dependency
31
+ name: commander
32
+ requirement: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - '='
35
+ - !ruby/object:Gem::Version
36
+ version: '4.4'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - '='
42
+ - !ruby/object:Gem::Version
43
+ version: '4.4'
44
+ - !ruby/object:Gem::Dependency
45
+ name: json
46
+ requirement: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - '='
49
+ - !ruby/object:Gem::Version
50
+ version: 2.1.0
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - '='
56
+ - !ruby/object:Gem::Version
57
+ version: 2.1.0
58
+ - !ruby/object:Gem::Dependency
59
+ name: net-http2
60
+ requirement: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - '='
63
+ - !ruby/object:Gem::Version
64
+ version: 0.18.2
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - '='
70
+ - !ruby/object:Gem::Version
71
+ version: 0.18.2
72
+ description: apple push notifications using only the new p8 format not the older pem
73
+ format. fork from 1klap/p8push
74
+ email: talhashoaib27@gmail.com
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - "./Gemfile"
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://talhashoaib.me
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
+ rubyforge_project:
108
+ rubygems_version: 2.7.8
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Send Apple Push Notifications
112
+ test_files: []