forked from 2factorauth/twofactorauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify.rb
115 lines (92 loc) · 2.98 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
# Load Yaml
require 'yaml'
require 'fastimage'
@output = 0
# Should the script ignore checking for Twitter handles?
@ignore_twitter = false
# TFA forms
@tfa_forms = %w(email hardware software sms phone)
# Should the script ignore checking the image size?
@ignore_image_size = false
# Image max size (in bytes)
@image_max_size = 2500
begin
# Send error message
def error(msg)
@output += 1
puts "<------------ ERROR ------------>\n" if @output == 1
puts "#{@output}. #{msg}"
end
# Verify that the tfa factors are booleans
def check_tfa(website)
tfa = website['tfa']
if tfa != true && tfa != false
error("#{website['name']} \'tfa\' tag should be either \'Yes\' or \'No\'. (#{tfa})")
end
@tfa_forms.each do |tfa_form|
form = website[tfa_form]
next if form.nil?
unless website['tfa']
error("#{website['name']} should not contain a \'#{tfa_form}\' tag when it doesn\'t support TFA.")
end
unless form
error("#{website['name']} should not contain a \'#{tfa_form}\' tag when it\'s value isn\'t \'YES\'.")
end
end
end
def tags_set(website)
tags = %w(url img name)
tags.each do |t|
tag = website[t]
next unless tag.nil?
error("#{website['name']} doesn\'t contain a \'#{t}\' tag.")
end
if website['tfa']
error("#{website['name']} should not contain a \'status\' tag when it doesn\'t support TFA.") unless website['status'].nil?
else
error("#{website['name']} should not contain a \'doc\' tag when it doesn\'t support TFA.") unless website['doc'].nil?
end
return if @ignore_twitter
twitter = website['twitter']
return if twitter.nil?
return unless website['tfa']
error("#{website['name']} should not contain a \'twitter\' tag as it supports TFA.")
end
def validate_image(image, name)
if File.exist?(image)
image_dimensions = [32, 32]
unless FastImage.size(image) == image_dimensions
error("#{image} is not #{image_dimensions.join('x')}")
end
ext = '.png'
error("#{image} is not #{ext}") unless File.extname(image) == ext
unless @ignore_image_size
image_size = File.size(image)
error("#{image} should not be larger than #{@image_max_size} bytes. It is currently #{image_size} bytes") unless image_size < @image_max_size
end
else
error("#{name} image not found.")
end
end
# Load each section, check for errors such as invalid syntax
# as well as if an image is missing
sections = YAML.load_file('_data/sections.yml')
sections.each do |section|
data = YAML.load_file('_data/' + section['id'] + '.yml')
data['websites'].each do |website|
check_tfa(website)
tags_set(website)
validate_image("img/#{section['id']}/#{website['img']}", website['name'])
end
end
exit 1 if @output > 0
rescue Psych::SyntaxError => e
puts 'Error in the YAML'
puts e
exit 1
rescue => e
puts e
exit 1
else
puts 'No errors. You\'re good to go!'
end