~singpolyma/sgx-jmp

ref: 7b6c0a067d230cf77abc3e796fd2649cab1ced4d sgx-jmp/lib/sip_account.rb -rw-r--r-- 1.8 KiB
7b6c0a06Stephen Paul Weber Reset sip account using v2 API 11 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
# 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

	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" },
			{ var: "password", value: @password, label: "Password" },
			{ var: "server", value: server, label: "Server" }
		]

		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

	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