Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
Merge bf4919c into ba427cf
Browse files Browse the repository at this point in the history
  • Loading branch information
hummusonrails committed Jul 20, 2020
2 parents ba427cf + bf4919c commit fb64b1e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 34 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,19 @@ the token option. For example:
```ruby
claims = {
application_id: application_id,
private_key: 'path/to/private.key',
nbf: 1483315200,
exp: 1514764800,
iat: 1483228800
ttl: 800
}

private_key = File.read('path/to/private.key')

token = Nexmo::JWT.generate(claims, private_key)
token = Nexmo::JWT.generate(claims)

client = Nexmo::Client.new(token: token)
````

Documentation for the Nexmo Ruby JWT generator gem can be found at
[https://www.rubydoc.info/github/nexmo/nexmo-jwt-ruby](https://www.rubydoc.info/github/nexmo/nexmo-jwt-ruby).
The documentation outlines all the possible parameters you can use to customize and build a token with.

## Webhook signatures

Expand Down
28 changes: 11 additions & 17 deletions lib/nexmo/jwt.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# typed: strict
# typed: false
# frozen_string_literal: true
require 'securerandom'
require 'openssl'
require 'jwt'
require 'nexmo-jwt'

module Nexmo
module JWT
extend T::Sig

class JWT
# Generate an encoded JSON Web Token.
#
# By default the Nexmo Ruby SDK generates a short lived JWT per request.
Expand All @@ -16,12 +14,14 @@ module JWT
# directly call {Nexmo::JWT.generate} to generate a token, and set the token
# attribute on the client object.
#
# Documentation for the Nexmo Ruby JWT generator gem can be found at
# https://www.rubydoc.info/github/nexmo/nexmo-jwt-ruby
#
# @example
# claims = {
# application_id: application_id,
# nbf: 1483315200,
# exp: 1514764800,
# iat: 1483228800
# ttl: 800,
# subject: 'My_Subject'
# }
#
# private_key = File.read('path/to/private.key')
Expand All @@ -33,15 +33,9 @@ module JWT
#
# @return [String]
#
sig { params(payload: T::Hash[T.any(Symbol, String), T.any(String, Integer)], private_key: T.any(OpenSSL::PKey::RSA, String)).returns(String) }
def self.generate(payload, private_key)
payload[:iat] = iat = Time.now.to_i unless payload.key?(:iat) || payload.key?('iat')
payload[:exp] = T.must(iat) + 60 unless payload.key?(:exp) || payload.key?('exp')
payload[:jti] = SecureRandom.uuid unless payload.key?(:jti) || payload.key?('jti')

private_key = OpenSSL::PKey::RSA.new(private_key) unless private_key.respond_to?(:sign)

::JWT.encode(payload, private_key, 'RS256')
def self.generate(payload, private_key = nil)
payload[:private_key] = private_key unless payload[:private_key]
@token = Nexmo::JWTBuilder.new(payload).jwt.generate
end
end
end
2 changes: 1 addition & 1 deletion nexmo.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Gem::Specification.new do |s|
s.summary = 'This is the Ruby client library for Nexmo\'s API. To use it you\'ll need a Nexmo account. Sign up for free at https://www.nexmo.com'
s.files = Dir.glob('lib/**/*.rb') + %w(LICENSE.txt README.md nexmo.gemspec)
s.required_ruby_version = '>= 2.5.0'
s.add_dependency('jwt', '~> 2')
s.add_dependency('nexmo-jwt', '~> 0.1')
s.add_dependency('zeitwerk', '~> 2', '>= 2.2')
s.add_dependency('sorbet-runtime', '~> 0.5')
s.require_path = 'lib'
Expand Down
26 changes: 15 additions & 11 deletions test/nexmo/jwt_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class Nexmo::JWTTest < Minitest::Test
def private_key
@private_key ||= OpenSSL::PKey::RSA.new(1024)
@private_key ||= File.read('test/private_key.txt')
end

def application_id
Expand All @@ -15,38 +15,42 @@ def uuid_pattern
end

def decode(token)
JWT.decode(token, private_key, _verify=true, {algorithm: 'RS256'}).first
JWT.decode(token, private_key, false, {algorithm: 'RS256'}).first
end

def test_generate_method_returns_payload_encoded_with_private_key
time = Time.now.to_i

payload = {
'application_id' => application_id,
'iat' => time,
'exp' => time + 3600,
'jti' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
:application_id => application_id,
:private_key => private_key,
:iat => time,
:exp => time + 3600,
:jti => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
}

token = Nexmo::JWT.generate(payload, private_key)
token = Nexmo::JWT.generate(payload)
decoded = decode(token)

assert_equal decode(token), payload
assert_equal decoded.fetch('application_id'), payload.fetch(:application_id)
assert_equal decoded.fetch('iat'), payload.fetch(:iat)
assert_equal decoded.fetch('jti'), payload.fetch(:jti)
end

def test_generate_method_sets_default_value_for_iat_parameter
token = Nexmo::JWT.generate({}, private_key)
token = Nexmo::JWT.generate({ :application_id => application_id }, private_key)

assert_kind_of Integer, decode(token).fetch('iat')
end

def test_generate_method_sets_default_value_for_exp_parameter
token = Nexmo::JWT.generate({}, private_key)
token = Nexmo::JWT.generate({ :application_id => application_id }, private_key)

assert_kind_of Integer, decode(token).fetch('exp')
end

def test_generate_method_sets_default_value_for_jti_parameter
token = Nexmo::JWT.generate({}, private_key)
token = Nexmo::JWT.generate({ :application_id => application_id }, private_key)

assert_match uuid_pattern, decode(token).fetch('jti')
end
Expand Down

0 comments on commit fb64b1e

Please sign in to comment.