Class: Gemstash::Authorization

Inherits:
Object
  • Object
show all
Extended by:
Env::Helper, Logging
Defined in:
lib/gemstash/authorization.rb

Overview

Authorization mechanism to manipulate private gems.

Constant Summary collapse

VALID_PERMISSIONS =
%w(push yank unyank).freeze

Constants included from Logging

Logging::LEVELS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

log, log_error, logger, reset, setup_logger

Constructor Details

#initialize(record) ⇒ Authorization

Returns a new instance of Authorization.



60
61
62
63
64
# File 'lib/gemstash/authorization.rb', line 60

def initialize(record)
  @auth_key = record.auth_key
  @all = record.permissions == "all"
  @permissions = Set.new(record.permissions.split(","))
end

Class Method Details

.[](auth_key) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gemstash/authorization.rb', line 48

def self.[](auth_key)
  cached_auth = gemstash_env.cache.authorization(auth_key)
  return cached_auth if cached_auth
  record = Gemstash::DB::Authorization[auth_key: auth_key]

  if record
    auth = new(record)
    gemstash_env.cache.set_authorization(record.auth_key, auth)
    auth
  end
end

.authorize(auth_key, permissions) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gemstash/authorization.rb', line 14

def self.authorize(auth_key, permissions)
  raise "Authorization key is required!" if auth_key.to_s.strip.empty?
  raise "Permissions are required!" if permissions.to_s.empty?

  unless permissions == "all"
    permissions.each do |permission|
      unless VALID_PERMISSIONS.include?(permission)
        raise "Invalid permission '#{permission}'"
      end
    end

    permissions = permissions.join(",")
  end

  Gemstash::DB::Authorization.insert_or_update(auth_key, permissions)
  gemstash_env.cache.invalidate_authorization(auth_key)
  log.info "Authorization '#{auth_key}' updated with access to '#{permissions}'"
end

.check(auth_key, permission) ⇒ Object

Raises:



41
42
43
44
45
46
# File 'lib/gemstash/authorization.rb', line 41

def self.check(auth_key, permission)
  raise NotAuthorizedError, "Authorization key required" if auth_key.to_s.strip.empty?
  auth = self[auth_key]
  raise NotAuthorizedError, "Authorization key is invalid" unless auth
  raise NotAuthorizedError, "Authorization key doesn't have #{permission} access" unless auth.can?(permission)
end

.remove(auth_key) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/gemstash/authorization.rb', line 33

def self.remove(auth_key)
  record = Gemstash::DB::Authorization[auth_key: auth_key]
  return unless record
  record.destroy
  gemstash_env.cache.invalidate_authorization(auth_key)
  log.info "Authorization '#{auth_key}' with access to '#{record.permissions}' removed"
end

Instance Method Details

#all?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/gemstash/authorization.rb', line 71

def all?
  @all
end

#can?(permission) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/gemstash/authorization.rb', line 66

def can?(permission)
  raise "Invalid permission '#{permission}'" unless VALID_PERMISSIONS.include?(permission)
  all? || @permissions.include?(permission)
end

#push?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/gemstash/authorization.rb', line 75

def push?
  can?("push")
end

#unyank?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/gemstash/authorization.rb', line 83

def unyank?
  can?("unyank")
end

#yank?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/gemstash/authorization.rb', line 79

def yank?
  can?("yank")
end