Module: Uits
- Defined in:
- lib/uits.rb
Defined Under Namespace
Classes: RSA2048
Instance Method Summary collapse
Instance Method Details
#get_xmlfile(options = {}) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/uits.rb', line 3 def get_xmlfile( = {}) raise ArgumentError, "Must required some data to get UITS." if .blank? raise ArgumentError, "Must provide Distributor." if [:distributor].blank? raise ArgumentError, "Must provide Product Id." if [:productid].blank? raise ArgumentError, "Must provide Asset ID." if [:assetid].blank? raise ArgumentError, "Must provide either Transaction ID or User ID." if [:tid].blank? && [:uid].blank? raise ArgumentError, "Must provide Media." if [:media].blank? raise ArgumentError, "Must provide Name to whom get UITS." if [:uits_for].blank? raise ArgumentError, "Must provide Passphrase to get UITS." if [:passphrase].blank? raise ArgumentError, "Passphrase must not be less than 3 charecters." if [:passphrase].to_s.length < 3.blank? nonce = ActiveSupport::SecureRandom.base64(6) # The nonce is a random value generated at the time of sale whose Base64 encoded length is 8 digits distributor = [:distributor].to_s # This is the retailer or distributor’s name or an associated globally unique identifier in clear text example "Sony" time = Time.now.iso8601 # The date and time of the purchase (not download or signature) in ISO 8601 format productid = [:productid].to_s # Track XML Product/Track/Metadata/PhysicalProduct/ProductCode example "00602517178656" assetid = [:assetid].to_s # Track XML Catalog/Action/Product/Track/Metadata/ISRC example "USUV70603512" tid = ![:tid].blank? ? [:tid].to_s : nil # The transaction ID is the unique identifier for the transaction example "39220345237" uid = ![:uid].blank? ? [:uid].to_s : nil # The User ID is the unique identifier for the user example "A74GHY8976547B" media = OpenSSL::Digest::SHA256.digest([:media]) # example "d5b17cc1975d3095c6353f3fdced45ae867c06e02c1efb7c09662cdc796724b0" first_part = "<uits:UITS xmlns:uits='http://www.udirector.net/schemas/2009/uits/1.1' xmlns:xsi='http://www.w3.org/2001/XMLSchema-‐instance'>" = "<metadata> <nonce>#{nonce}</nonce> <Distributor>#{distributor.to_s}</Distributor> <Time>#{time.to_s}</Time> <ProductId type='UPC' completed='false'>#{productid.to_s}</ProductId> <AssetID type='ISRC'>#{assetid.to_s}</AssetID> <TID version='1'>#{tid.to_s}</TID> <UID version='1'>#{uid.to_s}</UID> <URL/> <PA/> <Media algorithm='SHA256'>#{media.to_s}</Media> </metadata>" canonicalization = .to_s rsa = RSA2048.new([:uits_for].to_s, [:passphrase].to_s) keyid = Base64.encode64(OpenSSL::Digest::SHA1.digest(canonicalization)) sig = rsa.signature(keyid) signature = '<signature keyID="'+keyid.to_s+'" algorithm="RSA2048">'+sig.to_s+'</signature>' last_part = '</uits:UITS>' xml = first_part.to_s + .to_s + signature.to_s + last_part.to_s doc = Nokogiri.XML(xml, nil, 'UTF-8') doc.search(%Q{//signature[@keyID]}).each do |n| n['canonicalization'] = canonicalization end return doc.to_xml end |