module SimpleHKP::Identities

Public Instance Methods

getIdentityMetaData(fileContents) click to toggle source
# File lib/simpleHKP/identities.rb, line 16
def getIdentityMetaData(fileContents)
  metaData = Hash.new
  begin
    if fileContents =~ /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m
      metaData = SafeYAML.load($1)
    end
  rescue SyntaxError => e
    puts "YAML Exception reading #{fileName}: #{e.message}"
  end

  if !metaData.has_key?('role') ||
     !metaData.has_key?('userKeyID') ||
     metaData['userKeyID'] !~ /^\h+$/ then
    puts "WARNING the identity file has NO role or userKeyID or a malformed userKeyID"
    return ''
  end

  #
  # normalize the meta data with any missing (but useful) values
  #
  metaData['userID'] = metaData['userKeyID'] unless
    metaData.has_key?('userID')
  metaData['algorithm'] = 'unknown' unless metaData.has_key?('algorithm')
  metaData['created'] = Date.today.strftime("%Y%m%d") unless
    metaData.has_key?('created')
  metaData['expires'] = Date.today.strftime("%Y%m%d") unless
    metaData.has_key?('expires')
  metaData['flags'] = '' unless metaData.has_key?('flags')
  #
  # compute the idFile for searching
  #
  metaData['identity'] = metaData['role']+'-'+metaData['userKeyID']
  #
  # wrap the whole meta data into a searchable string
  #
  metaData['searchData'] = metaData.to_s
  metaData
end
indexIdentities(queryString) click to toggle source
# File lib/simpleHKP/identities.rb, line 176
def indexIdentities(queryString)
  return replyBadRequest("No search field in queryString") unless
    queryString.has_key?('search')
  #
  # normalize the search string
  #
  searchString = queryString['search']
  searchRegexp = Regexp.new(searchString,
                            Regexp::IGNORECASE | Regexp::MULTILINE)
  puts searchRegexp if @debug
  #
  # accumulate the idFiles of any identities that match the search
  #
  idFiles = Array.new
  pp @idMetaData
  @idMetaData.each_pair do | idKey, idData |
    next unless idData['searchData'] =~ searchRegexp
    puts "FOUND #{idKey} (#{idData})" if @debug
    idFiles.push(idKey)
  end

  if queryString.has_key?('options') &&
    queryString['options'] == 'mr' then
    #
    # return a machine readable list of the dFiles found
    #
    @header = { 'Content-Type' => 'text/plain' }
    @body << "info:1:#{idFiles.size}\n"
    idFiles.each do | anIdFile |
      next unless @idMetaData.has_key?(anIdFile)
      idData   = @idMetaData[anIdFile]
      idnData = [ "idn" ]
      idnData << anIdFile
      idnData << idData['role']
      idnData << idData['userID'].gsub(/:/, '\x3a')
      idnData << idData['algorithm']
      idnData << idData['created']
      idnData << idData['expires']
      idnData << idData['flags']
      @body << idnData.join(':')
      @body << "\n"
    end
  else
    #
    # return a (simple) human readable list of the keys found
    #
    @body << @headerHtml
    @body << @lookupIdentitiesForm
    @body << '<h1 class="simpleHKP-searchString">Identities matching: ['+searchString+']</h1><ul class="simpleHKP-searchList">'
    idFiles.each do | anIdFile |
      next unless @idMetaData.has_key?(anIdFile)
      idData   = @idMetaData[anIdFile]
      idStr = "  <li class=\"simpleHKP-searchItem\"><a href=\"/pis/lookup?op=get&search=#{anIdFile}\">"
      idStr << anIdFile+':&nbsp;'+idData['userID']+'&nbsp;('+idData['role']+')'
      idStr <<  "</a></li>\n"
      @body << idStr
    end
    @body << '</ul>'
    @body << @footer
  end
  pp @body if @debug
end
indexIdentityFiles() click to toggle source
# File lib/simpleHKP/identities.rb, line 55
    def indexIdentityFiles
      puts "SimpleHKP: indexing identities" if @debug
      @idMetaData = Hash.new
      Dir.glob(@idDir+'/**/*.asc') do | aFile |
        idFile = File.basename(aFile, '.*')
        puts aFile if @debug
        puts idFile if @debug
        identityFile = loadIdentityFile(aFile)
        metaData = getIdentityMetaData(identityFile)
        if metaData.empty? then
          puts "WARNING the identity file [#{aFile}] is malformed!"
        else
          @idMetaData[idFile] = metaData
        end
        puts @idMetaData[idFile] if @debug
      end
