Class: RailsWayback::RefPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_wayback/ref_policy.rb

Overview

Authorizes immutable commits before historical files enter Action View.

Defined Under Namespace

Classes: Result

Constant Summary collapse

ENV_KEY =
"rails_wayback.ref_policy"
FULL_SHA =
/\A(?:[0-9a-f]{40}|[0-9a-f]{64})\z/i

Instance Method Summary collapse

Constructor Details

#initialize(configuration: RailsWayback.configuration, git: RailsWayback::Git.new) ⇒ RefPolicy

Returns a new instance of RefPolicy.



19
20
21
22
# File 'lib/rails_wayback/ref_policy.rb', line 19

def initialize(configuration: RailsWayback.configuration, git: RailsWayback::Git.new)
  @configuration = configuration
  @git = git
end

Instance Method Details

#authorize(ref) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rails_wayback/ref_policy.rb', line 24

def authorize(ref)
  input = ref.to_s

  unless valid_sha?(input)
    return rejection(input, :invalid_format, "expected a full 40- or 64-character commit SHA")
  end

  sha = git.resolve_ref(input).downcase
  trusted = trusted_refs_containing(sha)
  if trusted.empty?
    return rejection(input, :untrusted_ref, "commit is not reachable from a trusted Git ref", sha: sha)
  end

  Result.new(input: input, sha: sha, trusted_refs: trusted.freeze)
rescue RefNotFoundError
  rejection(input, :unknown_ref, "commit does not exist in the local repository")
end