Module: JwtBouncer::Permissions

Defined in:
lib/jwt_bouncer/permissions.rb

Class Method Summary collapse

Class Method Details

.compress(permissions) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/jwt_bouncer/permissions.rb', line 8

def self.compress(permissions)
  stream = StringIO.new
  zip = Zlib::GzipWriter.new(stream)
  begin
    zip.write(permissions.to_json)
  ensure
    zip.close
  end

  Base64.encode64(stream.string)
end

.decompress(permissions) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jwt_bouncer/permissions.rb', line 20

def self.decompress(permissions)
  unzipped_stream = StringIO.new
  StringIO.open(Base64.decode64(permissions)) do |stream|
    unzip = Zlib::GzipReader.new(stream)
    begin
      unzipped_stream.write(unzip.read)
    ensure
      unzip.close
    end
  end

  JSON.parse(unzipped_stream.string)
end

.destructure(permissions) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/jwt_bouncer/permissions.rb', line 34

def self.destructure(permissions)
  destructured_permissions = []
  permissions.each do |service, resources|
    resources.each do |resource, resource_permissions|
      resource_permissions.each do |permission|
        destructured_permissions << [service, resource, permission].join('_')
      end
    end
  end
  destructured_permissions
end