~singpolyma/sgx-jmp

ref: f571b5e726750cf8c4523c22d77e110194c3e16a sgx-jmp/lib/sip_account.rb -rw-r--r-- 2.0 KiB
f571b5e7Stephen Paul Weber Postgres#query_one 1 year, 7 months ago
                                                                                
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
# frozen_string_literal: true

require "digest"
require "securerandom"
require "value_semantics/monkey_patched"

require_relative "mn_words"

class SipAccount
	def self.find(name)
		new(BandwidthIris::SipCredential.get(name))
	rescue BandwidthIris::Errors::GenericError # 404
		New.new(BandwidthIris::SipCredential.new(
			user_name: name,
			realm: CONFIG[:sip][:realm],
			http_voice_v2_app_id: CONFIG[:sip][:app]
		))
	end

	def initialize(api_object, password: nil)
		@api_object = api_object
		@password = password
	end

	def with(password:)
		self.class.new(@api_object.class.new(@api_object.to_data.merge(
			hash1: Digest::MD5.hexdigest("#{username}:#{server}:#{password}"),
			hash1b: Digest::MD5.hexdigest(
				"#{username}:#{server}:#{server}:#{password}"
			)
		)), password: password)
	end

	def with_random_password
		with(password: MN_WORDS.sample(3).join(" "))
	end

	PORT = {
		var: "port",
		type: :fixed,
		value: "5008",
		label: "Port",
		description: "usually optional"
	}.freeze

	def form
		form = Blather::Stanza::X.new(:result)
		form.title = "Sip Account Reset!"
		form.instructions = "These are your new SIP credentials"

		form.fields = [
			{ var: "username", value: username, label: "Username", type: :fixed },
			{ var: "password", value: @password, label: "Password", type: :fixed },
			{ var: "server", value: server, label: "Server", type: :fixed },
			PORT
		]

		form
	end

	def put
		@api_object.update(
			hash1: @api_object.hash1,
			hash1b: @api_object.hash1b,
			realm: server,
			http_voice_v2_app_id: @api_object.http_voice_v2_app_id
		)
		self
	end

	def delete
		@api_object.delete
	end

	def username
		@api_object.user_name.to_s
	end

	def server
		@api_object.realm
	end

	def uri
		"sip:#{username}@#{server}"
	end

	class New < SipAccount
		def put
			BandwidthIris::SipCredential.create(
				user_name: username,
				hash1: @api_object.hash1,
				hash1b: @api_object.hash1b,
				realm: server,
				http_voice_v2_app_id: @api_object.http_voice_v2_app_id
			)
			self
		end
	end
end