Finding Keychainitems

I’m having trouble finding a particular keychain item with Xojo. I see the item in my Mac OS keychain item:

In Xojo the code looks like this:

Dim kc As KeyChain
Dim kci As KeyChainItem
for i as integer=0 to system.keychaincount
kc = New KeyChain(i)

If kc <> Nil Then
  kci = New KeyChainItem
  Try
    
    kci.ServiceName = SearchField.Text
    
    PasswordField.Text = kc.FindPassword(kci)//always get a Keychain exception here
    
    ServiceNameField.Text = kci.ServiceName
    AccountNameField.Text = kci.AccountName
    CommentField.Text = kci.Comment
    LabelField.Text = kci.Label
  Catch e As KeyChainException
    MsgBox("Keychain exception: " + e.Message)
  End Try
End If

next

But no matter what I type into the SearchField, I can’t seem to find the item. The problem seems to be simply that I am not sure what the service name actually is (I’ve tried many…). Any pointers?

did you try MBS Plugin as an alternative?

Actually - yes. But to be honest, I couldn’t quite figure out how to access the item with the MBS-Commands either.

If you could post an example that shows how to access a particular keychain item with just the info given in the screenshot…that’d be great.

This is from the docs and use the Systems keychain

[code]
Dim ItemToFind as KeyChainItem
Dim password As String

ItemToFind = New KeyChainItem

'Indicate the name of the application whose keychain item you wish to find
ItemToFind.ServiceName = “MyApplication”

'get application’s password from the system keychain
password = System.KeyChain.FindPassword(ItemToFind)
MsgBox "The password for this item is: " + password

Exception err as KeyChainException
MsgBox "Can’t find item: " + err.Message[/code]

Yeah, but look at the screenshot. What exactly is the service name?

For the skype app I used

ItemToFind.ServiceName = "skype"

Have a look into your KeyChain and you can see the name listed in the table “Passwords” in the column “name”.

In the above case it could be
www.xing.com (maximil…), please type as shown in your picture above

But important is this:

System.KeyChain.FindPassword(ItemToFind)

If you have more keychains you can iterate through them.

The “skype” app is in my keychain “Anmeldung” with the name “skype” and the Art “Programmkennwort”.

Yeah, tried both of those to no avail. Do you happen to have those Xing-Keychain items by any chance? Does it work for you?

this gives password:

[code] // build query
dim query as new Dictionary

dim ServiceName as string = “mytest”
dim Username as string = “myusername”

query.value( KeychainManagerMBS.kSecAttrAccount ) = “email@email”
query.value( KeychainManagerMBS.kSecAttrServer ) = “www.xing.com
query.value( KeychainManagerMBS.kSecClass ) = KeychainManagerMBS.kSecClassInternetPassword
query.value( KeychainManagerMBS.kSecAttrProtocol ) = KeychainManagerMBS.kSecAttrProtocolHTTPS

// query password
dim passwordData as MemoryBlock
if KeychainManagerMBS.GetPassword(query, passwordData) then
// and show it
dim password as string = DefineEncoding(passwordData, Encodings.utf8)
MsgBox "OK: "+password
else
MsgBox “Failed.”
end if
[/code]
is that enough?

Thank you.

I tried that with
dim ServiceName as string = “www.xing.com” // (also with “www.xing.com (maximilian.tyrtania@contactking.de)”)
dim Username as string = “maximilian.tyrtania@contactking.de
.
.

…but I always get “failed”. :frowning:

permissions? Did you allow user interaction? Because I got a dialog asking if it is allowed.

Sorry, it doesn’t works with “Internetkennwort”.
I added a Xing account to the keychain as “Internetkennwort” but couldn’t access it. I tried different things and looked again into the docs. There I found the following info:

“KeyChain
Gives you access to the default Mac OS X Keychains for your applications. It is a Macintosh-only feature. The KeyChain class does not provide access to internet passwords.”

Ah, that makes sense. No, I didn’t get asked. But I was also wondering if an app should really be able access all internet passwords without user interaction (shudder…).

@Torsten Gaidies : Thanks for investigating. So the gist of it is: An App can access application keywords (as you did with Skype) but not internet keywords.

Okay, will think of a different approach then.

An application can access them. It works for me.

You can of course disable user interaction, see UserInteractionAllowed property in KeychainManagerMBS.

So if it fails with UserInteractionAllowed = false, you could of course try again with UserInteractionAllowed = True and let the user allow your app.

Here is my answer again, firstly my post was doubbled and as i deleted on of them, both seemed to be deleted after I logged out from the forum:

The Xojo framework doesn’t support access to internet keywords directly. With declares or the MBS plugin it should be possible (never tested it by myself).

Well, I didn’t do that. And adding

KeychainManagerMBS.UserInteractionAllowed=true

at the top of my code makes no difference. I don’t get asked, and it just fails. Just tested it again on a different machine. BTW, I’m on Yosemite (10.10.1).