forked from 2factorauth/twofactorauth
-
Notifications
You must be signed in to change notification settings - Fork 112
/
verify.rb
151 lines (122 loc) · 4.57 KB
/
verify.rb
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# frozen_string_literal: true
require 'yaml'
require 'fastimage'
require 'kwalify'
require 'diffy'
@output = 0
# YAML tags related to TFA
@tfa_tags = {
# YAML tags for TFA Yes
true => %w[hardware software doc otp u2f multipleu2f passwordless],
# YAML tags for TFA No
false => %w[status twitter facebook email_address lang]
}.freeze
# Image max size (in bytes)
@img_max_size = 2500
# Image dimensions
@img_dimensions = [32, 32]
# Image format used for all images in the 'img/' directories.
@img_extension = '.png'
# Permissions set for all the images in the 'img/' directories.
@img_permissions = %w[644 664]
# Send error message
def error(msg)
@output += 1
puts "<------------ ERROR ------------>\n" if @output == 1
puts "#{@output}. #{msg}"
end
def test_img(img, name, imgs)
# Exception if image file not found
raise "#{name} image not found." unless File.exist?(img)
# Remove img from array unless it doesn't exist (double reference case)
imgs.delete_at(imgs.index(img)) unless imgs.index(img).nil?
# Check image dimensions
error("#{img} is not #{@img_dimensions.join('x')} pixels.")\
unless FastImage.size(img) == @img_dimensions
test_img_file(img)
end
# rubocop:disable Metrics/MethodLength
def test_img_file(img)
# Check image file extension and type
error("#{img} is not using the #{@img_extension} format.")\
unless File.extname(img) == @img_extension && FastImage.type(img) == :png
# Check image file size
img_size = File.size(img)
unless img_size <= @img_max_size
error("#{img} should not be larger than #{@img_max_size} bytes. It is"\
" currently #{img_size} bytes.")
end
# Check image permissions
perms = File.stat(img).mode.to_s(8).split(//).last(3).join
# rubocop:disable Style/GuardClause
unless @img_permissions.include?(perms)
error("#{img} permissions must be one of: #{@img_permissions.join(',')}. "\
"It is currently #{perms}.")
end
# rubocop:enable Style/GuardClause
end
# rubocop:enable Metrics/MethodLength
# Load each section, check for errors such as invalid syntax
# as well as if an image is missing
begin
sections = YAML.load_file('_data/sections.yml')
# Check sections.yml alphabetization
error('section.yml is not alphabetized by name') \
if sections != (sections.sort_by { |section| section['id'].downcase })
schema = YAML.load_file('websites_schema.yml')
validator = Kwalify::Validator.new(schema)
sections.each do |section|
data = YAML.load_file("_data/#{section['id']}.yml")
websites = data['websites']
errors = validator.validate(data)
errors.each do |e|
error("#{websites.at(e.path.split('/')[2].to_i)['name']}: #{e.message}")
end
# Check section alphabetization
if websites != (sites_sort = websites.sort_by { |s| s['name'].downcase })
error("_data/#{section['id']}.yml not ordered by name. Correct order:" \
"\n" + Diffy::Diff.new(websites.to_yaml, sites_sort.to_yaml, \
context: 10).to_s(:color))
end
# Collect list of all images for section
imgs = Dir["img/#{section['id']}/*"]
websites.each do |website|
test_img("img/#{section['id']}/#{website['img']}", website['name'],
imgs)
# prevent travis fail when hardware is not present
# tested tags are desired in this case for dongleauth.com
next if website['hardware'].nil?
@tfa_tags[!website['tfa']].each do |tag|
next if website[tag].nil?
error("\'#{tag}\' should NOT be "\
"present when hardware: #{website['tfa'] ? 'true' : 'false'}.")
end
end
# After removing images associated with entries in test_img, alert
# for unused or orphaned images
imgs.each { |img| next unless img.nil? error("#{img} is not used") }
end
# Test the very same for the dongleauth.com's provider.yml as well
data = YAML.load_file('_data/providers.yml')
providers = data['providers']
# Check section alphabetization
error('_data/providers.yml is not alphabetized by name') \
if providers != (providers.sort_by { |provider| provider['name'].downcase })
# Collect list of all images for section
imgs = Dir['img/providers/*']
providers.each do |provider|
test_img("img/providers/#{provider['img']}", provider['name'],
imgs)
end
imgs.each { |img| next unless img.nil? error("#{img} is not used") }
exit 1 if @output.positive?
rescue Psych::SyntaxError => e
puts "<------------ ERROR in a YAML file ------------>\n"
puts e
exit 1
rescue StandardError => e
puts e
exit 1
else
puts "<------------ No errors. You\'re good to go! ------------>\n"
end