Module: WinCred
- Defined in:
- lib/wincred/wincred.rb,
lib/wincred/native.rb,
lib/wincred/conversion.rb
Overview
This module provides a high-level API for interations with the Windows Credential Manager
Defined Under Namespace
Modules: Conversion, Native
Class Method Summary
collapse
Class Method Details
.delete_credential(target) ⇒ Object
109
110
111
112
113
114
115
116
117
|
# File 'lib/wincred/wincred.rb', line 109
def delete_credential(target)
delete_result = Native.CredDeleteW(
target.encode('utf-16le'),
Native::CRED_TYPE_GENERIC,
Native::FLAGS_NONE,
)
raise "Delete from WinCred failed. Error code: #{Native.GetLastError}" if delete_result.zero?
end
|
.enumerate_credentials ⇒ Object
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
|
# File 'lib/wincred/wincred.rb', line 25
def enumerate_credentials
pp_credentials = Fiddle::Pointer.new 0
cred_count_ptr = Fiddle::Pointer.malloc(Fiddle::SIZEOF_LONG, Fiddle::RUBY_FREE)
enum_result = Native.CredEnumerateW(
nil,
Native::FLAGS_NONE,
cred_count_ptr,
pp_credentials.ref,
)
if enum_result.zero?
raise "Enumerate credentials in WinCred failed. Error code: #{Native.GetLastError}"
end
count = cred_count_ptr[0, 4].unpack('L').first
count.times.map do |ndx|
cred_ptr = (pp_credentials + (ndx * Fiddle::SIZEOF_VOIDP)).ptr
cred = Native::CREDENTIALW.new(cred_ptr)
cred_to_hash(cred)
end
ensure
Native.CredFree(pp_credentials) if pp_credentials
end
|
.exist?(target) ⇒ Boolean
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/wincred/wincred.rb', line 10
def exist?(target)
cred_ptr = Fiddle::Pointer.new 0
read_result = Native.CredReadW(
target.encode('utf-16le'),
Native::CRED_TYPE_GENERIC,
Native::FLAGS_NONE,
cred_ptr.ref,
)
read_result.positive?
ensure
Native.CredFree(cred_ptr) if cred_ptr
end
|
.read_credential(target) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/wincred/wincred.rb', line 90
def read_credential(target)
cred_ptr = Fiddle::Pointer.new 0
read_result = Native.CredReadW(
target.encode('utf-16le'),
Native::CRED_TYPE_GENERIC,
Native::FLAGS_NONE,
cred_ptr.ref,
)
raise "Read from WinCred failed. Error code: #{Native.GetLastError}" if read_result.zero?
cred = Native::CREDENTIALW.new(cred_ptr)
cred_to_hash(cred)
ensure
Native.CredFree(cred_ptr) if cred_ptr
end
|
.write_credential(target:, username:, value:) ⇒ Object
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
|
# File 'lib/wincred/wincred.rb', line 59
def write_credential(
target:,
username:,
value: )
if value.bytes.size != value.size
raise "Write to WinCred failed. Value is not a binary string. Encoding is (#{value.encoding})."
end
cred = Native::CREDENTIALW.malloc
cred.Flags = Native::FLAGS_NONE
cred.Type = Native::CRED_TYPE_GENERIC
cred.TargetName = target.encode('utf-16le')
cred.Comment = ''
cred.CredentialBlobSize = value.size
cred.CredentialBlob = Fiddle::Pointer[value]
cred.Persist = Native::CRED_PERSIST_LOCAL_MACHINE
cred.AttributeCount = 0
cred.Attributes = nil
cred.TargetAlias = nil
cred.UserName = username.encode('utf-16le')
write_result = Native.CredWriteW(Fiddle::Pointer[cred], 0)
raise "Write to WinCred failed. Error code: #{Native.GetLastError}" if write_result.zero?
ensure
Fiddle.free(cred.to_i) if cred
end
|