kreait / laravel-firebase

A Laravel package for the Firebase PHP Admin SDK
https://github.com/kreait/firebase-php
MIT License
994 stars 163 forks source link

Correct way to put credentials.json directly into .env variable #68

Closed chuckienorton closed 3 years ago

chuckienorton commented 3 years ago

This excellent package works great for me when referencing the credentials via file in the .env file. However, we use Heroku for staging/production and only have access to string environment variables.

Following this closed issue #63

I used JQ to minify my file, and then placed it into the .env like this:

FIREBASE_CREDENTIALS={"type":"service_account","project_id" ...

This works without the private key, but with the full json which includes '"private_key":"-----BEGIN PRIVATE KEY-----\n' it throws the following laravel application error: "Failed to parse dotenv file due to unexpected whitespace."

If i enclose the entire thing in apostrophes like this:

FIREBASE_CREDENTIALS='{"type":"service_account","project_id" ... '

I get the following error: "Invalid service account: json_decode error: Control character error, possibly incorrectly encoded"

If i remove the apostrophes so the json can correctly decode, and then attempt to change the private key at all - escaping the spaces or anything like this:

FIREBASE_CREDENTIALS={"type":"service_account","private_key":"-----BEGIN\ PRIVATE\ KEY-----\

i get the following error response when trying to use the service: "openssl_sign(): supplied key param cannot be coerced into a private key",…}"

What is the correct format for the FIREBASE_CREDENTIALS variable?

Thanks in advance!!

jeromegamez commented 3 years ago

It works for me when I put the output of jq -c . < /path/to/service_account.json in single quotes:

FIREBASE_CREDENTIALS='{"type":"service_account",...}'

My IDE highlights it as a warning, but Laravel doesn't seem to have an issue with it 😅

image

chuckienorton commented 3 years ago

Thanks!

Yes I tried with single quotes and laravel works, but then I receive a certificate error when the package connects to google. "Invalid service account: json_decode error: Control character error, possibly incorrectly encoded"

It’s useful to know that it’s working for you like that. Now I’ll need to figure out what’s different in my system.

jeromegamez commented 3 years ago

Which versions of Laravel, the SDK and the package are you using?

Also, can you ensure that nothing is escaped in the minified JSON except the line breaks inside the private key? My JSON looks like this:

FIREBASE_CREDENTIALS='{"type":"service_account","project_id":"my-project-id","private_key_id":"1a2b3c4d5e6f7g8h9i","private_key":"-----BEGIN PRIVATE KEY-----\nxxx\n-----END PRIVATE KEY-----\n","client_email":"client@email.tld","client_id":"123456789","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_x509_cert_url":"https://www.googleapis.com/robot/v1/metadata/x509/my-project-id%40my-project-id.iam.gserviceaccount.com"}'
chuckienorton commented 3 years ago

Thanks - sorry for slow weekend response.

Laravel 6.18.35 kreait/firebase-php 5.9.0 kreait/laravel-firebase 2.4.0 laravel-notification-channels/fcm 2.1.1

Here are the credentials

FIREBASE_CREDENTIALS='{"type":"service_account","project_id":"my-project-id","private_key_id":"my-private-key-id","private_key":"-----BEGIN PRIVATE KEY-----\nMIIXXXX1n\nXXXX....w==\n-----END PRIVATE KEY-----\n","client_email":"myclientemail","client_id":"clientid","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_x509_cert_url":"https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-647mw%40projectid.iam.gserviceaccount.com"}'

here is the network error from the browser:

Screen Shot 2020-11-09 at 9 29 57 AM

and here is the error from the logs:

[2020-11-09 14:27:41] local.ERROR: Invalid service account: json_decode error: Control character error, possibly incorrectly encoded {"userId":2,"exception":"[object] (Kreait\\Firebase\\Exception\\InvalidArgumentException(code: 0): Invalid service account: json_decode error: Control character error, possibly incorrectly encoded at /home/vagrant/luvo.care/vendor/kreait/firebase-php/src/Firebase/ServiceAccount.php:66)
[stacktrace]
#0 /home/vagrant/luvo.care/vendor/kreait/firebase-php/src/Firebase/Factory.php(134): Kreait\\Firebase\\ServiceAccount::fromValue()
#1 /home/vagrant/luvo.care/vendor/kreait/laravel-firebase/src/ServiceProvider.php(94): Kreait\\Firebase\\Factory->withServiceAccount()
#2 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Container/Container.php(799): Kreait\\Laravel\\Firebase\\ServiceProvider->Kreait\\Laravel\\Firebase\\{closure}()
#3 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Container/Container.php(681): Illuminate\\Container\\Container->build()
#4 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(785): Illuminate\\Container\\Container->resolve()
#5 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Container/Container.php(629): Illuminate\\Foundation\\Application->resolve()
#6 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(770): Illuminate\\Container\\Container->make()
#7 /home/vagrant/luvo.care/vendor/kreait/laravel-firebase/src/ServiceProvider.php(69): Illuminate\\Foundation\\Application->make()
#8 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Container/Container.php(799): Kreait\\Laravel\\Firebase\\ServiceProvider::Kreait\\Laravel\\Firebase\\{closure}()
#9 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Container/Container.php(681): Illuminate\\Container\\Container->build()
#10 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(785): Illuminate\\Container\\Container->resolve()
#11 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Container/Container.php(629): Illuminate\\Foundation\\Application->resolve()
#12 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(770): Illuminate\\Container\\Container->make()
#13 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Container/Container.php(1245): Illuminate\\Foundation\\Application->make()
#14 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(198): Illuminate\\Container\\Container->offsetGet()
#15 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(166): Illuminate\\Support\\Facades\\Facade::resolveFacadeInstance()
#16 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(255): Illuminate\\Support\\Facades\\Facade::getFacadeRoot()
#17 /home/vagrant/luvo.care/vendor/laravel-notification-channels/fcm/src/FcmChannel.php(78): Illuminate\\Support\\Facades\\Facade::__callStatic()
#18 /home/vagrant/luvo.care/vendor/laravel-notification-channels/fcm/src/FcmChannel.php(57): NotificationChannels\\Fcm\\FcmChannel->sendToFcm()
#19 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php(148): NotificationChannels\\Fcm\\FcmChannel->send()
#20 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php(106): Illuminate\\Notifications\\NotificationSender->sendToNotifiable()
#21 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Support/Traits/Localizable.php(19): Illuminate\\Notifications\\NotificationSender->Illuminate\\Notifications\\{closure}()
#22 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php(109): Illuminate\\Notifications\\NotificationSender->withLocale()
#23 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php(79): Illuminate\\Notifications\\NotificationSender->sendNow()
#24 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Notifications/ChannelManager.php(39): Illuminate\\Notifications\\NotificationSender->send()
#25 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Notifications/RoutesNotifications.php(18): Illuminate\\Notifications\\ChannelManager->send()
#26 /home/vagrant/luvo.care/app/Models/Chat.php(160): App\\Models\\User->notify()
#27 /home/vagrant/luvo.care/app/Http/Controllers/Api/ChatsController.php(282): App\\Models\\Chat->notifySubscribers()
#28 [internal function]: App\\Http\\Controllers\\Api\\ChatsController->addMessage()
#29 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array()
#30 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\\Routing\\Controller->callAction()
#31 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Routing/Route.php(219): Illuminate\\Routing\\ControllerDispatcher->dispatch()
#32 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Routing/Route.php(176): Illuminate\\Routing\\Route->runController()
#33 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Routing/Router.php(681): Illuminate\\Routing\\Route->run()
#34 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(130): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}()
#35 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#36 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle()
#37 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(59): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#38 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Routing\\Middleware\\ThrottleRequests->handle()
#39 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(43): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#40 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Auth\\Middleware\\Authenticate->handle()
#41 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(105): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#42 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Routing/Router.php(683): Illuminate\\Pipeline\\Pipeline->then()
#43 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Routing/Router.php(658): Illuminate\\Routing\\Router->runRouteWithinStack()
#44 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Routing/Router.php(624): Illuminate\\Routing\\Router->runRoute()
#45 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Routing/Router.php(613): Illuminate\\Routing\\Router->dispatchToRoute()
#46 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(170): Illuminate\\Routing\\Router->dispatch()
#47 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(130): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}()
#48 /home/vagrant/luvo.care/vendor/barryvdh/laravel-debugbar/src/Middleware/InjectDebugbar.php(65): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#49 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Barryvdh\\Debugbar\\Middleware\\InjectDebugbar->handle()
#50 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#51 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle()
#52 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#53 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle()
#54 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#55 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle()
#56 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(63): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#57 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle()
#58 /home/vagrant/luvo.care/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#59 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Fideloper\\Proxy\\TrustProxies->handle()
#60 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(105): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#61 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(145): Illuminate\\Pipeline\\Pipeline->then()
#62 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(110): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter()
#63 /home/vagrant/luvo.care/public/index.php(55): Illuminate\\Foundation\\Http\\Kernel->handle()
#64 {main}

[previous exception] [object] (Kreait\\Firebase\\Exception\\InvalidArgumentException(code: 0): json_decode error: Control character error, possibly incorrectly encoded at /home/vagrant/luvo.care/vendor/kreait/firebase-php/src/Firebase/Util/JSON.php:63)
[stacktrace]
#0 /home/vagrant/luvo.care/vendor/kreait/firebase-php/src/Firebase/ServiceAccount.php(105): Kreait\\Firebase\\Util\\JSON::decode()
#1 /home/vagrant/luvo.care/vendor/kreait/firebase-php/src/Firebase/ServiceAccount.php(61): Kreait\\Firebase\\ServiceAccount::fromJson()
#2 /home/vagrant/luvo.care/vendor/kreait/firebase-php/src/Firebase/Factory.php(134): Kreait\\Firebase\\ServiceAccount::fromValue()
#3 /home/vagrant/luvo.care/vendor/kreait/laravel-firebase/src/ServiceProvider.php(94): Kreait\\Firebase\\Factory->withServiceAccount()
#4 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Container/Container.php(799): Kreait\\Laravel\\Firebase\\ServiceProvider->Kreait\\Laravel\\Firebase\\{closure}()
#5 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Container/Container.php(681): Illuminate\\Container\\Container->build()
#6 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(785): Illuminate\\Container\\Container->resolve()
#7 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Container/Container.php(629): Illuminate\\Foundation\\Application->resolve()
#8 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(770): Illuminate\\Container\\Container->make()
#9 /home/vagrant/luvo.care/vendor/kreait/laravel-firebase/src/ServiceProvider.php(69): Illuminate\\Foundation\\Application->make()
#10 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Container/Container.php(799): Kreait\\Laravel\\Firebase\\ServiceProvider::Kreait\\Laravel\\Firebase\\{closure}()
#11 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Container/Container.php(681): Illuminate\\Container\\Container->build()
#12 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(785): Illuminate\\Container\\Container->resolve()
#13 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Container/Container.php(629): Illuminate\\Foundation\\Application->resolve()
#14 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(770): Illuminate\\Container\\Container->make()
#15 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Container/Container.php(1245): Illuminate\\Foundation\\Application->make()
#16 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(198): Illuminate\\Container\\Container->offsetGet()
#17 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(166): Illuminate\\Support\\Facades\\Facade::resolveFacadeInstance()
#18 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(255): Illuminate\\Support\\Facades\\Facade::getFacadeRoot()
#19 /home/vagrant/luvo.care/vendor/laravel-notification-channels/fcm/src/FcmChannel.php(78): Illuminate\\Support\\Facades\\Facade::__callStatic()
#20 /home/vagrant/luvo.care/vendor/laravel-notification-channels/fcm/src/FcmChannel.php(57): NotificationChannels\\Fcm\\FcmChannel->sendToFcm()
#21 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php(148): NotificationChannels\\Fcm\\FcmChannel->send()
#22 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php(106): Illuminate\\Notifications\\NotificationSender->sendToNotifiable()
#23 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Support/Traits/Localizable.php(19): Illuminate\\Notifications\\NotificationSender->Illuminate\\Notifications\\{closure}()
#24 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php(109): Illuminate\\Notifications\\NotificationSender->withLocale()
#25 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php(79): Illuminate\\Notifications\\NotificationSender->sendNow()
#26 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Notifications/ChannelManager.php(39): Illuminate\\Notifications\\NotificationSender->send()
#27 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Notifications/RoutesNotifications.php(18): Illuminate\\Notifications\\ChannelManager->send()
#28 /home/vagrant/luvo.care/app/Models/Chat.php(160): App\\Models\\User->notify()
#29 /home/vagrant/luvo.care/app/Http/Controllers/Api/ChatsController.php(282): App\\Models\\Chat->notifySubscribers()
#30 [internal function]: App\\Http\\Controllers\\Api\\ChatsController->addMessage()
#31 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array()
#32 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\\Routing\\Controller->callAction()
#33 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Routing/Route.php(219): Illuminate\\Routing\\ControllerDispatcher->dispatch()
#34 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Routing/Route.php(176): Illuminate\\Routing\\Route->runController()
#35 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Routing/Router.php(681): Illuminate\\Routing\\Route->run()
#36 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(130): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}()
#37 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#38 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle()
#39 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(59): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#40 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Routing\\Middleware\\ThrottleRequests->handle()
#41 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(43): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#42 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Auth\\Middleware\\Authenticate->handle()
#43 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(105): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#44 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Routing/Router.php(683): Illuminate\\Pipeline\\Pipeline->then()
#45 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Routing/Router.php(658): Illuminate\\Routing\\Router->runRouteWithinStack()
#46 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Routing/Router.php(624): Illuminate\\Routing\\Router->runRoute()
#47 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Routing/Router.php(613): Illuminate\\Routing\\Router->dispatchToRoute()
#48 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(170): Illuminate\\Routing\\Router->dispatch()
#49 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(130): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}()
#50 /home/vagrant/luvo.care/vendor/barryvdh/laravel-debugbar/src/Middleware/InjectDebugbar.php(65): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#51 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Barryvdh\\Debugbar\\Middleware\\InjectDebugbar->handle()
#52 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#53 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle()
#54 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#55 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle()
#56 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#57 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle()
#58 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(63): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#59 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle()
#60 /home/vagrant/luvo.care/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#61 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Fideloper\\Proxy\\TrustProxies->handle()
#62 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(105): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#63 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(145): Illuminate\\Pipeline\\Pipeline->then()
#64 /home/vagrant/luvo.care/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(110): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter()
#65 /home/vagrant/luvo.care/public/index.php(55): Illuminate\\Foundation\\Http\\Kernel->handle()
#66 {main}
"} 

Just to note again - everything works great locally if i just reference the json file directly. FIREBASE_CREDENTIALS=storage/tmp-firebase-service.json

I know troubleshooting things like this takes time so thanks in advance for your help!!!

jeromegamez commented 3 years ago

Thanks for sharing the additional info! I haven't been able to test this yet, but I am (sadly) quite confident that it could have something to do with the used Laravel bein on version 6, perhaps the DotEnv component within is not as capable as in newer Laravel versions.

But this is just a guess, I will try to replicate this later tonight 🤞

chuckienorton commented 3 years ago

Thanks so much for your help!!

If it’s a laravel version issue - at least I’ll know my options. On Nov 10, 2020, 14:15 -0500, Jérôme Gamez notifications@github.com, wrote:

Thanks for sharing the additional info! I haven't been able to test this yet, but I am (sadly) quite confident that it could have something to do with the used Laravel bein on version 6, perhaps the DotEnv component within is not as capable as in newer Laravel versions. But this is just a guess, I will try to replicate this later tonight 🤞 — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

jeromegamez commented 3 years ago

Sorry again for the late reply, but I wanted to make sure I test this with enough time. Here's what I did (on a Mac with PHP 7.4.12):

$ composer create-project "laravel/laravel:6.18.35" laravel6
$ cd laravel6
$ composer require "kreait/laravel-firebase:^2.0"
$ jq -c . < /path/to/firebase_service_account.json | pbcopy
# Add the clipboard contents into the .env file
# so that it has FIREBASE_CREDENTIALS='<clipboard_content>'

and, alas, I got the same error 😩:

$ php artisan tinker
Psy Shell v0.10.4 (PHP 7.4.12 — cli) by Justin Hileman
>>> app('firebase.database')
Kreait/Firebase/Exception/InvalidArgumentException with message 'Invalid service account: json_decode error: Control character error, possibly incorrectly encoded'

I assume this has something to do with the required version of vlucas/phpdotenv - Laravel 6 requires a ^3.3 release, Laravel 8 (in which I initially tested this) requires ^5.2.

However, here's something that worked on my machine with Laravel 6, and I hope it works for you as well:

First, if you haven't already done it, publish the package configuration with

$ php artisan vendor:publish --provider="Kreait\Laravel\Firebase\ServiceProvider" --tag=config

Then, in config/firebase.php, change the line

'file' => env('FIREBASE_CREDENTIALS'),

to

'file' => preg_replace('/[\x00-\x1F\x7F]/', '', env('FIREBASE_CREDENTIALS')),

This will remove all control characters from the string (I assume the linebreaks are what killed it) and worked on my machine™

🤞

chuckienorton commented 3 years ago

Thanks for your work on this!! Wow that was helpful.

First, that did not work for me. However I did find another solution below.

I published the config, made your suggested changes, and then recompressed the file. I got a different error - the openssl_sign error - which i had previously gotten when messing with the spaces in the key.

exception: "NotificationChannels\Fcm\Exceptions\CouldNotSendNotification" file: "/home/vagrant/luvo.care/vendor/laravel-notification-channels/fcm/src/Exceptions/CouldNotSendNotification.php" line: 11 message: "openssl_sign(): supplied key param cannot be coerced into a private key"

However, your comment about vlucas/phpdotenv versions made me fine my way to this solution which recommended enclosing the slashes like this:

"-----BEGIN RSA PRIVATE KEY-----\\nMYKEYHERE...\\n-

And it works!! I rewound all the other modifications and just left the config file alone. Here is a recap for anyone else finding this:

  1. jq -c . < ./google-firebase-service.json
  2. add single quotes around that output
  3. add second backslash to all the backslashes in the private key
  4. place directly into the environment variable FIREBASE_CREDENTIALS

Your help on this was great. Maybe this will be useful to someone down the line.

Thanks!

zainaldeen commented 2 years ago

@chuckienorton @jeromegamez thanks guys, that worked with me too!

but i got another problem i hope to help me with it as i am fresher to deal with firebase i did this: ` $auth = app('firebase.auth'); $idTokenString = 'eyJhbGciU......ctZgJXl0wmCw'; // here is my Token i got

    try {
        $verifiedIdToken = $auth->verifyIdToken($idTokenString);

    } catch (InvalidToken $e) {
        echo 'The token is invalid: '.$e->getMessage();
    } catch (\InvalidArgumentException $e) {
        echo 'The token could not be parsed: '.$e->getMessage();
    }

    $uid = $verifiedIdToken->claims()->get('sub');

    // dd($uid); and it shown correctly like what stored on firebase console
    $user = $auth->getUser($uid); // this line return the next error

    dd($user);

`

_opensslsign(): supplied key param cannot be coerced into a private key

thanks!

wildangunawan commented 2 years ago

Leaving comment for future researcher:

  1. add second backslash to all the backslashes in the private key

Somehow it's no longer needed now. I only need to jq it and paste it directly (with single quote of course) to .env file. And it's working :D

NiravViralDevelopment commented 3 months ago

Somehow it's no longer needed now. I only need to jq it and paste it directly (with single quote of course) to .env file. And it's working :D