Skip to content

Commit

Permalink
Add database tokens to TokenHolder
Browse files Browse the repository at this point in the history
  • Loading branch information
aurelien-reeves-scalingo committed Dec 21, 2022
1 parent d6b93da commit bdb8acf
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 14 deletions.
60 changes: 46 additions & 14 deletions lib/scalingo/token_holder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,60 @@ module Scalingo
module TokenHolder
def self.included(base)
base.attr_reader :token
base.attr_reader :database_tokens
end

def token=(input)
@token = input.is_a?(BearerToken) ? input : BearerToken.new(input.to_s, raise_on_expired: config.raise_on_expired_token)
@token = bearer_token(input)
end

def authenticated?
token.present? && !token.expired?
def add_database_token(database_id, token)
@database_tokens ||= Hash.new

@database_tokens[database_id] = bearer_token(token)
end

def authenticated?(database_id: "")
valid?(token)
end

def authenticated_for_database?(database_id)
return false if database_tokens.nil?
return false unless database_tokens.has_key?(database_id)

valid?(database_tokens[database_id])
end

def authenticate_with_bearer_token(bearer_token, expires_at:, raise_on_expired_token:)
self.token = if expires_at
token = bearer_token.is_a?(BearerToken) ? bearer_token.value : bearer_token.to_s

BearerToken.new(
token,
expires_at: expires_at,
raise_on_expired: raise_on_expired_token,
)
else
bearer_token
end
self.token = build_bearer_token(bearer_token, expires_at: expires_at, raise_on_expired_token: raise_on_expired_token)
end

def authenticate_database_with_bearer_token(database_id, bearer_token, expires_at:, raise_on_expired_token:)
bearer_token = build_bearer_token(bearer_token, expires_at: expires_at, raise_on_expired_token: raise_on_expired_token)

add_database_token(database_id, bearer_token)
end

private

def valid?(token)
token.present? && !token.expired?
end

def bearer_token(token)
token.is_a?(BearerToken) ? token : BearerToken.new(token.to_s, raise_on_expired: config.raise_on_expired_token)
end

def build_bearer_token(bearer_token, expires_at:, raise_on_expired_token:)
return bearer_token unless expires_at

token = bearer_token.is_a?(BearerToken) ? bearer_token.value : bearer_token.to_s

BearerToken.new(
token,
expires_at: expires_at,
raise_on_expired: raise_on_expired_token,
)
end
end
end
11 changes: 11 additions & 0 deletions spec/scalingo/regional_database_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "spec_helper"

RSpec.describe Scalingo::RegionalDatabase do
subject { described_class.new("url") }

%w[databases backups].each do |section|
it "handles requests for #{section}" do
expect(subject.respond_to?(section)).to be true
end
end
end
78 changes: 78 additions & 0 deletions spec/scalingo/token_holder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
require "spec_helper"

RSpec.describe Scalingo::TokenHolder do
subject(:token_holder_dummy_class) do
Class.new { include(Scalingo::TokenHolder); attr_accessor :config }
end

describe "authenticate_with_bearer_token" do
subject { token_holder.authenticate_with_bearer_token(token, expires_at: expires_at, raise_on_expired_token: false) }

let(:token_holder) do
holder = token_holder_dummy_class.new
holder.config = Scalingo::Configuration.new

holder
end

context "without expiration date" do
let(:token) { "1234" }
let(:expires_at) { nil }

it "set the auth token" do
expect(token_holder.authenticated?).to be false
subject()
expect(token_holder.authenticated?).to be true
end
end

context "with an expiration date" do
let(:token) { "1234" }
let(:expires_at) { Time.now + 1.hour }

it "refresh the auth token" do
token_holder.authenticate_with_bearer_token(token, expires_at: 1.hour.ago, raise_on_expired_token: false)
expect(token_holder.authenticated?).to be false

subject()
expect(token_holder.authenticated?).to be true
end
end
end

describe "authenticate_database_with_bearer_token" do
subject { token_holder.authenticate_database_with_bearer_token(database_id, token, expires_at: expires_at, raise_on_expired_token: false) }

let(:token_holder) do
holder = token_holder_dummy_class.new
holder.config = Scalingo::Configuration.new

holder
end

let(:database_id) { "db-id-1234" }

context "without expiration date" do
let(:token) { "1234" }
let(:expires_at) { nil }

it "set the database auth token" do
expect(token_holder.authenticated_for_database?(database_id)).to be false
subject()
expect(token_holder.authenticated_for_database?(database_id)).to be true
end
end

context "with an expiration date" do
let(:token) { "1234" }
let(:expires_at) { Time.now + 1.hour }

it "refresh the database token" do
token_holder.authenticate_database_with_bearer_token(database_id, token, expires_at: 1.hour.ago, raise_on_expired_token: false)
expect(token_holder.authenticated_for_database?(database_id)).to be false
subject()
expect(token_holder.authenticated_for_database?(database_id)).to be true
end
end
end
end

0 comments on commit bdb8acf

Please sign in to comment.