Class: OpenProject::Token::Extractor

Inherits:
Object
  • Object
show all
Defined in:
lib/open_project/token/extractor.rb

Defined Under Namespace

Classes: DecryptionError, Error, KeyError, SignatureError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Extractor

Returns a new instance of Extractor.



13
14
15
# File 'lib/open_project/token/extractor.rb', line 13

def initialize(key)
  @key = key
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



11
12
13
# File 'lib/open_project/token/extractor.rb', line 11

def key
  @key
end

Instance Method Details

#read(data) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/open_project/token/extractor.rb', line 17

def read(data)
  unless key.public?
    raise KeyError, "Provided key is not a public key."
  end

  json_data = Base64.decode64(data.chomp)

  begin
    container = JSON.parse(json_data)
  rescue JSON::ParserError
    raise DecryptionError, "Encryption data is invalid JSON."
  end

  if container["payload"].nil?
    decrypt_encryption_data(container)
  else
    decrypt_signed_format(container)
  end
end