Class: Rights::FileAccess

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

Overview

The file access settings determine the keys needed to authenticate in order to perform some operations on files (inside an application), and how the file transfer are performed. Those two separate concepts are called in the DESFire documentation “file access rights” and “communication settings”.

The keys can be a key number from the application (from 0x0 to 0xD) or the constants FREE_ACCESS (no authentication needed) or DENY_ACCESS (the operation cannot be performed) (constants defined in the Rights module).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ FileAccess

The constructor takes a hash of parameters, which correspond to the class attributes. See the class documentation for an explanation of valid key values.



50
51
52
53
54
55
56
# File 'lib/file_access.rb', line 50

def initialize(hash={})
  @read = hash[:read] || DENY_ACCESS
  @write = hash[:write] || DENY_ACCESS
  @rw = hash[:rw] || DENY_ACCESS
  @change = hash[:change] || DENY_ACCESS
  @com_set = hash[:com_set] || CS_CIPHERED
end

Instance Attribute Details

#changeObject (readonly)

Key to change the file access rights.



40
41
42
# File 'lib/file_access.rb', line 40

def change
  @change
end

#com_setObject (readonly)

Do the data transfer to/from the file need to authenticated or ciphered? The valid values are the CS_... constants defined in the Rights module. com_set stands for “communication settings”.



45
46
47
# File 'lib/file_access.rb', line 45

def com_set
  @com_set
end

#readObject (readonly)

Key to be able to read.



31
32
33
# File 'lib/file_access.rb', line 31

def read
  @read
end

#rwObject (readonly)

Key to be able to read and write.



37
38
39
# File 'lib/file_access.rb', line 37

def rw
  @rw
end

#writeObject (readonly)

Key to be able to write.



34
35
36
# File 'lib/file_access.rb', line 34

def write
  @write
end

Class Method Details

.from_a(ba) ⇒ Object

Builds and returns a FileAccess object from the byte array returned by the GET_FILE_SETTINGS command.



60
61
62
63
64
65
66
67
# File 'lib/file_access.rb', line 60

def self.from_a(ba)
  FileAccess.new(
    :read     => ba[3] >> 4,
    :write    => ba[3] % 16,
    :rw       => ba[2] >> 4,
    :change   => ba[2] % 16,
    :com_set  => ba[1])
end

Instance Method Details

#read_type(key_no) ⇒ Object

Can the file be read when authenticated with the given key, and how? Returns either false or a CS_... constant (see Rights module documentation).



78
79
80
81
82
83
84
85
86
# File 'lib/file_access.rb', line 78

def read_type(key_no)
  if @read == key_no or @rw == key_no
    return @com_set
  elsif @read == FREE_ACCESS or @rw == FREE_ACCESS
    return CS_PLAIN
  else
    return false
  end
end

#to_aObject

Returns a two-byte little-endian view of the access rights (not the communication settings), fit to be used as a command parameter.



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

def to_a
 [rw*16 + change, read*16 + write]
end

#write_type(key_no) ⇒ Object

Can the file be written when authenticated with the given key, and how? Returns either false or a CS_... constant (see Rights module documentation).



91
92
93
94
95
96
97
98
99
# File 'lib/file_access.rb', line 91

def write_type(key_no)
  if @write == key_no or @rw == key_no
    return @com_set
  elsif @write == FREE_ACCESS or @rw == FREE_ACCESS
    return CS_PLAIN
  else
    return false
  end
end