-
Notifications
You must be signed in to change notification settings - Fork 32
/
GNKeychain.rb
60 lines (55 loc) · 1.15 KB
/
GNKeychain.rb
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
#
# GNKeychain.rb
# Gmail Notifr
#
# Created by james on 10/4/08.
# Copyright (c) 2008 ashchan.com. All rights reserved.
#
require 'osx/cocoa'
OSX.require_framework 'Security'
class GNKeychain < OSX::NSObject
include OSX
SERVICE = "GmailNotifr"
def set_account(username, password)
return if username.nil? || password.nil?
pass = "#{password}"
error = SecKeychainAddGenericPassword(
nil,
SERVICE.length,
SERVICE,
username.length,
username,
pass.length,
pass,
nil)
#already set
if error == OSX::ErrSecDuplicateItem
status, *data = SecKeychainFindGenericPassword(
nil,
SERVICE.length,
SERVICE,
username.length,
username)
data.shift
data.shift
item = data.shift #SecKeychainItemRef
SecKeychainItemModifyContent(item, nil, pass.length, pass)
end
end
def get_password(username)
return "" if username.nil?
status, *data = SecKeychainFindGenericPassword(
nil,
SERVICE.length,
SERVICE,
username.length,
username)
if status == 0
password_length = data.shift
password_data = data.shift
password = password_data.bytestr(password_length)
else
""
end
end
end