nexmo-jwt 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8810bf15b38858eab28165f9fe3e206cac5f3ed14bbe775b8f1086c440cd48aa
4
+ data.tar.gz: a2edfdeba6628732c4e4c9fbdc8ececdb4b0b0029e5c2dfcd9a577ab8bc64922
5
+ SHA512:
6
+ metadata.gz: 0cd2beaf48a5a9ea952e67decb88381f096133be034570004151220bf7a642f2cb69618cbb7aa4e8315868b381c06baf17f1ab3e216c5a2cd907175b3f362a0d
7
+ data.tar.gz: 348c52c22c21ea98c379128e1062b6ce55a3c6c3b031cf9a34afd88147ba7d14ce4e5da86362faea083629f0c05ebea64602f243ea077fcf05c1d525818acdfb
@@ -0,0 +1,24 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016-2020 Nexmo Inc
4
+
5
+ Permission is hereby granted, free of charge, to any person
6
+ obtaining a copy of this software and associated documentation
7
+ files (the "Software"), to deal in the Software without
8
+ restriction, including without limitation the rights to use,
9
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the
11
+ Software is furnished to do so, subject to the following
12
+ conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,80 @@
1
+ # Nexmo JWT Generator for Ruby
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/nexmo-jwt.svg)](https://badge.fury.io/rb/nexmo-jwt) [![Build Status](https://api.travis-ci.org/Nexmo/nexmo-jwt-ruby.svg?branch=master)](https://travis-ci.org/Nexmo/nexmo-jwt-ruby) [![Coverage Status](https://coveralls.io/repos/github/Nexmo/nexmo-jwt-ruby/badge.svg?branch=coveralls)](https://coveralls.io/github/Nexmo/nexmo-jwt-ruby?branch=master)[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg)](CODE_OF_CONDUCT.md)
4
+
5
+ <img src="https://developer.nexmo.com/assets/images/Vonage_Nexmo.svg" height="48px" alt="Nexmo is now known as Vonage" />
6
+
7
+ This is the Ruby library to generate Nexmo JSON Web Tokens (JWTs). To use it you'll
8
+ need a Nexmo account. Sign up [for free at nexmo.com][signup].
9
+
10
+ * [Requirements](#requirements)
11
+ * [Installation](#installation)
12
+ * [Usage](#usage)
13
+ * [Documentation](#documentation)
14
+ * [License](#license)
15
+
16
+ ## Requirements
17
+
18
+ The JWT generator supports Ruby version 2.5 or newer.
19
+
20
+ ## Installation
21
+
22
+ To install the Ruby client library using Rubygems:
23
+
24
+ gem install nexmo-jwt
25
+
26
+ Alternatively you can clone the repository:
27
+
28
+ git clone git@github.com:Nexmo/nexmo-jwt-ruby.git
29
+
30
+ ## Usage
31
+
32
+ By default the Nexmo JWT generator creates a short lived JWT (15 minutes) per request.
33
+ To generate a long lived JWT for multiple requests, specify a longer value in the `exp`
34
+ parameter during initialization.
35
+
36
+ Example with no custom configuration:
37
+
38
+ ```ruby
39
+ @builder = Nexmo::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
40
+ @token = @builder.jwt.generate
41
+ ```
42
+
43
+ Example providing custom configuration options:
44
+
45
+ ```ruby
46
+ @builder = Nexmo::JWTBuilder.new(
47
+ application_id: YOUR_APPLICATION_ID,
48
+ private_key: YOUR_PRIVATE_KEY,
49
+ ttl: 500,
50
+ paths: {
51
+ "acl": {
52
+ "paths": {
53
+ "/messages": {
54
+ "methods": ["POST", "GET"],
55
+ "filters": {
56
+ "from": "447977271009"
57
+ }
58
+ }
59
+ }
60
+ }
61
+ },
62
+ subject: 'My_Custom_Subject'
63
+ )
64
+ @token = @builder.jwt.generate
65
+ ```
66
+
67
+ ## Documentation
68
+
69
+ Nexmo Ruby JWT documentation: https://www.rubydoc.info/github/nexmo/nexmo-jwt
70
+
71
+ Nexmo Ruby code examples: https://github.com/Nexmo/nexmo-ruby-code-snippets
72
+
73
+ Nexmo API reference: https://developer.nexmo.com/api
74
+
75
+ ## License
76
+
77
+ This library is released under the [MIT License][license]
78
+
79
+ [signup]: https://dashboard.nexmo.com/sign-up?utm_source=DEV_REL&utm_medium=github&utm_campaign=ruby-client-library
80
+ [license]: LICENSE.txt
@@ -0,0 +1,4 @@
1
+ require_relative 'nexmo-jwt/jwt_builder'
2
+
3
+ module Nexmo
4
+ end
@@ -0,0 +1,31 @@
1
+ require 'jwt'
2
+
3
+ module Nexmo
4
+ class JWT
5
+ attr_accessor :generator, :typ, :iat
6
+
7
+ def initialize(params = {})
8
+ @generator = params.fetch(:generator)
9
+ @typ = params.fetch(:typ, 'JWT')
10
+ @iat = params.fetch(:iat, Time.now.to_i)
11
+ end
12
+
13
+ def generate
14
+ ::JWT.encode(to_payload, generator.private_key, generator.alg)
15
+ end
16
+
17
+ def to_payload
18
+ hash = {
19
+ iat: iat,
20
+ jti: generator.jti,
21
+ exp: generator.exp + generator.ttl,
22
+ sub: generator.subject,
23
+ application_id: generator.application_id,
24
+ typ: typ
25
+ }
26
+ hash.merge!(paths: generator.paths) if generator.paths
27
+ hash.merge!(nbf: generator.nbf) if generator.nbf
28
+ hash
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,112 @@
1
+ require 'openssl'
2
+ require 'securerandom'
3
+ require_relative 'jwt'
4
+
5
+ module Nexmo
6
+ class JWTBuilder
7
+ # Generate an encoded JSON Web Token.
8
+ #
9
+ # By default the Nexmo JWT generator creates a short lived (15 minutes) JWT per request.
10
+ #
11
+ # To generate a long lived JWT for multiple requests, specify a longer value in the `exp`
12
+ # parameter during initialization.
13
+ #
14
+ # Example with no custom configuration:
15
+ #
16
+ # @example
17
+ # @builder = Nexmo::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
18
+ # @token = @builder.jwt.generate
19
+ #
20
+ # Example providing custom configuration options:
21
+ #
22
+ # @example
23
+ # @generator = Nexmo::JWTBuilder.new(
24
+ # application_id: YOUR_APPLICATION_ID,
25
+ # private_key: YOUR_PRIVATE_KEY,
26
+ # ttl: 500,
27
+ # subject: 'My_Custom_Subject'
28
+ # )
29
+ # @token = @builder.jwt.generate
30
+ #
31
+ # @param [String] application_id
32
+ # @param [Integer] iat
33
+ # @param [SecureRandom::UUIDv4] jti
34
+ # @param [Integer] nbf
35
+ # @param [Integer] exp
36
+ # @param [Hash] paths
37
+ # @param [String] sub
38
+ # @param [String, OpenSSL::PKey::RSA] private_key
39
+ #
40
+ # @return [String]
41
+ #
42
+ attr_accessor :application_id, :private_key, :jti, :nbf, :ttl, :exp, :alg, :paths, :subject, :jwt
43
+
44
+ def initialize(params = {})
45
+ @application_id = set_application_id(params.fetch(:application_id))
46
+ @private_key = set_private_key(params.fetch(:private_key))
47
+ @jti = params.fetch(:jti, SecureRandom.uuid)
48
+ @nbf = params.fetch(:nbf, nil)
49
+ @ttl = params.fetch(:ttl, 900)
50
+ @exp = params.fetch(:exp, 0)
51
+ @alg = params.fetch(:alg, 'RS256')
52
+ @paths = params.fetch(:paths, nil)
53
+ @subject = params.fetch(:subject, 'Subject')
54
+ @jwt = Nexmo::JWT.new(:generator => self)
55
+
56
+ after_initialize!(self)
57
+ end
58
+
59
+ def after_initialize!(builder)
60
+ validate_not_before(builder.nbf) if builder.nbf
61
+ validate_time_to_live(builder.ttl)
62
+ validate_paths(builder.paths) if builder.paths
63
+ validate_subject(builder.subject) if builder.subject
64
+ end
65
+
66
+ def set_application_id(application_id)
67
+ validate_application_id(application_id)
68
+
69
+ application_id
70
+ end
71
+
72
+ def set_private_key(private_key)
73
+ validate_private_key(private_key)
74
+
75
+ if File.exist?(private_key)
76
+ key = OpenSSL::PKey::RSA.new(File.read(private_key))
77
+ else
78
+ key = OpenSSL::PKey::RSA.new(private_key)
79
+ end
80
+
81
+ key
82
+ end
83
+
84
+ def set_exp
85
+ Time.now.to_i
86
+ end
87
+
88
+ def validate_application_id(application_id)
89
+ raise ArgumentError, "Missing required 'application_id' parameter" if application_id.nil?
90
+ end
91
+
92
+ def validate_private_key(private_key)
93
+ raise ArgumentError, "Missing required 'private_key' parameter" if private_key.nil?
94
+ end
95
+
96
+ def validate_not_before(nbf)
97
+ raise ArgumentError, "Expected Integer parameter type for NotBefore 'nbf' parameter" unless nbf.is_a?(Integer)
98
+ end
99
+
100
+ def validate_time_to_live(ttl)
101
+ raise ArgumentError, "Expected Integer parameter type for TimeToLive 'ttl' parameter" unless ttl.is_a?(Integer)
102
+ end
103
+
104
+ def validate_paths(acl_paths)
105
+ raise ArgumentError, "Expected Hash parameter type for Paths 'paths' parameter" unless acl_paths.is_a?(Hash)
106
+ end
107
+
108
+ def validate_subject(subject)
109
+ raise ArgumentError, "Expected String parameter type for Subject 'subject' parameter" unless subject.is_a?(String)
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,5 @@
1
+ module Nexmo
2
+ class JWT
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ require File.expand_path('lib/nexmo-jwt/version', File.dirname(__FILE__))
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'nexmo-jwt'
5
+ s.version = Nexmo::JWT::VERSION
6
+ s.license = 'MIT'
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ['Nexmo']
9
+ s.email = ['devrel@nexmo.com']
10
+ s.homepage = 'https://github.com/Nexmo/nexmo-jwt-ruby'
11
+ s.description = 'Nexmo JWT Generator for Ruby'
12
+ s.summary = 'This is the Ruby client library to generate Nexmo JSON Web Tokens (JWTs).'
13
+ s.files = Dir.glob('lib/**/*.rb') + %w(LICENSE.txt README.md nexmo-jwt.gemspec)
14
+ s.required_ruby_version = '>= 2.5.0'
15
+ s.add_dependency('jwt', '~> 2')
16
+ s.require_path = 'lib'
17
+ s.metadata = {
18
+ 'homepage' => 'https://github.com/Nexmo/nexmo-jwt-ruby',
19
+ 'source_code_uri' => 'https://github.com/Nexmo/nexmo-jwt-ruby',
20
+ 'bug_tracker_uri' => 'https://github.com/Nexmo/nexmo-jwt-ruby/issues',
21
+ 'changelog_uri' => 'https://github.com/Nexmo/nexmo-jwt-ruby/blob/master/CHANGES.md',
22
+ 'documentation_uri' => 'https://www.rubydoc.info/github/nexmo/nexmo-jwt'
23
+ }
24
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nexmo-jwt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nexmo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-16 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: Nexmo JWT Generator for Ruby
28
+ email:
29
+ - devrel@nexmo.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE.txt
35
+ - README.md
36
+ - lib/nexmo-jwt.rb
37
+ - lib/nexmo-jwt/jwt.rb
38
+ - lib/nexmo-jwt/jwt_builder.rb
39
+ - lib/nexmo-jwt/version.rb
40
+ - nexmo-jwt.gemspec
41
+ homepage: https://github.com/Nexmo/nexmo-jwt-ruby
42
+ licenses:
43
+ - MIT
44
+ metadata:
45
+ homepage: https://github.com/Nexmo/nexmo-jwt-ruby
46
+ source_code_uri: https://github.com/Nexmo/nexmo-jwt-ruby
47
+ bug_tracker_uri: https://github.com/Nexmo/nexmo-jwt-ruby/issues
48
+ changelog_uri: https://github.com/Nexmo/nexmo-jwt-ruby/blob/master/CHANGES.md
49
+ documentation_uri: https://www.rubydoc.info/github/nexmo/nexmo-jwt
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.5.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.0.0
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: This is the Ruby client library to generate Nexmo JSON Web Tokens (JWTs).
69
+ test_files: []