diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 70baddf5..761134bb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,7 @@ jobs: strategy: matrix: os: [ubuntu-latest, windows-latest, macos-latest] - ruby: [3.0, 3.1, 3.2, 3.3] + ruby: [2.5, 2.6, 2.7, 3.0] exclude: - os: windows-latest ruby: 3.0 diff --git a/CHANGES.md b/CHANGES.md index ec61605f..c25ca9d5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,7 +1,3 @@ -# 4.10.0 - -* Updating client token creation to use JWTs by default. See [#274](https://github.com/opentok/OpenTok-Ruby-SDK/pull/274) - # 4.9.0 * Adds the `publisheronly` role for client token creation. See [#272](https://github.com/opentok/OpenTok-Ruby-SDK/pull/272) diff --git a/Gemfile b/Gemfile index 6a24022d..57fae0ce 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,4 @@ source "http://rubygems.org" -gem "pry" - # Specify your gem's dependencies in opentok.gemspec gemspec diff --git a/lib/opentok/opentok.rb b/lib/opentok/opentok.rb index ae347845..d47e5aa7 100644 --- a/lib/opentok/opentok.rb +++ b/lib/opentok/opentok.rb @@ -36,7 +36,6 @@ module OpenTok # the token. # # @param [Hash] options A hash defining options for the token. - # @option options [String] :token_type The type of token to generate. Must be one of 'T1' or 'JWT'. 'JWT' is the default. # @option options [Symbol] :role The role for the token. Set this to one of the following # values: # * :subscriber -- A subscriber can only subscribe to streams. diff --git a/lib/opentok/session.rb b/lib/opentok/session.rb index fba1475c..4bd7fd55 100644 --- a/lib/opentok/session.rb +++ b/lib/opentok/session.rb @@ -28,7 +28,6 @@ module OpenTok # Generates a token. # # @param [Hash] options - # @option options [String] :token_type The type of token to generate. Must be one of 'T1' or 'JWT'. 'JWT' is the default. # @option options [Symbol] :role The role for the token. Set this to one of the following # values: # * :subscriber -- A subscriber can only subscribe to streams. diff --git a/lib/opentok/token_generator.rb b/lib/opentok/token_generator.rb index 1720099a..a5cbee25 100644 --- a/lib/opentok/token_generator.rb +++ b/lib/opentok/token_generator.rb @@ -6,13 +6,10 @@ require "openssl" require "active_support" require "active_support/time" -require "jwt" module OpenTok # @private module TokenGenerator - VALID_TOKEN_TYPES = ['T1', 'JWT'].freeze - # this works when using include TokenGenerator def self.included(base) base.extend(ClassMethods) @@ -36,14 +33,7 @@ def generates_tokens(arg_lambdas={}) end dynamic_args.compact! args = args.first(4-dynamic_args.length) - token_type = if args.any? && args.last.is_a?(Hash) && args.last.has_key?(:token_type) - args.last[:token_type].upcase - else - "JWT" - end - raise "'#{token_type}' is not a valid token type. Must be one of: #{VALID_TOKEN_TYPES.join(', ')}" unless VALID_TOKEN_TYPES.include? token_type - - self.class.generate_token(token_type).call(*dynamic_args, *args) + self.class.generate_token.call(*dynamic_args, *args) end end @@ -53,14 +43,14 @@ def arg_lambdas end # Generates a token - def generate_token(token_type) - token_type == 'T1' ? TokenGenerator::GENERATE_T1_TOKEN_LAMBDA : TokenGenerator::GENERATE_JWT_LAMBDA + def generate_token + TokenGenerator::GENERATE_TOKEN_LAMBDA end end # @private TODO: this probably doesn't need to be a constant anyone can read - GENERATE_T1_TOKEN_LAMBDA = ->(api_key, api_secret, session_id, opts = {}) do + GENERATE_TOKEN_LAMBDA = ->(api_key, api_secret, session_id, opts = {}) do # normalize required data params role = opts.fetch(:role, :publisher) unless ROLES.has_key? role @@ -111,53 +101,6 @@ def generate_token(token_type) TOKEN_SENTINEL + Base64.strict_encode64(meta_string + ":" + data_string) end - GENERATE_JWT_LAMBDA = ->(api_key, api_secret, session_id, opts = {}) do - # normalize required data params - role = opts.fetch(:role, :publisher) - unless ROLES.has_key? role - raise "'#{role}' is not a recognized role" - end - unless Session.belongs_to_api_key? session_id.to_s, api_key - raise "Cannot generate token for a session_id that doesn't belong to api_key: #{api_key}" - end - - # minimum data params - data_params = { - :iss => api_key, - :ist => "project", - :iat => Time.now.to_i, - :exp => Time.now.to_i + 86400, - :nonce => Random.rand, - :role => role, - :scope => "session.connect", - :session_id => session_id, - } - - # normalize and add additional data params - unless (expire_time = opts[:expire_time].to_i) == 0 - unless expire_time.between?(Time.now.to_i, (Time.now + 30.days).to_i) - raise "Expire time must be within the next 30 days" - end - data_params[:exp] = expire_time.to_i - end - - unless opts[:data].nil? - unless (data = opts[:data].to_s).length < 1000 - raise "Connection data must be less than 1000 characters" - end - data_params[:connection_data] = data - end - - if opts[:initial_layout_class_list] - if opts[:initial_layout_class_list].is_a?(Array) - data_params[:initial_layout_class_list] = opts[:initial_layout_class_list].join(' ') - else - data_params[:initial_layout_class_list] = opts[:initial_layout_class_list].to_s - end - end - - JWT.encode(data_params, api_secret, 'HS256', header_fields={typ: 'JWT'}) - end # this works when using extend TokenGenerator # def generates_tokens(method_opts) diff --git a/lib/opentok/version.rb b/lib/opentok/version.rb index b9ce99e1..fcbe1618 100644 --- a/lib/opentok/version.rb +++ b/lib/opentok/version.rb @@ -1,4 +1,4 @@ module OpenTok # @private - VERSION = '4.10.0' + VERSION = '4.9.0' end diff --git a/spec/matchers/token.rb b/spec/matchers/token.rb index eed8f86d..0906697e 100644 --- a/spec/matchers/token.rb +++ b/spec/matchers/token.rb @@ -3,9 +3,8 @@ require "base64" require "openssl" require "addressable/uri" -require "jwt" -RSpec::Matchers.define :carry_t1_token_data do |input_data| +RSpec::Matchers.define :carry_token_data do |input_data| option_to_token_key = { :api_key => :partner_id, :data => :connection_data, @@ -38,7 +37,7 @@ end end -RSpec::Matchers.define :carry_valid_t1_token_signature do |api_secret| +RSpec::Matchers.define :carry_valid_token_signature do |api_secret| match do |token| decoded_token = Base64.decode64(token[4..token.length]) metadata, data_string = decoded_token.split(':') @@ -49,34 +48,3 @@ signature == OpenSSL::HMAC.hexdigest(digest, api_secret, data_string) end end - -RSpec::Matchers.define :carry_jwt_token_data do |input_data| - match do |token| - decoded_token = JWT.decode(token, nil, false) - token_data = decoded_token.first.transform_keys {|k| k.to_sym}.transform_values {|v| v.to_s} - check_token_data = lambda { |key, value| - if token_data.has_key? key - unless value.nil? - return token_data[key] == value.to_s - end - return true - end - false - } - unless input_data.respond_to? :all? - return check_token_data.call(input_data, nil) - end - input_data.all? { |k, v| check_token_data.call(k, v) } - end -end - -RSpec::Matchers.define :carry_valid_jwt_token_signature do |api_secret| - match do |token| - begin - JWT.decode(token, api_secret, true) - rescue - return false - end - true - end -end diff --git a/spec/opentok/session_spec.rb b/spec/opentok/session_spec.rb index b6a6c9fc..628c50e8 100644 --- a/spec/opentok/session_spec.rb +++ b/spec/opentok/session_spec.rb @@ -72,4 +72,5 @@ end include_examples "session generates tokens" end + end diff --git a/spec/shared/opentok_generates_tokens.rb b/spec/shared/opentok_generates_tokens.rb index 277756ab..a96185bc 100644 --- a/spec/shared/opentok_generates_tokens.rb +++ b/spec/shared/opentok_generates_tokens.rb @@ -15,284 +15,124 @@ let(:api_secret) { "1234567890abcdef1234567890abcdef1234567890" } let(:session_id) { "1_MX4xMjM0NTZ-flNhdCBNYXIgMTUgMTQ6NDI6MjMgUERUIDIwMTR-MC40OTAxMzAyNX4" } let(:default_role) { :publisher } - let(:ist) { "project" } - let(:scope) { "session.connect" } - context "when token type is T1" do - it "generates plain tokens" do - plain_token = opentok.generate_token session_id, :token_type => "T1" - expect(plain_token).to be_an_instance_of String - expect(plain_token).to carry_t1_token_data :session_id => session_id - expect(plain_token).to carry_t1_token_data :api_key => api_key - expect(plain_token).to carry_t1_token_data :role => default_role - expect(plain_token).to carry_t1_token_data [:nonce, :create_time] - expect(plain_token).to carry_valid_t1_token_signature api_secret - end - - it "generates tokens with an expire time" do - expire_time = Time.now + (60*60*24) - expiring_token = opentok.generate_token session_id, :expire_time => expire_time, :token_type => "T1" - expect(expiring_token).to be_an_instance_of String - expect(expiring_token).to carry_t1_token_data :session_id => session_id - expect(expiring_token).to carry_t1_token_data :api_key => api_key - expect(expiring_token).to carry_t1_token_data :role => default_role - expect(expiring_token).to carry_t1_token_data :expire_time => expire_time.to_i - expect(expiring_token).to carry_t1_token_data [:nonce, :create_time] - expect(expiring_token).to carry_valid_t1_token_signature api_secret - end - - it "generates tokens with an integer expire time" do - expire_time = Time.now.to_i + (60*60*24) - expiring_token = opentok.generate_token session_id, :expire_time => expire_time, :token_type => "T1" - expect(expiring_token).to be_an_instance_of String - expect(expiring_token).to carry_t1_token_data :session_id => session_id - expect(expiring_token).to carry_t1_token_data :api_key => api_key - expect(expiring_token).to carry_t1_token_data :role => default_role - expect(expiring_token).to carry_t1_token_data :expire_time => expire_time - expect(expiring_token).to carry_t1_token_data [:nonce, :create_time] - expect(expiring_token).to carry_valid_t1_token_signature api_secret - end - - it "generates tokens with a publisher role" do - role = :publisher - role_token = opentok.generate_token session_id, :role => role, :token_type => "T1" - expect(role_token).to be_an_instance_of String - expect(role_token).to carry_t1_token_data :session_id => session_id - expect(role_token).to carry_t1_token_data :api_key => api_key - expect(role_token).to carry_t1_token_data :role => role - expect(role_token).to carry_t1_token_data [:nonce, :create_time] - expect(role_token).to carry_valid_t1_token_signature api_secret - end - - it "generates tokens with a subscriber role" do - role = :subscriber - role_token = opentok.generate_token session_id, :role => role, :token_type => "T1" - expect(role_token).to be_an_instance_of String - expect(role_token).to carry_t1_token_data :session_id => session_id - expect(role_token).to carry_t1_token_data :api_key => api_key - expect(role_token).to carry_t1_token_data :role => role - expect(role_token).to carry_t1_token_data [:nonce, :create_time] - expect(role_token).to carry_valid_t1_token_signature api_secret - end - - it "generates tokens with a moderator role" do - role = :moderator - role_token = opentok.generate_token session_id, :role => role, :token_type => "T1" - expect(role_token).to be_an_instance_of String - expect(role_token).to carry_t1_token_data :session_id => session_id - expect(role_token).to carry_t1_token_data :api_key => api_key - expect(role_token).to carry_t1_token_data :role => role - expect(role_token).to carry_t1_token_data [:nonce, :create_time] - expect(role_token).to carry_valid_t1_token_signature api_secret - end - - it "generates tokens with a publisheronly role" do - role = :publisheronly - role_token = opentok.generate_token session_id, :role => role, :token_type => "T1" - expect(role_token).to be_an_instance_of String - expect(role_token).to carry_t1_token_data :session_id => session_id - expect(role_token).to carry_t1_token_data :api_key => api_key - expect(role_token).to carry_t1_token_data :role => role - expect(role_token).to carry_t1_token_data [:nonce, :create_time] - expect(role_token).to carry_valid_t1_token_signature api_secret - end - - it "generates tokens with data" do - data = "name=Johnny" - data_bearing_token = opentok.generate_token session_id, :data => data, :token_type => "T1" - expect(data_bearing_token).to be_an_instance_of String - expect(data_bearing_token).to carry_t1_token_data :session_id => session_id - expect(data_bearing_token).to carry_t1_token_data :api_key => api_key - expect(data_bearing_token).to carry_t1_token_data :role => default_role - expect(data_bearing_token).to carry_t1_token_data :data => data - expect(data_bearing_token).to carry_t1_token_data [:nonce, :create_time] - expect(data_bearing_token).to carry_valid_t1_token_signature api_secret - end - - it "generates tokens with initial layout classes" do - layout_classes = ["focus", "small"] - layout_class_bearing_token = opentok.generate_token session_id, :initial_layout_class_list => layout_classes, :token_type => "T1" - expect(layout_class_bearing_token).to be_an_instance_of String - expect(layout_class_bearing_token).to carry_t1_token_data :session_id => session_id - expect(layout_class_bearing_token).to carry_t1_token_data :api_key => api_key - expect(layout_class_bearing_token).to carry_t1_token_data :role => default_role - expect(layout_class_bearing_token).to carry_t1_token_data :initial_layout_class_list => layout_classes.join(' ') - expect(layout_class_bearing_token).to carry_t1_token_data [:nonce, :create_time] - expect(layout_class_bearing_token).to carry_valid_t1_token_signature api_secret - end - - it "generates tokens with one initial layout class" do - layout_class = "focus" - layout_class_bearing_token = opentok.generate_token session_id, :initial_layout_class_list => layout_class, :token_type => "T1" - expect(layout_class_bearing_token).to be_an_instance_of String - expect(layout_class_bearing_token).to carry_t1_token_data :session_id => session_id - expect(layout_class_bearing_token).to carry_t1_token_data :api_key => api_key - expect(layout_class_bearing_token).to carry_t1_token_data :role => default_role - expect(layout_class_bearing_token).to carry_t1_token_data :initial_layout_class_list => layout_class - expect(layout_class_bearing_token).to carry_t1_token_data [:nonce, :create_time] - expect(layout_class_bearing_token).to carry_valid_t1_token_signature api_secret - end - - context "when the role is invalid" do - it "raises an error" do - expect { opentok.generate_token session_id, :role => :invalid_role, :token_type => "T1" }.to raise_error - end - end + it "generates plain tokens" do + plain_token = opentok.generate_token session_id + expect(plain_token).to be_an_instance_of String + expect(plain_token).to carry_token_data :session_id => session_id + expect(plain_token).to carry_token_data :api_key => api_key + expect(plain_token).to carry_token_data :role => default_role + expect(plain_token).to carry_token_data [:nonce, :create_time] + expect(plain_token).to carry_valid_token_signature api_secret end - context "when token type is JWT" do - it "generates plain tokens" do - plain_token = opentok.generate_token session_id, :token_type => "JWT" - expect(plain_token).to be_an_instance_of String - expect(plain_token).to carry_jwt_token_data :session_id => session_id - expect(plain_token).to carry_jwt_token_data :iss => api_key - expect(plain_token).to carry_jwt_token_data :ist => ist - expect(plain_token).to carry_jwt_token_data :scope => scope - expect(plain_token).to carry_jwt_token_data :role => default_role - expect(plain_token).to carry_jwt_token_data [:ist, :iat, :nonce] - expect(plain_token).to carry_valid_jwt_token_signature api_secret - end - - it "generates tokens with a custom expire time" do - expire_time = Time.now + (60*60*24) - expiring_token = opentok.generate_token session_id, :expire_time => expire_time, :token_type => "JWT" - expect(expiring_token).to be_an_instance_of String - expect(expiring_token).to carry_jwt_token_data :session_id => session_id - expect(expiring_token).to carry_jwt_token_data :iss => api_key - expect(expiring_token).to carry_jwt_token_data :ist => ist - expect(expiring_token).to carry_jwt_token_data :scope => scope - expect(expiring_token).to carry_jwt_token_data :role => default_role - expect(expiring_token).to carry_jwt_token_data :exp => expire_time.to_i - expect(expiring_token).to carry_jwt_token_data [:ist, :iat, :nonce] - expect(expiring_token).to carry_valid_jwt_token_signature api_secret - end - - it "generates tokens with an integer expire time" do - expire_time = Time.now.to_i + (60*60*24) - expiring_token = opentok.generate_token session_id, :expire_time => expire_time, :token_type => "JWT" - expect(expiring_token).to be_an_instance_of String - expect(expiring_token).to carry_jwt_token_data :session_id => session_id - expect(expiring_token).to carry_jwt_token_data :iss => api_key - expect(expiring_token).to carry_jwt_token_data :ist => ist - expect(expiring_token).to carry_jwt_token_data :scope => scope - expect(expiring_token).to carry_jwt_token_data :role => default_role - expect(expiring_token).to carry_jwt_token_data :exp => expire_time - expect(expiring_token).to carry_jwt_token_data [:ist, :iat, :nonce] - expect(expiring_token).to carry_valid_jwt_token_signature api_secret - end - - it "generates tokens with a publisher role" do - role = :publisher - role_token = opentok.generate_token session_id, :role => role, :token_type => "JWT" - expect(role_token).to be_an_instance_of String - expect(role_token).to carry_jwt_token_data :session_id => session_id - expect(role_token).to carry_jwt_token_data :iss => api_key - expect(role_token).to carry_jwt_token_data :ist => ist - expect(role_token).to carry_jwt_token_data :scope => scope - expect(role_token).to carry_jwt_token_data :role => role - expect(role_token).to carry_jwt_token_data [:ist, :iat, :nonce] - expect(role_token).to carry_valid_jwt_token_signature api_secret - end + it "generates tokens with an expire time" do + expire_time = Time.now + (60*60*24) + expiring_token = opentok.generate_token session_id, :expire_time => expire_time + expect(expiring_token).to be_an_instance_of String + expect(expiring_token).to carry_token_data :session_id => session_id + expect(expiring_token).to carry_token_data :api_key => api_key + expect(expiring_token).to carry_token_data :role => default_role + expect(expiring_token).to carry_token_data :expire_time => expire_time.to_i + expect(expiring_token).to carry_token_data [:nonce, :create_time] + expect(expiring_token).to carry_valid_token_signature api_secret + end - it "generates tokens with a subscriber role" do - role = :subscriber - role_token = opentok.generate_token session_id, :role => role, :token_type => "JWT" - expect(role_token).to be_an_instance_of String - expect(role_token).to carry_jwt_token_data :session_id => session_id - expect(role_token).to carry_jwt_token_data :iss => api_key - expect(role_token).to carry_jwt_token_data :ist => ist - expect(role_token).to carry_jwt_token_data :scope => scope - expect(role_token).to carry_jwt_token_data :role => role - expect(role_token).to carry_jwt_token_data [:ist, :iat, :nonce] - expect(role_token).to carry_valid_jwt_token_signature api_secret - end + it "generates tokens with an integer expire time" do + expire_time = Time.now.to_i + (60*60*24) + expiring_token = opentok.generate_token session_id, :expire_time => expire_time + expect(expiring_token).to be_an_instance_of String + expect(expiring_token).to carry_token_data :session_id => session_id + expect(expiring_token).to carry_token_data :api_key => api_key + expect(expiring_token).to carry_token_data :role => default_role + expect(expiring_token).to carry_token_data :expire_time => expire_time + expect(expiring_token).to carry_token_data [:nonce, :create_time] + expect(expiring_token).to carry_valid_token_signature api_secret + end - it "generates tokens with a moderator role" do - role = :moderator - role_token = opentok.generate_token session_id, :role => role, :token_type => "JWT" - expect(role_token).to be_an_instance_of String - expect(role_token).to carry_jwt_token_data :session_id => session_id - expect(role_token).to carry_jwt_token_data :iss => api_key - expect(role_token).to carry_jwt_token_data :ist => ist - expect(role_token).to carry_jwt_token_data :scope => scope - expect(role_token).to carry_jwt_token_data :role => role - expect(role_token).to carry_jwt_token_data [:ist, :iat, :nonce] - expect(role_token).to carry_valid_jwt_token_signature api_secret - end + it "generates tokens with a publisher role" do + role = :publisher + role_token = opentok.generate_token session_id, :role => role + expect(role_token).to be_an_instance_of String + expect(role_token).to carry_token_data :session_id => session_id + expect(role_token).to carry_token_data :api_key => api_key + expect(role_token).to carry_token_data :role => role + expect(role_token).to carry_token_data [:nonce, :create_time] + expect(role_token).to carry_valid_token_signature api_secret + end - it "generates tokens with a publisheronly role" do - role = :publisheronly - role_token = opentok.generate_token session_id, :role => role, :token_type => "JWT" - expect(role_token).to be_an_instance_of String - expect(role_token).to carry_jwt_token_data :session_id => session_id - expect(role_token).to carry_jwt_token_data :iss => api_key - expect(role_token).to carry_jwt_token_data :ist => ist - expect(role_token).to carry_jwt_token_data :scope => scope - expect(role_token).to carry_jwt_token_data :role => role - expect(role_token).to carry_jwt_token_data [:ist, :iat, :nonce] - expect(role_token).to carry_valid_jwt_token_signature api_secret - end + it "generates tokens with a subscriber role" do + role = :subscriber + role_token = opentok.generate_token session_id, :role => role + expect(role_token).to be_an_instance_of String + expect(role_token).to carry_token_data :session_id => session_id + expect(role_token).to carry_token_data :api_key => api_key + expect(role_token).to carry_token_data :role => role + expect(role_token).to carry_token_data [:nonce, :create_time] + expect(role_token).to carry_valid_token_signature api_secret + end - it "generates tokens with data" do - data = "name=Johnny" - data_bearing_token = opentok.generate_token session_id, :data => data, :token_type => "JWT" - expect(data_bearing_token).to be_an_instance_of String - expect(data_bearing_token).to carry_jwt_token_data :session_id => session_id - expect(data_bearing_token).to carry_jwt_token_data :iss => api_key - expect(data_bearing_token).to carry_jwt_token_data :ist => ist - expect(data_bearing_token).to carry_jwt_token_data :scope => scope - expect(data_bearing_token).to carry_jwt_token_data :role => default_role - expect(data_bearing_token).to carry_jwt_token_data :connection_data => data - expect(data_bearing_token).to carry_jwt_token_data [:ist, :iat, :nonce] - expect(data_bearing_token).to carry_valid_jwt_token_signature api_secret - end + it "generates tokens with a moderator role" do + role = :moderator + role_token = opentok.generate_token session_id, :role => role + expect(role_token).to be_an_instance_of String + expect(role_token).to carry_token_data :session_id => session_id + expect(role_token).to carry_token_data :api_key => api_key + expect(role_token).to carry_token_data :role => role + expect(role_token).to carry_token_data [:nonce, :create_time] + expect(role_token).to carry_valid_token_signature api_secret + end - it "generates tokens with initial layout classes" do - layout_classes = ["focus", "small"] - layout_class_bearing_token = opentok.generate_token session_id, :initial_layout_class_list => layout_classes, :token_type => "JWT" - expect(layout_class_bearing_token).to be_an_instance_of String - expect(layout_class_bearing_token).to carry_jwt_token_data :session_id => session_id - expect(layout_class_bearing_token).to carry_jwt_token_data :iss => api_key - expect(layout_class_bearing_token).to carry_jwt_token_data :ist => ist - expect(layout_class_bearing_token).to carry_jwt_token_data :scope => scope - expect(layout_class_bearing_token).to carry_jwt_token_data :role => default_role - expect(layout_class_bearing_token).to carry_jwt_token_data :initial_layout_class_list => layout_classes.join(' ') - expect(layout_class_bearing_token).to carry_jwt_token_data [:ist, :iat, :nonce] - expect(layout_class_bearing_token).to carry_valid_jwt_token_signature api_secret - end + it "generates tokens with a publisheronly role" do + role = :publisheronly + role_token = opentok.generate_token session_id, :role => role + expect(role_token).to be_an_instance_of String + expect(role_token).to carry_token_data :session_id => session_id + expect(role_token).to carry_token_data :api_key => api_key + expect(role_token).to carry_token_data :role => role + expect(role_token).to carry_token_data [:nonce, :create_time] + expect(role_token).to carry_valid_token_signature api_secret + end - it "generates tokens with one initial layout class" do - layout_class = "focus" - layout_class_bearing_token = opentok.generate_token session_id, :initial_layout_class_list => layout_class, :token_type => "JWT" - expect(layout_class_bearing_token).to be_an_instance_of String - expect(layout_class_bearing_token).to carry_jwt_token_data :session_id => session_id - expect(layout_class_bearing_token).to carry_jwt_token_data :iss => api_key - expect(layout_class_bearing_token).to carry_jwt_token_data :ist => ist - expect(layout_class_bearing_token).to carry_jwt_token_data :scope => scope - expect(layout_class_bearing_token).to carry_jwt_token_data :role => default_role - expect(layout_class_bearing_token).to carry_jwt_token_data :initial_layout_class_list => layout_class - expect(layout_class_bearing_token).to carry_jwt_token_data [:ist, :iat, :nonce] - expect(layout_class_bearing_token).to carry_valid_jwt_token_signature api_secret - end + it "generates tokens with data" do + data = "name=Johnny" + data_bearing_token = opentok.generate_token session_id, :data => data + expect(data_bearing_token).to be_an_instance_of String + expect(data_bearing_token).to carry_token_data :session_id => session_id + expect(data_bearing_token).to carry_token_data :api_key => api_key + expect(data_bearing_token).to carry_token_data :role => default_role + expect(data_bearing_token).to carry_token_data :data => data + expect(data_bearing_token).to carry_token_data [:nonce, :create_time] + expect(data_bearing_token).to carry_valid_token_signature api_secret + end - context "when the role is invalid" do - it "raises an error" do - expect { opentok.generate_token session_id, :role => :invalid_role, :token_type => "JWT" }.to raise_error - end - end + it "generates tokens with initial layout classes" do + layout_classes = ["focus", "small"] + layout_class_bearing_token = opentok.generate_token session_id, :initial_layout_class_list => layout_classes + expect(layout_class_bearing_token).to be_an_instance_of String + expect(layout_class_bearing_token).to carry_token_data :session_id => session_id + expect(layout_class_bearing_token).to carry_token_data :api_key => api_key + expect(layout_class_bearing_token).to carry_token_data :role => default_role + expect(layout_class_bearing_token).to carry_token_data :initial_layout_class_list => layout_classes.join(' ') + expect(layout_class_bearing_token).to carry_token_data [:nonce, :create_time] + expect(layout_class_bearing_token).to carry_valid_token_signature api_secret end - context "when token type is not specified" do - it "generates a JWT token by default" do - default_token = opentok.generate_token session_id - expect(default_token).to be_an_instance_of String - expect(default_token).to carry_valid_jwt_token_signature api_secret - end + it "generates tokens with one initial layout class" do + layout_class = "focus" + layout_class_bearing_token = opentok.generate_token session_id, :initial_layout_class_list => layout_class + expect(layout_class_bearing_token).to be_an_instance_of String + expect(layout_class_bearing_token).to carry_token_data :session_id => session_id + expect(layout_class_bearing_token).to carry_token_data :api_key => api_key + expect(layout_class_bearing_token).to carry_token_data :role => default_role + expect(layout_class_bearing_token).to carry_token_data :initial_layout_class_list => layout_class + expect(layout_class_bearing_token).to carry_token_data [:nonce, :create_time] + expect(layout_class_bearing_token).to carry_valid_token_signature api_secret end - context "when token type is invalid" do + context "when the role is invalid" do it "raises an error" do - expect { opentok.generate_token session_id, :token_type => "invalid_token_type" }.to raise_error + expect { opentok.generate_token session_id, :role => :invalid_role }.to raise_error end end diff --git a/spec/shared/session_generates_tokens.rb b/spec/shared/session_generates_tokens.rb index 6f81c8fa..7690b4b2 100644 --- a/spec/shared/session_generates_tokens.rb +++ b/spec/shared/session_generates_tokens.rb @@ -15,125 +15,53 @@ let(:api_secret) { "1234567890abcdef1234567890abcdef1234567890" } let(:session_id) { "1_MX4xMjM0NTZ-flNhdCBNYXIgMTUgMTQ6NDI6MjMgUERUIDIwMTR-MC40OTAxMzAyNX4" } let(:default_role) { :publisher } - let(:ist) { "project" } - let(:scope) { "session.connect" } - context "when token type is T1" do - it "generates plain tokens" do - plain_token = session.generate_token :token_type => "T1" - expect(plain_token).to be_an_instance_of String - expect(plain_token).to carry_t1_token_data :session_id => session_id - expect(plain_token).to carry_t1_token_data :api_key => api_key - expect(plain_token).to carry_t1_token_data :role => default_role - expect(plain_token).to carry_t1_token_data [:nonce, :create_time] - expect(plain_token).to carry_valid_t1_token_signature api_secret - end - - it "generates tokens with an expire time" do - expire_time = Time.now + (60*60*24) - expiring_token = session.generate_token :expire_time => expire_time, :token_type => "T1" - expect(expiring_token).to be_an_instance_of String - expect(expiring_token).to carry_t1_token_data :session_id => session_id - expect(expiring_token).to carry_t1_token_data :api_key => api_key - expect(expiring_token).to carry_t1_token_data :role => default_role - expect(expiring_token).to carry_t1_token_data :expire_time => expire_time.to_i - expect(expiring_token).to carry_t1_token_data [:nonce, :create_time] - expect(expiring_token).to carry_valid_t1_token_signature api_secret - end - - it "generates tokens with a role" do - role = :moderator - role_token = session.generate_token :role => role, :token_type => "T1" - expect(role_token).to be_an_instance_of String - expect(role_token).to carry_t1_token_data :session_id => session_id - expect(role_token).to carry_t1_token_data :api_key => api_key - expect(role_token).to carry_t1_token_data :role => role - expect(role_token).to carry_t1_token_data [:nonce, :create_time] - expect(role_token).to carry_valid_t1_token_signature api_secret - end - - it "generates tokens with data" do - data = "name=Johnny" - data_bearing_token = session.generate_token :data => data, :token_type => "T1" - expect(data_bearing_token).to be_an_instance_of String - expect(data_bearing_token).to carry_t1_token_data :session_id => session_id - expect(data_bearing_token).to carry_t1_token_data :api_key => api_key - expect(data_bearing_token).to carry_t1_token_data :role => default_role - expect(data_bearing_token).to carry_t1_token_data :data => data - expect(data_bearing_token).to carry_t1_token_data [:nonce, :create_time] - expect(data_bearing_token).to carry_valid_t1_token_signature api_secret - end + it "generates plain tokens" do + plain_token = session.generate_token + expect(plain_token).to be_an_instance_of String + expect(plain_token).to carry_token_data :session_id => session_id + expect(plain_token).to carry_token_data :api_key => api_key + expect(plain_token).to carry_token_data :role => default_role + expect(plain_token).to carry_token_data [:nonce, :create_time] + expect(plain_token).to carry_valid_token_signature api_secret end - context "when token type is JWT" do - it "generates plain tokens" do - plain_token = session.generate_token :token_type => "JWT" - expect(plain_token).to be_an_instance_of String - expect(plain_token).to carry_jwt_token_data :session_id => session_id - expect(plain_token).to carry_jwt_token_data :iss => api_key - expect(plain_token).to carry_jwt_token_data :ist => ist - expect(plain_token).to carry_jwt_token_data :scope => scope - expect(plain_token).to carry_jwt_token_data :role => default_role - expect(plain_token).to carry_jwt_token_data [:ist, :iat, :nonce] - expect(plain_token).to carry_valid_jwt_token_signature api_secret - end - - it "generates tokens with a custom expire time" do - expire_time = Time.now + (60*60*24) - expiring_token = session.generate_token :expire_time => expire_time, :token_type => "JWT" - expect(expiring_token).to be_an_instance_of String - expect(expiring_token).to carry_jwt_token_data :session_id => session_id - expect(expiring_token).to carry_jwt_token_data :iss => api_key - expect(expiring_token).to carry_jwt_token_data :ist => ist - expect(expiring_token).to carry_jwt_token_data :scope => scope - expect(expiring_token).to carry_jwt_token_data :role => default_role - expect(expiring_token).to carry_jwt_token_data :exp => expire_time.to_i - expect(expiring_token).to carry_jwt_token_data [:ist, :iat, :nonce] - expect(expiring_token).to carry_valid_jwt_token_signature api_secret - end - - it "generates tokens with a non-default role" do - role = :moderator - role_token = session.generate_token :role => role, :token_type => "JWT" - expect(role_token).to be_an_instance_of String - expect(role_token).to carry_jwt_token_data :session_id => session_id - expect(role_token).to carry_jwt_token_data :iss => api_key - expect(role_token).to carry_jwt_token_data :ist => ist - expect(role_token).to carry_jwt_token_data :scope => scope - expect(role_token).to carry_jwt_token_data :role => role - expect(role_token).to carry_jwt_token_data [:ist, :iat, :nonce] - expect(role_token).to carry_valid_jwt_token_signature api_secret - end - - it "generates tokens with data" do - data = "name=Johnny" - data_bearing_token = session.generate_token :data => data, :token_type => "JWT" - expect(data_bearing_token).to be_an_instance_of String - expect(data_bearing_token).to carry_jwt_token_data :session_id => session_id - expect(data_bearing_token).to carry_jwt_token_data :iss => api_key - expect(data_bearing_token).to carry_jwt_token_data :ist => ist - expect(data_bearing_token).to carry_jwt_token_data :scope => scope - expect(data_bearing_token).to carry_jwt_token_data :role => default_role - expect(data_bearing_token).to carry_jwt_token_data :connection_data => data - expect(data_bearing_token).to carry_jwt_token_data [:ist, :iat, :nonce] - expect(data_bearing_token).to carry_valid_jwt_token_signature api_secret - end + it "generates tokens with an expire time" do + expire_time = Time.now + (60*60*24) + expiring_token = session.generate_token :expire_time => expire_time + expect(expiring_token).to be_an_instance_of String + expect(expiring_token).to carry_token_data :session_id => session_id + expect(expiring_token).to carry_token_data :api_key => api_key + expect(expiring_token).to carry_token_data :role => default_role + expect(expiring_token).to carry_token_data :expire_time => expire_time.to_i + expect(expiring_token).to carry_token_data [:nonce, :create_time] + expect(expiring_token).to carry_valid_token_signature api_secret end - context "when token type is not specified" do - it "generates a JWT token by default" do - default_token = session.generate_token - expect(default_token).to be_an_instance_of String - expect(default_token).to carry_valid_jwt_token_signature api_secret - end + it "generates tokens with a role" do + role = :moderator + role_token = session.generate_token :role => role + expect(role_token).to be_an_instance_of String + expect(role_token).to carry_token_data :session_id => session_id + expect(role_token).to carry_token_data :api_key => api_key + expect(role_token).to carry_token_data :role => role + expect(role_token).to carry_token_data [:nonce, :create_time] + expect(role_token).to carry_valid_token_signature api_secret end - context "when token type is invalid" do - it "raises an error" do - expect { session.generate_token :token_type => "invalid_token_type" }.to raise_error - end + it "generates tokens with data" do + data = "name=Johnny" + data_bearing_token = session.generate_token :data => data + expect(data_bearing_token).to be_an_instance_of String + expect(data_bearing_token).to carry_token_data :session_id => session_id + expect(data_bearing_token).to carry_token_data :api_key => api_key + expect(data_bearing_token).to carry_token_data :role => default_role + expect(data_bearing_token).to carry_token_data :data => data + expect(data_bearing_token).to carry_token_data [:nonce, :create_time] + expect(data_bearing_token).to carry_valid_token_signature api_secret end + # TODO a context about using a bad session_id end