nomad-cli / houston

Apple Push Notifications; No Dirigible Required
http://nomad-cli.com
MIT License
2.93k stars 229 forks source link

setting env var as certificate doesn't work #88

Closed mehulkar closed 9 years ago

mehulkar commented 9 years ago

This might be an issue of better documentation, but as a first time user of houston and push notifications, I had the following problem:

I used dotenv to set the environment variable:

APN_CERTIFICATE='/Users/mehulkar/dev/myproj/api/cert.pem'

in an Rails initializer, I create the client, assuming that the env vars will be used automatically.

APN = Houston::Client.new

Now, when I open up rails console and try to send a push, I get this error.

notification = Houston::Notification.new(token: 'foo')
APN.push(notification)
#=> OpenSSL::PKey::RSAError: Neither PUB key nor PRIV key: not enough data

Looking at the code, this seems obvious to me because Houston::Client.development, for example, sets the certificate accessor to the contents of the file, rather than the path

When I reassign APN.certificate=File.read(APN.certificate), then it works.

There are a few of solutions:

  1. Document that this reassignment needs to happen
  2. Use a different ruby accessor like certificate_path and on initialize, attempt to read that if @certificate is nil
  3. auto reassign APN.certificate if FIle.exists? APN.certificate is true either in Client#push or in Client#new

Let me know if I've missed something obvious or if I'm completely wrong here.

akihitoalextsuboi commented 9 years ago

I've got a same problem. And, I reassign APN.certificate=File.read(APN.certificate) but, I've got

Errno::ENAMETOOLONG: File name too long @ rb_sysopen

How can I fix it?

mehulkar commented 9 years ago

@akihitotsuboi that sounds like a general Ruby issue. Perhaps the contents of your certificate file are too big? Or the filename is to long?

akihitoalextsuboi commented 9 years ago

@mehulkar my problem was maybe different from users. I solved my issue like this. Thank you.

need passphrase when create .pem from .cer with passphrase, or need .pem without passphrase need to create .pem from .p12 from .cer

Invalid .pem file

when .pem has passphrase

in command

apn push token -c /path/ -m 'hello' -p
enter password

in method

apn.certificate = File.read('/path')
apn.passphrase = 'password'
mattt commented 9 years ago

Fixed by 70e245f. Now if a valid certificate path is passed into APN_CERTIFICATE, it will be read as expected. If that file does not exist, an exception will be raised.

mehulkar commented 9 years ago

@mattt does this warrant a patch update on rubygems? I try to be very specific about shipping dependency versions.

Thank you for the fix!

mattt commented 9 years ago

Yep. See version 2.2.2.

mehulkar commented 9 years ago

Thanks!