Skip to content

Commit

Permalink
Prepare for first gem release.
Browse files Browse the repository at this point in the history
  • Loading branch information
omarish committed Nov 21, 2017
1 parent cf01811 commit aa9998c
Show file tree
Hide file tree
Showing 15 changed files with 266 additions and 105 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.vscode
ext/node_modules/
7 changes: 3 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
source "https://rubygems.org"
ruby '2.3.1'

gem 'rspec'
gem 'rspec-expectations'
gem 'timecop'
git_source(:github) {|repo_name| "https://github.com/omarish/vidyo" }

ruby '2.3.1'
14 changes: 14 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
GEM
remote: https://rubygems.org/
specs:

PLATFORMS
ruby

DEPENDENCIES

RUBY VERSION
ruby 2.3.1p112

BUNDLED WITH
1.16.0
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Omar Bohsali

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
# vidyo-ruby

Gem for creating auth tokens for vidyo sessions.
Create authentication tokens for [vidyo][1] sessions in Ruby. I found token generation code in javascript, C#, and Python, but could not find anything for Ruby.

https://developer.vidyo.io/documentation/4-1-17-5/getting-started
Learn more about the Vidyo API here: [Vidyo API Docs][2].

## Install

```bash
$ bundle
```

## Usage

```ruby
token = Vidyo::Token.new(
key: 'your_vidyo_developer_key',
application_id: 'vidyo_application_id',
user_name: 'the user joining the session',
expires_in: 3600
)
token.serialize
```

## Tests

Expand All @@ -14,3 +32,7 @@ $ rspec

* https://github.com/Vidyo/generateToken-c-sharp
* https://static.vidyo.io/4.1.17.5/utils/generateToken.py


[1]: http://vidyo.io "Vidyo Homepage"
[2]: https://developer.vidyo.io/documentation/4-1-17-5/getting-started "Vidyo API Docs"
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
14 changes: 14 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "vidyo"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require "irb"
IRB.start(__FILE__)
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
123 changes: 123 additions & 0 deletions ext/generateToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
//This is a nodejs script, and requires the following npm packages to run:
//jssha, btoa and command-line-args

//WARNING - Token generation should NEVER be done client side (in a browser for
//example), because then you are exposing your developer key to customers
/*jshint esversion: 6 */

jsSHA = require('jssha');
btoa = require('btoa');
fs = require('fs');
const commandLineArgs = require('command-line-args');
var tokenGenerated = false;
var vCardFileSpecified = false;

const optionDefinitions = [{
name: 'key',
type: String
}, {
name: 'appID',
type: String
}, {
name: 'userName',
type: String
}, {
name: 'vCardFile',
type: String
}, {
name: 'expiresInSecs',
type: Number
}, {
name: 'expiresAt',
type: String,
multiple: true
}, {
name: 'help',
alias: 'h',
type: String
}];

const options = commandLineArgs(optionDefinitions);

function printHelp() {
console.log("\nThis script will generate a provision login token from a developer key" +
"\nOptions:" +
"\n\t--key Developer key supplied with the developer account" +
"\n\t--appID ApplicationID supplied with the developer account" +
"\n\t--userName Username to generate a token for" +
"\n\t--vCardFile Path to the XML file containing a vCard for the user" +
"\n\t--expiresInSecs Number of seconds the token will be valid can be used instead of expiresAt" +
"\n\t--expiresAt Time at which the token will expire ex: (2055-10-27T10:54:22Z) can be used instead of expiresInSecs" +
"\n");
process.exit();
}

if ((typeof options.help !== 'undefined') || (typeof options.key == 'undefined') || (typeof options.appID == 'undefined') || (typeof options.userName == 'undefined')) {
printHelp();
}

if (typeof options.vCardFile !== 'undefined') {
vCardFileSpecified = true;
}

function checkForVCardFileAndGenerateToken(key, appID, userName, expiresInSeconds) {
if (vCardFileSpecified) {
fs.readFile(options.vCardFile, 'utf8', function(err, data) {
if (err) {
return console.log("error reading vCard file " + err);
}
console.log("read in the fillowing vCard: " + data);
generateToken(key, appID, userName, expiresInSeconds, data);
});
} else {
generateToken(key, appID, userName, expiresInSeconds, "");
}
}

