├── .env.example ├── .github └── dependabot.yml ├── .gitignore ├── Gemfile ├── LICENSE ├── README.md └── app.rb /.env.example: -------------------------------------------------------------------------------- 1 | # For Sinatra configuration, defaults to production. 2 | # For local development, set this value to development 3 | APP_ENV=production 4 | 5 | TWILIO_ACCOUNT_SID= 6 | TWILIO_API_KEY= 7 | TWILIO_API_SECRET= 8 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: bundler 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | open-pull-requests-limit: 10 8 | ignore: 9 | - dependency-name: twilio-ruby 10 | versions: 11 | - 5.46.1 12 | - 5.47.0 13 | - 5.48.0 14 | - 5.49.0 15 | - 5.50.0 16 | - 5.51.0 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | .DS_STORE 3 | *.swp 4 | *.rbc 5 | *.sass-cache 6 | /pkg 7 | /doc/api 8 | /Gemfile.lock 9 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | gem 'dotenv' 3 | gem 'sinatra' 4 | gem 'sinatra-contrib' 5 | gem 'twilio-ruby', '5.0.0.rc18' 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Twilio Inc. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Video Access Token Server for Ruby 2 | 3 | #### Looking for the JavaScript Video Quickstart? It has been moved [here](https://github.com/twilio/video-quickstart-js). 4 | 5 | 6 | This server-side application demonstrates generating Access Token for Twilio Video. 7 | Before we begin, we need to collect 8 | all the config values we need to run the application: 9 | 10 | | Config Value | Description | 11 | | :------------- |:------------- | 12 | Account SID | Your primary Twilio account identifier - find this [in the console here](https://www.twilio.com/console). 13 | API Key | Used to authenticate - [generate one here](https://www.twilio.com/console/runtime/api-keys/create). 14 | API Secret | Used to authenticate - [just like the above, you'll get one here](https://www.twilio.com/console/runtime/api-keys/create). 15 | 16 | 17 | ## A Note on API Keys 18 | 19 | When you generate an API key pair at the URLs above, your API Secret will only 20 | be shown once - make sure to save this in a secure location, 21 | or possibly your `~/.bash_profile`. 22 | 23 | ## Setting up the Ruby (Sinatra) Application 24 | 25 | Create a configuration file for your application: 26 | 27 | ```bash 28 | cp .env.example .env 29 | ``` 30 | 31 | Edit `.env` with the three configuration parameters we gathered from above. 32 | 33 | Next, we need to install our dependencies: 34 | 35 | ```bash 36 | bundle install 37 | ``` 38 | 39 | Now we should be all set! Run the application using the `ruby` command. 40 | 41 | ```bash 42 | bundle exec ruby app.rb 43 | ``` 44 | 45 | To generate Access Token, visit [http://localhost:4567?identity=alice&room=example](http://localhost:4567?identity=alice&room=example). 46 | 47 | ### Configure Development vs Production Settings 48 | 49 | By default, this application will run in production mode - stack traces will not be visible in the web browser. If you would like to run this application in development locally, change the `APP_ENV` variable in your `.env` file. 50 | 51 | `APP_ENV=development` 52 | 53 | For more about development vs production, visit [Sinatra's configuration page](http://sinatrarb.com/configuration.html). 54 | 55 | ## License 56 | 57 | MIT 58 | -------------------------------------------------------------------------------- /app.rb: -------------------------------------------------------------------------------- 1 | require 'twilio-ruby' 2 | require 'sinatra' 3 | require 'dotenv' 4 | 5 | # Load environment configuration 6 | Dotenv.load 7 | 8 | # Set the environment after dotenv loads 9 | # Default to production 10 | environment = (ENV['APP_ENV'] || ENV['RACK_ENV'] || :production).to_sym 11 | set :environment, environment 12 | 13 | # Generate a token for use in our Video application 14 | get '/' do 15 | identity = params[:identity] || 'identity' 16 | 17 | # Create an Access Token for Video usage 18 | token = Twilio::JWT::AccessToken.new ENV['TWILIO_ACCOUNT_SID'], 19 | ENV['TWILIO_API_KEY'], ENV['TWILIO_API_SECRET'], 3600, identity 20 | 21 | # Grant access to Video 22 | grant = Twilio::JWT::AccessToken::VideoGrant.new 23 | grant.room = params[:room] 24 | token.add_grant grant 25 | 26 | # Generate the token and send to client 27 | token.to_jwt 28 | end 29 | --------------------------------------------------------------------------------