#      File.open(@idIndex, 'w') do | indexFile |
#        indexFile.puts "# identity metaData index created: #{DateTime.now}"
#        indexFile.write(YAML.dump(@idMetaData))
#      end
      puts "SimpleHKP: finished indexing identities" if @debug
    end
loadIdentities() click to toggle source
# File lib/simpleHKP/identities.rb, line 78
def loadIdentities
  @idLookupData = Hash.new
  puts "SimpleHKP: loading identities" if @debug
  indexIdentityFiles
  pp @idMetaData
  puts "SimpleHKP: finished loading identities" if @debug
end
loadIdentityFile(fileName) click to toggle source
# File lib/simpleHKP/identities.rb, line 6
def loadIdentityFile(fileName)
  identityContents = ''
  begin
    identityContents = File.open(fileName, 'r').read
  rescue Exception => e
    puts "Error reading file #{fileName}: #{e.message}"
  end
  identityContents
end
lookUpIdentity(queryString) click to toggle source
# File lib/simpleHKP/identities.rb, line 124
def lookUpIdentity(queryString)
  return replyBadRequest("No search field in queryString") unless
    queryString.has_key?('search')
  #
  # normalize the search string
  #
  searchString = queryString['search']
  searchRegexp = Regexp.new(searchString,
                            Regexp::IGNORECASE | Regexp::MULTILINE)
  puts searchRegexp if @debug
  #
  # (linearly) look through the hash of known identities
  # looking for the FIRST match
  #
  idFile = nil
  pp @idMetaData
  @idMetaData.each_pair do | idKey, idData |
    next unless idData['searchData'] =~ searchRegexp
    puts "FOUND #{idKey} (#{idData})" if @debug
    idFile = idKey
    break
  end
  return replyNotFound if idFile.nil?

  idFileName = @idDir+'/'+idFile+'.asc'
  if queryString.has_key?('options') &&
    queryString['options'] == 'mr' then
    #
    # return the key data in machine readable format
    #
    @header = {
      'Content-Type' => 'text/plain; charset=utf-8',
      'Content-Disposition' =>
        'attachment; filename=' + File.basename(idFileName)
    }
    puts @header if @debug
    @body << File.read(idFileName)
  else
    #
    # return the key data for a human to read
    #
    @body << @headerHtml
    @body << @lookupIdentitiesForm
    @body << '<h1 class="simpleHKP-identityAscIdFile">Identity: '+idFile+'</h1>'
    @body << '<h2 class="simpleHKP-identityAscTitle">Identity contents:</h2>'
    @body << '<pre class="simpleHKP-identityAsc">'
    @body << File.read(idFileName)
    @body << '</pre>'
    @body << @footer
  end
end
storeIdentity(env) click to toggle source
# File lib/simpleHKP/identities.rb, line 86
def storeIdentity(env)
  #
  # decode the post data
  #
  keys = URI.decode_www_form(env['rack.input'].read)
  #
  # ensure we are being sent a key
  #
  return replyBadRequest("No keytext field in post data") unless
    keys[0][0] =~ /keytext/
  pp keys[0][1] if @debug
  metaData = getIdentityMetaData(keys[0][1])
  pp metaData
  return replyBadRequest("malformed identity metaData") if metaData.empty?
  #
  # store this key as a flat file with the name of the keyID.asc
  #
  idFile = metaData['role']+'-'+metaData['userKeyID']
  idFileName = @idDir+'/'+idFile+'.asc'
  idChanged = 'unchanged'
  if !File.exists?(idFileName) || keys[0][1] != File.read(idFileName) then
    idChanged = 'changed'
    File.write(idFileName, keys[0][1])
    @idMetaData[idFile] = metaData
  end
  pp @idMetaData
  #
  # return OK
  #
  @statusCode = 200
  @body << @headerHtml
  @body << "<h1 class=\"simpleHKP-storedIdentity\">Stored identity: [#{idFile}]</h1>"
  @body << "<p>identity #{idChanged}</p>"
  @body << "<h2 class=\"simpleHKP-identityDataID\">Identity data for identity: [#{idFile}]</h2>"
  @body << '<pre class="simpleHKP-identityData">'+keys[0][1]+"</pre>\n"
  @body << @footer
end