1# frozen_string_literal: true
2
3require "digest"
4require "securerandom"
5require "value_semantics/monkey_patched"
6
7require_relative "bandwidth_iris_patch"
8require_relative "mn_words"
9
10class SipAccount
11 def self.find(customer_id)
12 new(BandwidthIris::SipCredential.get("c#{customer_id}"))
13 rescue BandwidthIris::APIError # 404
14 New.new(BandwidthIris::SipCredential.new(
15 user_name: "c#{customer_id}",
16 realm: CONFIG[:sip][:realm],
17 http_voice_v2_app_id: CONFIG[:sip][:app]
18 ))
19 end
20
21 def initialize(api_object, password: nil)
22 @api_object = api_object
23 @password = password
24 end
25
26 def with(password:)
27 self.class.new(@api_object.class.new(@api_object.to_data.merge(
28 hash1: Digest::MD5.hexdigest("#{username}:#{server}:#{password}"),
29 hash1b: Digest::MD5.hexdigest(
30 "#{username}:#{server}:#{server}:#{password}"
31 )
32 )), password: password)
33 end
34
35 def with_random_password
36 with(password: MN_WORDS.sample(3).join(" "))
37 end
38
39 PORT = {
40 var: "port",
41 type: :fixed,
42 value: "5008",
43 label: "Port",
44 description: "usually optional"
45 }.freeze
46
47 def form
48 form = Blather::Stanza::X.new(:result)
49 form.title = "Sip Account Reset!"
50 form.instructions = "These are your new SIP credentials"
51
52 form.fields = [
53 { var: "username", value: username, label: "Username", type: :fixed },
54 { var: "password", value: @password, label: "Password", type: :fixed },
55 { var: "server", value: server, label: "Server", type: :fixed },
56 PORT
57 ]
58
59 form
60 end
61
62 def put
63 @api_object.update(
64 hash1: @api_object.hash1,
65 hash1b: @api_object.hash1b,
66 realm: server,
67 http_voice_v2_app_id: @api_object.http_voice_v2_app_id
68 )
69 self
70 end
71
72 def delete
73 @api_object.delete
74 end
75
76 def username
77 @api_object.user_name.to_s
78 end
79
80 def server
81 @api_object.realm
82 end
83
84 def uri
85 "sip:#{username}@#{server}"
86 end
87
88 class New < SipAccount
89 def put
90 BandwidthIris::SipCredential.create(
91 user_name: username,
92 hash1: @api_object.hash1,
93 hash1b: @api_object.hash1b,
94 realm: server,
95 http_voice_v2_app_id: @api_object.http_voice_v2_app_id
96 )
97 self
98 end
99 end
100end