lib/terminalwire/client/entitlement.rb in terminalwire-0.1.3 vs lib/terminalwire/client/entitlement.rb in terminalwire-0.1.4
- old
+ new
@@ -1,7 +1,7 @@
module Terminalwire::Client
- class Entitlement
+ module Entitlement
class Paths
include Enumerable
def initialize
@permitted = []
@@ -53,55 +53,91 @@
def serialize
@permitted.to_a.map(&:to_s)
end
end
- attr_reader :paths, :authority, :schemes
+ class Policy
+ attr_reader :paths, :authority, :schemes
- def initialize(authority:)
- @authority = authority
- @paths = Paths.new
+ ROOT_PATH = "~/.terminalwire".freeze
- # Permit the domain directory. This is necessary for basic operation of the client.
- @paths.permit storage_path
- @paths.permit storage_pattern
+ def initialize(authority:)
+ @authority = authority
+ @paths = Paths.new
- @schemes = Schemes.new
- # Permit http & https by default.
- @schemes.permit "http"
- @schemes.permit "https"
- end
+ # Permit the domain directory. This is necessary for basic operation of the client.
+ @paths.permit storage_path
+ @paths.permit storage_pattern
- def domain_path
- Pathname.new("~/.terminalwire/authorities/#{@authority}").expand_path
- end
+ @schemes = Schemes.new
+ # Permit http & https by default.
+ @schemes.permit "http"
+ @schemes.permit "https"
+ end
- def storage_path
- domain_path.join("storage")
- end
+ def root_path
+ Pathname.new(ROOT_PATH)
+ end
- def storage_pattern
- storage_path.join("**/*")
+ def authority_path
+ root_path.join("authorities/#{authority}")
+ end
+
+ def storage_path
+ authority_path.join("storage")
+ end
+
+ def storage_pattern
+ storage_path.join("**/*")
+ end
+
+ def serialize
+ {
+ authority: @authority,
+ schemes: @schemes.serialize,
+ paths: @paths.serialize,
+ storage_path: storage_path.to_s,
+ }
+ end
end
- def serialize
- {
- authority: @authority,
- schemes: @schemes.serialize,
- paths: @paths.serialize,
- storage_path: storage_path.to_s,
- }
+ class RootPolicy < Policy
+ HOST = "terminalwire.com".freeze
+
+ def initialize(*, **, &)
+ # Make damn sure the authority is set to Terminalwire.
+ super(*, authority: HOST, **, &)
+
+ # Now setup special permitted paths.
+ @paths.permit root_path
+ @paths.permit root_pattern
+ end
+
+ # Grant access to the `~/.terminalwire/**/*` path so users can install
+ # terminalwire apps via `terminalwire install svbtle`, etc.
+ def root_pattern
+ root_path.join("**/*")
+ end
end
def self.from_url(url)
+ url = URI(url)
+
+ case url.host
+ when RootPolicy::HOST
+ RootPolicy.new
+ else
+ Policy.new authority: url_authority(url)
+ end
+ end
+
+ def self.url_authority(url)
# I had to lift this from URI::HTTP because `ws://` doesn't
# have an authority method.
- authority = if url.port == url.default_port
+ if url.port == url.default_port
url.host
else
"#{url.host}:#{url.port}"
end
-
- new authority:
end
end
end