function generateToken(key, appID, userName, expiresInSeconds, vCard) {
var EPOCH_SECONDS = 62167219200;
var expires = Math.floor(Date.now() / 1000) + expiresInSeconds + EPOCH_SECONDS;
var shaObj = new jsSHA("SHA-384", "TEXT");
shaObj.setHMACKey(key, "TEXT");
jid = userName + '@' + appID;
var body = 'provision' + '\x00' + jid + '\x00' + expires + '\x00' + vCard;
shaObj.update(body);
var mac = shaObj.getHMAC("HEX");
var serialized = body + '\0' + mac;
console.log("\nGenerated Token: \n" + btoa(serialized));
}

//Date is in the format: "October 13, 2014 11:13:00"
function generateTokenExpiresOnDate(key, appID, userName, date) {
var currentDate = new Date(date);
var dateInSeconds = Math.floor(currentDate.valueOf() / 1000);
var nowInSeconds = Math.floor(Date.now() / 1000);
if (dateInSeconds < nowInSeconds) {
console.log("Date is before current time, so token will be invalid");
expiresInSeconds = 0;
} else {
expiresInSeconds = dateInSeconds - nowInSeconds;
console.log("Expires in seconds: " + expiresInSeconds);
}
checkForVCardFileAndGenerateToken(key, appID, userName, expiresInSeconds);
}

console.log("\nGenerating token with the following inputs");
console.log("Key: " + options.key);
console.log("appID: " + options.appID);
console.log("userName: " + options.userName);

if (typeof options.vCardFile !== 'undefined') {
console.log("vCardFile: " + options.vCardFile);
}

if (typeof options.expiresInSecs !== 'undefined') {
console.log("expiresInSecs: " + options.expiresInSecs);
checkForVCardFileAndGenerateToken(options.key, options.appID, options.userName, options.expiresInSecs);

} else if (typeof options.expiresAt !== 'undefined') {
console.log("expiresAt: " + options.expiresAt);
generateTokenExpiresOnDate(options.key, options.appID, options.userName, options.expiresAt);
} else {
console.log("Error: Neither expiresInSecs or expiresAt parameters passed in");
}
5 changes: 4 additions & 1 deletion lib/vidyo.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
class Vidyo
require "vidyo/version"
require "vidyo/token"

module Vidyo
end
10 changes: 2 additions & 8 deletions lib/vidyo/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,17 @@ def to_bytes(x)
x.force_encoding('utf-8').unpack("C*")
end

def bin_to_hex(s)
s.each_byte.map { |b| b.to_s(16) }.join
end

class Vidyo
module Vidyo
class Token
def initialize(key:, application_id:, user_name:, expires_in:)
@key = key
@application_id = application_id
@user_name = user_name
@expires_in = expires_in

@debug = true
end

def epoch_seconds
# Converts 1970-01-01 to seconds since 0AD.
# Converts 1970-01-01 to seconds since 0 AD.
62167219200
end

Expand Down
3 changes: 3 additions & 0 deletions lib/vidyo/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Vidyo
VERSION = "0.1.0"
end
91 changes: 1 addition & 90 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,105 +1,16 @@

# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
# files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need
# it.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
require 'date'
require 'timecop'
require_relative '../lib/vidyo/version.rb'
require_relative '../lib/vidyo/token.rb'

RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end

# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
# have no way to turn it off -- the option exists only for backwards
# compatibility in RSpec 3). It causes shared context metadata to be
# inherited by the metadata hash of host groups and examples, rather than
# triggering implicit auto-inclusion in groups with matching metadata.
config.shared_context_metadata_behavior = :apply_to_host_groups

# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
# This allows you to limit a spec run to individual examples or groups
# you care about by tagging them with `:focus` metadata. When nothing
# is tagged with `:focus`, all examples get run. RSpec also provides
# aliases for `it`, `describe`, and `context` that include `:focus`
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
config.filter_run_when_matching :focus
# Allows RSpec to persist some state between runs in order to support
# the `--only-failures` and `--next-failure` CLI options. We recommend
# you configure your source control system to ignore this file.
config.example_status_persistence_file_path = "spec/examples.txt"
# Limits the available syntax to the non-monkey patched syntax that is
# recommended. For more details, see:
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
config.disable_monkey_patching!
# This setting enables warnings. It's recommended, but in some cases may
# be too noisy due to issues in dependencies.
config.warnings = true
# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = "doc"
end
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random
# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
=end
end
5 changes: 5 additions & 0 deletions spec/vidyo/version_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe Vidyo::VERSION do
it "has a version number" do
expect(Vidyo::VERSION).not_to be nil
end
end
Loading

0 comments on commit aa9998c

Please sign in to comment.