8000 Initial Implementation by bryanp · Pull Request #1 · bryanp/branca · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Initial Implementation #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and < 8000 a class="Link--inTextBlock" href="https://docs.github.com/privacy" target="_blank">privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/branca.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# frozen_string_literal: true

require "securerandom"

module Branca
def self.key
SecureRandom.random_bytes(32)
end

require_relative "branca/token"
require_relative "branca/version"
end
35 changes: 35 additions & 0 deletions lib/ 10000 branca/token.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require "b3bm"
require "core/global"
require "openssl"
require "securerandom"

module Branca
class Token
include Is::Global

def initialize(cipher: "chacha20", version: 0xBA, encoder: B3bm.method(:encode), decoder: B3bm.method(:decode))
@cipher = cipher
@version = version
@encoder = encoder
@decoder = decoder
end

def encode(payload, key:, timestamp: Time.now)
nonce = SecureRandom.random_bytes(24)
header = [@version, timestamp.to_i].pack("C N") + nonce

cipher = OpenSSL::Cipher.new(@cipher)
cipher.encrypt
cipher.iv = nonce
cipher.key = key

encrypted = cipher.update(payload) + cipher.final
encoder.call(encrypted)
end

def decode(payload, key:, ttl: nil)
end
end
end
5 changes: 5 additions & 0 deletions spec/features/expiration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

RSpec.describe "expiring tokens" do
it "needs specs"
end
6 changes: 6 additions & 0 deletions spec/features/key_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

RSpec.describe "generating a key" do
it "generates a 32 byte key"
it "generates a unique key"
end
6 changes: 6 additions & 0 deletions spec/features/security_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

RSpec.describe "security" do
it "does not decode a token with an invalid key"
it "does not decode a token that has been tampered with"
end
36 changes: 36 additions & 0 deletions spec/features/token_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require "securerandom"

require "branca"

RSpec.describe "tokens" do
let(:key) {
Branca.key
}

let(:encoded) {
Branca::Token.encode(payload, key: key)
}

let(:decoded) {
Branca::Token.decode(payload, key: key)
}

context "string payload" do
let(:payload) {
SecureRandom.alphanumeric(128)
}

it "encodes" do
pp encoded
end

it "decodes"
end

context "byte string payload" do
it "encodes"
it "decodes"
end
end
0