Sha256: b987b6e60a3f740b5fb2109577395eeceedce94020538c63e81af4c195e1b229

Contents?: true

Size: 921 Bytes

Versions: 2

Compression:

Stored size: 921 Bytes

Contents

# frozen_string_literal: true

require_relative "embed/version"
require 'json'
require 'rbnacl'
require "base64"

module Canvas
  module Embed
    extend self
    def generate_token(private_key, scopes)
      # token consists of an id and the signing key
      key_id, key = private_key.split('.')
      # transform signing key hex into bytes
      key_bytes = [key].pack('H*')
      secret_box = RbNaCl::SecretBox.new(key_bytes)
      nonce = RbNaCl::Random.random_bytes(secret_box.nonce_bytes)
      message = { "scopes" => scopes }.to_json
      ciphertext = secret_box.encrypt(nonce, message)
      # transform bytes into hex
      unpacked_message = ciphertext.unpack('H*').first
      unpacked_nonce = nonce.unpack('H*').first
      token = { "message" => unpacked_message, "nonce" => unpacked_nonce, "keyId" => key_id }.to_json;
      # strict for no newlines
      Base64.strict_encode64(token)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
canvas-embed-0.1.4 lib/canvas/embed.rb
canvas-embed-0.1.3 lib/canvas/embed.rb