forked from 2factorauth/twofactorauth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPIv2.rb
More file actions
executable file
·52 lines (41 loc) · 2.26 KB
/
Copy pathAPIv2.rb
File metadata and controls
executable file
·52 lines (41 loc) · 2.26 KB
1
2
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
49
50
51
52
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'json'
tfa = { 'email' => {}, 'hardware' => {}, 'phone' => {}, 'proprietary' => {}, 'sms' => {}, 'totp' => {}, 'u2f' => {} }
rplc_ptrn = { 'call' => 'phone', 'custom-software' => 'proprietary', 'custom-hardware' => 'hardware' }
all = {}
regions = {}
Dir.glob('entries/*/*.json') do |file|
website = JSON.parse(File.read(file)).values[0]
name = JSON.parse(File.read(file)).keys[0]
category = website['categories'][0]
entry = {
'url' => (website['url'].nil? ? "https://#{website['domain']}/" : website['url']),
'img' => (website['img'].nil? ? "#{website['domain']}.svg" : website['img']),
'doc' => (website['documentation'] unless website['documentation'].nil?),
'exception' => (website['notes'] unless website['notes'].nil?)
}.select { |_, i| i } # Use keep the entries that aren't nil
entry['tfa'] = website['tfa'].map { |e| rplc_ptrn.keys.include?(e) ? rplc_ptrn[e] : e } unless website['tfa'].nil?
website['contact']&.each { |a, b| a.eql?('email') ? entry['email_address'] = b : entry[a] = b }
website['regions']&.each { |region| regions[region] = 1 + regions[region].to_i unless region[0] == '-' }
all[category].nil? ? all[category] = { name => entry } : all[category][name] = entry # Initialize the object
end
# Add 'tfa' hash to tfa hash
tfa['tfa'] = {}
# Sort all entries and add appropriate entries to the tfa object
all.each { |k, v| all[k] = v.sort_by { |entry_name, _| entry_name }.to_h }.each do |ctgry, sites|
sites.each do |name, website|
tfa.each do |method, _|
next if website['tfa'].nil?
tfa['tfa'][ctgry].nil? ? (tfa['tfa'][ctgry] = { name => website }) : (tfa['tfa'][ctgry][name] = website)
next unless website['tfa'].include?(method)
tfa[method][ctgry].nil? ? (tfa[method][ctgry] = { name => website }) : (tfa[method][ctgry][name] = website)
end
end
end
# Merge all and tfa, output to separate files
{ 'all' => all }.merge(tfa).each do |file_name, output|
output.each { |k, v| output[k] = v.sort_by { |entry_name, _| entry_name }.to_h } # Sort output alphabetically
File.open("api/v2/#{file_name}.json", 'w') { |file| file.write output.to_json }
end
File.open('api/v2/regions.json', 'w') { |file| file.write regions.sort_by(&:last).reverse.to_h.to_json }