masked-email 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/masked-email +6 -0
  3. data/lib/masked-email.rb +140 -0
  4. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 17797fd673f4ecabcefb191a9021022a2f1a8d84902692b2ba2a7db0ba189095
4
+ data.tar.gz: 0f291eb60ffe522aee66dc8ff1b9fa435c27bafefc8ab68b724965517b7d3b3f
5
+ SHA512:
6
+ metadata.gz: f311a4312c6fd4f0cfd1db24827d9814952fbda4ab2bd7caf58f95dede886f1dbd190a30f81c3d7e6030d8fd23164b415ec786475918dd763bbc7e4a1688c501
7
+ data.tar.gz: a0c7c42ca3d4f0fc3ae4038e95d6d94d4575370e98f4b94781b2691ec49dc1a5a9262142e3ee466b6215d2692fb31ce98df03370921d838197fcceceb58aa24b
data/bin/masked-email ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'masked-email'
4
+
5
+ masked_email = MaskedEmail.new
6
+ masked_email.run
@@ -0,0 +1,140 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+ require 'openssl'
4
+ require 'json'
5
+ require 'optparse'
6
+
7
+ class MaskedEmail
8
+ BASE_URL = 'https://api.fastmail.com/'
9
+ API_KEY_FILE = File.expand_path('~/.fastmail-api-key')
10
+
11
+ SET_METHOD = 'MaskedEmail/set'
12
+ MASKEDEMAIL = 'https://www.fastmail.com/dev/maskedemail'
13
+ APPLICATION_JSON_CONTENT_TYPE = 'application/json; charset=utf-8'
14
+
15
+ def initialize
16
+ url = URI(BASE_URL)
17
+ @http = Net::HTTP.new(url.host, url.port)
18
+ @http.use_ssl = true
19
+ end
20
+
21
+ def run
22
+ parse_options
23
+ validate_options
24
+ setup_api_token
25
+
26
+ session = fetch_session_map
27
+ account_id = session['primaryAccounts'][MASKEDEMAIL]
28
+ api_url = session['apiUrl']
29
+
30
+ exit if @options[:dry_run]
31
+
32
+ # create masked email
33
+ method_id = 'k1'
34
+ response = create_masked_email(account_id, api_url, method_id)
35
+ method_response = response['methodResponses'].find { _1[0] == SET_METHOD }
36
+ email = method_response[1]['created'][method_id]['email']
37
+ puts "Masked email created: #{email}"
38
+ end
39
+
40
+ private
41
+
42
+ # parse command-line options
43
+ def parse_options
44
+ @options = {}
45
+ OptionParser.new do |parser|
46
+ parser.banner = 'Usage: masked-email [options]'
47
+
48
+ parser.on('-d', '--domain DOMAIN', 'Domain to create email for') do |d|
49
+ @options[:domain] = d
50
+ end
51
+
52
+ parser.on('-c', '--credentials FILE', 'Credentials file') do |c|
53
+ @options[:credentials] = c
54
+ end
55
+
56
+ parser.on('-v', '--[no-]verbose', 'Run verbosely') do |v|
57
+ @options[:verbose] = v
58
+ end
59
+
60
+ parser.on('', '--dry-run', 'Dry run') do |dr|
61
+ @options[:dry_run] = dr
62
+ end
63
+
64
+ parser.on('-h', '--help', 'Prints this help') do
65
+ puts parser
66
+ exit
67
+ end
68
+
69
+ end.parse!
70
+
71
+ @options
72
+ end
73
+
74
+ def validate_options
75
+ if @options[:domain].nil?
76
+ puts 'Domain required, see --help'
77
+ exit(1)
78
+ end
79
+
80
+ unless URI::regexp.match?(@options[:domain])
81
+ puts 'Domain must be valid URI'
82
+ exit(1)
83
+ end
84
+ end
85
+
86
+ def setup_api_token
87
+ @api_token =
88
+ if @options[:credentials]
89
+ IO.readlines(@options[:credentials]).first.chomp
90
+ elsif ENV['FASTMAIL_API_KEY']
91
+ ENV['FASTMAIL_API_KEY']
92
+ elsif File.exist?(API_KEY_FILE)
93
+ IO.readlines(API_KEY_FILE).first.chomp
94
+ else
95
+ raise 'No credentials found, see --help'
96
+ end
97
+ end
98
+
99
+ # fetch account information
100
+ def fetch_session_map
101
+ url = URI("#{BASE_URL}jmap/session")
102
+ request = Net::HTTP::Get.new(url)
103
+ request['Content-Type'] = APPLICATION_JSON_CONTENT_TYPE
104
+ request["Authorization"] = "Bearer #{@api_token}"
105
+ response = @http.request(request)
106
+ body = response.read_body
107
+ puts body if @options[:verbose]
108
+
109
+ JSON.parse(body)
110
+ end
111
+
112
+ def create_masked_email(account_id, api_url, method_id)
113
+ request = Net::HTTP::Post.new(api_url)
114
+ request['Content-Type'] = APPLICATION_JSON_CONTENT_TYPE
115
+ request['Authorization'] = "Bearer #{@api_token}"
116
+ request.body = {
117
+ using: [MASKEDEMAIL],
118
+ methodCalls: [
119
+ [
120
+ SET_METHOD,
121
+ {
122
+ accountId: account_id,
123
+ create: {
124
+ method_id => {
125
+ state: 'enabled',
126
+ forDomain: @options[:domain]
127
+ }
128
+ }
129
+ },
130
+ 'a'
131
+ ]
132
+ ]
133
+ }.to_json
134
+ response = @http.request(request)
135
+ body = response.read_body
136
+ puts body if @options[:verbose]
137
+
138
+ JSON.parse(body)
139
+ end
140
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: masked-email
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Watts
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-10-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple command-line script for generating masked email addresses via
14
+ Fastmail
15
+ email:
16
+ executables:
17
+ - masked-email
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - bin/masked-email
22
+ - lib/masked-email.rb
23
+ homepage: https://github.com/mikwat/masked-email
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubygems_version: 3.4.10
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Masked Email
46
+ test_files: []