Keychain.ServiceName wrong location

Hello guys.

Can someone test for me please if you get same result on tests before i submit a bug report.

So we have the sample code found in the official documentation

And it says the following

Sample code

The following example adds a KeychainItem for an application and assigns a password.

Var newItem As KeychainItem
If System.KeychainCount > 0 Then
  newItem = New KeychainItem
  ' Indicate the name of the application
  newItem.ServiceName = "MyApplication"

  Try
    ' Create a new keychain item for the application and assign the password
    System.Keychain.AddPassword(newItem, "SecretPassword")
  Catch error As KeychainException
    MessageBox("Can't add item: " + error.Message)
  End Try
Else
  System.Beep
  MessageBox("You don't have a key chain.")
End If

The following example retrieves the password and displays it in a message box.

Var itemToFind As KeychainItem
Var password As String

itemToFind = New KeychainItem
' Indicate the name of the application whose keychain item you wish to find
itemToFind.ServiceName = "MyApplication"

Try
  ' get application's password from the system keychain
  password = System.Keychain.FindPassword(itemToFind)
  MessageBox("The password for this item is: " + password)
Catch Exception error As KeychainException
  MessageBox("Can't find item: " + error.Message)
End Try

Now adding as well the code to remove the keychain item, supposedly .

Var itemToFind As KeychainItem

itemToFind = New KeychainItem
' Indicate the name of the application whose keychain item you wish to find
itemToFind.ServiceName = appName


If itemToFind <> Nil Then
  itemToFind.Remove
  
  MessageBox("Keychain Item Removed. ")
End If

Now, the first issue.

Screenshot 2024-03-10 at 18.56.09

AppNameTest in my case it should be under Name and not under Where.

Then it seems that the delete part does not do anything.

In my case tests were done on MacOS Ventura 13.6.5, M1 MacMini, And XOJO 2023R4

Thanks

Hi @Aurelian_N

In order to get the Keychain item Name set to the expected one (“MyApplication”), you need to set the “Label” property of the new KeychainItem to “MyApplication”; so the correct lines of code would be:

newItem = New KeychainItem
' Indicate the name of the application

newItem.ServiceName = "MyApplication"
newItem.Label = "MyApplication"

As for the second case (removing an existing item with the provided example code), and because how Keychain works, the issue here is that you are creating a new Keychain Item instance… so it won’t be found in the Keychain until you ask it for the password and, thus, the original one is not removed (a different instance with a different persistent handler under the hood); so this is probably a documentation issue.

The way to remove the original (added) keychain item using the existing methods, requires to retrieve the right KeychainItem asking for the password. For example this modified snippet of code would remove it from the Keychain:

Var itemToFind As New KeychainItem

' Indicate the name of the application whose keychain item you wish to find
itemToFind.ServiceName = "MyApplication"

Var pass As String = System.Keychain.FindPassword(itemToFind)

If itemToFind <> Nil And pass <> "" Then
  itemToFind.Remove
  
  MessageBox("Keychain Item Removed. ")
End If

In order to avoid the extra step of asking for the password, you need to keep around a reference to the originally added KeychainItem (for example a property hanging from the Window1 instance); so let’s say that such property is named “AddedKeychainPassword”. The code to add a new password would be in this case:

If System.KeychainCount > 0 Then
  AddedKeyChainPassword = New KeychainItem
  ' Indicate the name of the application
  
  AddedKeyChainPassword.ServiceName = "MyApplication"
  AddedKeyChainPassword.Label = "MyApplication"
  
  ' Create a new keychain item for the application and assign the password
  System.Keychain.AddPassword(AddedKeyChainPassword, "SecretPassword")
Else
  System.Beep
  MessageBox("You don't have a key chain.")
End If

Exception err As KeychainException
  MessageBox("Can't add item: " + err.Message)

And in order to remove it from the Keychain, it would be:

If AddedKeyChainPassword <> Nil Then
  AddedKeyChainPassword.Remove
  
  MessageBox("Keychain Item Removed. ")
End If
3 Likes

Hello @Javier_Menendez , i guess if that is not a. bug then it would be helpful for others to know about those, so update on the docs will be perfect.

Thanks again for clarifying those. I will test as well and see if i have any issues.

1 Like