vonage-jwt 0.1.0

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: 75f27de718f0fdf8dce4a63da2ea80d69921985dc41c4c056e6072bce3c7bb4d
4
+ data.tar.gz: 6e1dd89c4f5df012efb95a76d827f61ac5b1b1dce5a0d658e007e6e171f4229c
5
+ SHA512:
6
+ metadata.gz: 025d062660e96cd6bcaef552ebb71fe2d5249cf4be450e492f48dedd1ac49f8464309f0de70ffdc909a03af140f626e89e212cd85e07af51143788a1be6c80c6
7
+ data.tar.gz: 61f33cf546932c49cbc7b0fb4433705edeb972746122bfdaea2f2f9db789eccd77d63f7859c42800d28ff90102835b05048b9243e742da60d74de1adfa32a9e1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Vonage
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # Vonage JWT Generator for Ruby
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/vonage-jwt.svg)](https://badge.fury.io/rb/vonage-jwt)![Coverage Status](https://github.com/Vonage/vonage-jwt-ruby/workflows/CI/badge.svg)[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg)](CODE_OF_CONDUCT.md)
4
+
5
+ This is the Ruby library to generate Vonage JSON Web Tokens (JWTs). To use it you'll
6
+ need a Vonage account. Sign up [for free at vonage.com][signup].
7
+
8
+ * [Requirements](#requirements)
9
+ * [Installation](#installation)
10
+ * [Usage](#usage)
11
+ * [Documentation](#documentation)
12
+ * [License](#license)
13
+
14
+ ## Requirements
15
+
16
+ The JWT generator supports Ruby version 2.7 or newer.
17
+
18
+ ## Installation
19
+
20
+ To install the Ruby client library using Rubygems:
21
+
22
+ gem install vonage-jwt
23
+
24
+ Alternatively you can clone the repository:
25
+
26
+ git clone git@github.com:Vonage/vonage-jwt-ruby.git
27
+
28
+ ## Usage
29
+
30
+ By default the Vonage JWT generator creates a short lived JWT (15 minutes) per request.
31
+ To generate a long lived JWT for multiple requests, specify a longer value in the `exp`
32
+ parameter during initialization.
33
+
34
+ Example with no custom configuration:
35
+
36
+ ```ruby
37
+ @builder = Vonage::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
38
+ @token = @builder.jwt.generate
39
+ ```
40
+
41
+ Example providing custom configuration options:
42
+
43
+ ```ruby
44
+ @builder = Vonage::JWTBuilder.new(
45
+ application_id: YOUR_APPLICATION_ID,
46
+ private_key: YOUR_PRIVATE_KEY,
47
+ ttl: 500,
48
+ paths: {
49
+ "acl": {
50
+ "paths": {
51
+ "/messages": {
52
+ "methods": ["POST", "GET"],
53
+ "filters": {
54
+ "from": "447977271009"
55
+ }
56
+ }
57
+ }
58
+ }
59
+ },
60
+ subject: 'My_Custom_Subject'
61
+ )
62
+ @token = @builder.jwt.generate
63
+ ```
64
+
65
+ ## Documentation
66
+
67
+ Vonage Ruby JWT documentation: https://www.rubydoc.info/github/Vonage/vonage-jwt
68
+
69
+ Vonage Ruby code examples: https://github.com/Vonage/vonage-ruby-code-snippets
70
+
71
+ Vonage API reference: https://developer.vonage.com/api
72
+
73
+ ## License
74
+
75
+ This library is released under the [MIT License][license]
76
+
77
+ [signup]: https://ui.idp.vonage.com/ui/auth/registration?utm_source=DEV_REL&utm_medium=github&utm_campaign=ruby-client-library
78
+ [license]: LICENSE.txt
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jwt'
4
+
5
+ module Vonage
6
+ class JWT
7
+ attr_accessor :generator, :typ, :iat
8
+
9
+ def initialize(params = {})
10
+ @generator = params.fetch(:generator)
11
+ @typ = params.fetch(:typ, 'JWT')
12
+ @iat = params.fetch(:iat, Time.now.to_i)
13
+ end
14
+
15
+ def generate
16
+ ::JWT.encode(to_payload, generator.private_key, generator.alg, header_fields={typ: typ})
17
+ end
18
+
19
+ def to_payload
20
+ hash = {
21
+ iat: iat,
22
+ jti: generator.jti,
23
+ exp: generator.exp || iat + generator.ttl,
24
+ sub: generator.subject,
25
+ application_id: generator.application_id
26
+ }
27
+ hash.merge!(generator.paths) if generator.paths
28
+ hash.merge!(nbf: generator.nbf) if generator.nbf
29
+ hash.merge!(generator.additional_claims) if !generator.additional_claims.empty?
30
+ hash
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openssl'
4
+ require 'securerandom'
5
+ require_relative 'jwt'
6
+
7
+ module Vonage
8
+ class JWTBuilder
9
+ # Generate an encoded JSON Web Token.
10
+ #
11
+ # By default the Vonage JWT generator creates a short lived (15 minutes) JWT per request.
12
+ #
13
+ # To generate a long lived JWT for multiple requests, specify a longer value in the `exp`
14
+ # parameter during initialization.
15
+ #
16
+ # Example with no custom configuration:
17
+ #
18
+ # @example
19
+ # @builder = Vonage::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
20
+ # @token = @builder.jwt.generate
21
+ #
22
+ # Example providing custom configuration options:
23
+ #
24
+ # @example
25
+ # @generator = Vonage::JWTBuilder.new(
26
+ # application_id: YOUR_APPLICATION_ID,
27
+ # private_key: YOUR_PRIVATE_KEY,
28
+ # ttl: 500,
29
+ # subject: 'My_Custom_Subject'
30
+ # )
31
+ # @token = @builder.jwt.generate
32
+ #
33
+ # @param [String] application_id
34
+ # @param [Integer] iat
35
+ # @param [SecureRandom::UUIDv4] jti
36
+ # @param [Integer] nbf
37
+ # @param [Integer] exp
38
+ # @param [Hash] paths
39
+ # @param [String] sub
40
+ # @param [String, OpenSSL::PKey::RSA] private_key
41
+ #
42
+ # @return [String]
43
+ #
44
+ attr_accessor :application_id, :private_key, :jti, :nbf, :ttl, :exp, :alg, :paths, :subject, :additional_claims, :jwt
45
+
46
+ def initialize(params = {})
47
+ Vonage::JWTBuilder.validate_parameters_not_conflicting(params)
48
+
49
+ @application_id = set_application_id(params.fetch(:application_id))
50
+ @private_key = set_private_key(params.fetch(:private_key))
51
+ @jti = params.fetch(:jti, SecureRandom.uuid)
52
+ @nbf = params.fetch(:nbf, nil)
53
+ @ttl = params.fetch(:ttl, 900)
54
+ @exp = params.fetch(:exp, nil)
55
+ @alg = params.fetch(:alg, 'RS256')
56
+ @paths = params.fetch(:paths, nil)
57
+ @subject = params.fetch(:subject, 'Subject')
58
+ @additional_claims = set_additional_claims(params)
59
+ @jwt = Vonage::JWT.new(generator: self)
60
+
61
+ after_initialize!(self)
62
+ end
63
+
64
+ def self.validate_parameters_not_conflicting(params)
65
+ return unless params[:ttl] && params[:exp]
66
+
67
+ raise ArgumentError, "Expected either 'ttl' or 'exp' parameter, preference is to set 'ttl' parameter"
68
+ end
69
+
70
+ def after_initialize!(builder)
71
+ validate_not_before(builder.nbf) if builder.nbf
72
+ validate_time_to_live(builder.ttl)
73
+ validate_paths(builder.paths) if builder.paths
74
+ validate_subject(builder.subject) if builder.subject
75
+ end
76
+
77
+ def set_application_id(application_id)
78
+ validate_application_id(application_id)
79
+
80
+ application_id
81
+ end
82
+
83
+ def set_private_key(private_key)
84
+ validate_private_key(private_key)
85
+
86
+ if File.exist?(private_key)
87
+ OpenSSL::PKey::RSA.new(File.read(private_key))
88
+ else
89
+ OpenSSL::PKey::RSA.new(private_key)
90
+ end
91
+ end
92
+
93
+ def set_additional_claims(params)
94
+ params.delete_if {|k,v| [:application_id, :private_key, :jti, :nbf, :ttl, :exp, :alg, :paths, :subject].include?(k) }
95
+ end
96
+
97
+ def set_exp
98
+ Time.now.to_i
99
+ end
100
+
101
+ def validate_application_id(application_id)
102
+ raise ArgumentError, "Missing required 'application_id' parameter" if application_id.nil?
103
+ end
104
+
105
+ def validate_private_key(private_key)
106
+ raise ArgumentError, "Missing required 'private_key' parameter" if private_key.nil?
107
+ end
108
+
109
+ def validate_not_before(nbf)
110
+ raise ArgumentError, "Expected Integer parameter type for NotBefore 'nbf' parameter" unless nbf.is_a?(Integer)
111
+ end
112
+
113
+ def validate_time_to_live(ttl)
114
+ raise ArgumentError, "Expected Integer parameter type for TimeToLive 'ttl' parameter" unless ttl.is_a?(Integer)
115
+ end
116
+
117
+ def validate_paths(acl_paths)
118
+ raise ArgumentError, "Expected Hash parameter type for Paths 'paths' parameter" unless acl_paths.is_a?(Hash)
119
+ end
120
+
121
+ def validate_subject(subject)
122
+ raise ArgumentError, "Expected String parameter type for Subject 'subject' parameter" unless subject.is_a?(String)
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vonage
4
+ class JWT
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
data/lib/vonage-jwt.rb ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'vonage-jwt/jwt_builder'
4
+
5
+ module Vonage
6
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path('lib/vonage-jwt/version', File.dirname(__FILE__))
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'vonage-jwt'
7
+ s.version = Vonage::JWT::VERSION
8
+ s.license = 'MIT'
9
+ s.platform = Gem::Platform::RUBY
10
+ s.authors = ['Vonage']
11
+ s.email = ['devrel@vonage.com']
12
+ s.homepage = 'https://github.com/Vonage/vonage-jwt-ruby'
13
+ s.description = 'Vonage JWT Generator for Ruby'
14
+ s.summary = 'This is the Ruby client library to generate Vonage JSON Web Tokens (JWTs).'
15
+ s.files = Dir.glob('lib/**/*.rb') + %w[LICENSE.txt README.md vonage-jwt.gemspec]
16
+ s.required_ruby_version = '>= 2.7.0'
17
+ s.add_dependency('jwt', '~> 2')
18
+ s.require_path = 'lib'
19
+ s.metadata = {
20
+ 'homepage' => 'https://github.com/Vonage/vonage-jwt-ruby',
21
+ 'source_code_uri' => 'https://github.com/Vonage/vonage-jwt-ruby',
22
+ 'bug_tracker_uri' => 'https://github.com/Vonage/vonage-jwt-ruby/issues',
23
+ 'changelog_uri' => 'https://github.com/Vonage/vonage-jwt-ruby/blob/main/CHANGES.md',
24
+ 'documentation_uri' => 'https://rubydoc.info/github/Vonage/vonage-jwt-ruby'
25
+ }
26
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vonage-jwt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vonage
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-11-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jwt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ description: Vonage JWT Generator for Ruby
28
+ email:
29
+ - devrel@vonage.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE.txt
35
+ - README.md
36
+ - lib/vonage-jwt.rb
37
+ - lib/vonage-jwt/jwt.rb
38
+ - lib/vonage-jwt/jwt_builder.rb
39
+ - lib/vonage-jwt/version.rb
40
+ - vonage-jwt.gemspec
41
+ homepage: https://github.com/Vonage/vonage-jwt-ruby
42
+ licenses:
43
+ - MIT
44
+ metadata:
45
+ homepage: https://github.com/Vonage/vonage-jwt-ruby
46
+ source_code_uri: https://github.com/Vonage/vonage-jwt-ruby
47
+ bug_tracker_uri: https://github.com/Vonage/vonage-jwt-ruby/issues
48
+ changelog_uri: https://github.com/Vonage/vonage-jwt-ruby/blob/main/CHANGES.md
49
+ documentation_uri: https://rubydoc.info/github/Vonage/vonage-jwt-ruby
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 2.7.0
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubygems_version: 3.2.3
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: This is the Ruby client library to generate Vonage JSON Web Tokens (JWTs).
69
+ test_files: []