0

I'm trying to retrieve a password from a keychain with rubyMotion, on OS X

I tried this :

#   passsword_data_pointer=Pointer.new(:object) #works but empty password
#   password_data_pointer=Pointer.new('^') #makes ruby crash and complain 'Can't find pointer description for type '^'
    password_data=NSMutableData.new #works but empty password

    password_length = Pointer.new('I')
    result=SecKeychainFindGenericPassword (
                                           nil,
                                           "some_service_string".length,
                                           "some_service_string",
                                           "some_username_string".length,
                                           "some_username_string",
                                           password_length,
                                           password_data_pointer,#or password_data.bytes
                                           nil
                                           )

#    password_string=NSMutableData.dataWithBytes(password_data.bytes, length:password_length[0])
    password_string=NSMutableData.dataWithBytes(password_data_pointer, length:password_length[0])

    p password_string

No matter what I do, no way to retrieve the password.

Please help ; searched for ages, the Internet is full of macruby or cocoa or c examples, but nothing for rubymotion on this topic.

MichaelC
  • 357
  • 2
  • 12

1 Answers1

1

I'm not too familiar with SecKeychainFindGenericPassword but I know that you need to set the proper entitlement to use the Keychain as discussed in the RubyMotion Project Management Guide.

So you make sure you have the following line in your Rakefile:

app.entitlements['keychain-access-groups'] = [
  app.seed_id + '.' + app.identifier
]

If you'd like a nicer interface to the Keychain, I use the SSKeychain cocoa wrapper which can be pulled in via Cocoapods.

In your Gemfile:

gem 'cocoapods',        '~> 0.23.0'
gem 'motion-cocoapods', '~> 1.3.6'

Also in the Rakefile:

app.pods do
  pod 'SSKeychain', '~> 1.2.0'
end

Here's a simplified version of the wrapper I use for storing and retrieving sensitive data with SSKeychain:

class CredentialStore
  SERVICE = 'YOUR_APP_NAME'

  def set_secure_value(value, for_key: key)
    if value
      SSKeychain.setPassword(value, forService: SERVICE, account: key)
    else
      SSKeychain.deletePasswordForService(SERVICE, account: key)
    end
  end

  def secure_value_for_key(key)
    SSKeychain.passwordForService(SERVICE, account: key)
  end
end

Let me know if you have any further questions. Good luck!

Devon
  • 118
  • 7