Using PHP to generate JWT signatures for web push notifications

I am currently working on setting up web push notifications using PHP. While I have researched the implementation of the web push protocol, such as reading about it here, I am struggling with understanding how to create the Authorization header as explained by the guide. To attempt this, I utilized this library along with VAPID keys generated from an online generator. My code snippet looks like:

use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\Signer\Ecdsa\Sha256;

$signer = new Sha256();
$privateKey = new Key('<the generated private VAPID key>');
$time = time();

$token = (new Builder())->permittedFor('https://example.com')
                        ->expiresAt($time + 3600)
                        ->withHeader('alg', 'ES256')
                        ->withClaim('sub', 'mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0a7965676f65646f4a6f726b677a666f24696567">[email protected]</a>')
                        ->getToken($signer, $privateKey);

My goal is to obtain something similar to

<JWT Info>.<JWT Data>.<Signature>
from $token. However, I encountered an error.

Fatal error: Uncaught InvalidArgumentException: It was not possible to parse your key, reason: error:0909006C:PEM ...

If anyone has insight into what may be going wrong in my approach, I would greatly appreciate your assistance. Thank you in advance!

Answer №1

The Lcobucci/jwt library internally utilizes both the openssl_pkey_get_private() and openssl_pkey_get_public() functions.

As per the documentation, these functions require PEM-encoded keys, which seems to be missing in your input. The expected format for such keys typically begins with a prefix of -------BEGIN.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

As the value steadily grows, it continues to rise without interruption

Even though I thought it was a simple issue, I am still struggling to solve it. What I need is for the output value to increment continuously when I click the button. Here is the code snippet I have been working on: $('.submit').on('click&a ...

Utilizing Active Callbacks in WordPress Customizer: A Step-By-Step Guide

Within my theme, I have set up 2 options using the Theme Customization API, as shown in the code snippet below. I am trying to achieve a functionality where the radio option is displayed when the checkbox is true, and hidden when the checkbox is false. I ...

Retrieve data from the following fields: {"attending":0,"eventid":1,"userid":1} using PHP

After receiving an array from JAVA and using print_r($_POST), I have tried various methods to extract data but with no success. The format of the array is as follows: {"attending":0,"eventid":1,"userid":1} I've attempted different approaches found o ...

What is the best way to remove all the links with a specific feature?

My task involves shortening the links for a specific service using this PHP function: function Shorten($url){ $api_url="https://server/api?api=key&url=".$url."&format=text"; $res= @file_get_contents($api_url); if($res){ return $res; }} Ad ...

Using ajax and json to create interconnected dropdown menus

I'm currently working on implementing a dependent drop-down field in PHP. Everything seems to be functioning correctly, as it displays the printed array values. However, the JSON returned value does not seem to populate the second drop-down list. Can ...

The parser for PHP CSS struggles with interpreting semicolons within property values

I encountered an issue while using a function to parse CSS files. The problem arises when the property values contain semicolons, causing it to break. Here's an example snippet from the CSS file: #logo { background-image: url("data:image/png;base ...

Achieving user ranks: is it possible without relying on SQL queries?

Currently, I am working on the server-side component of an online game, and one of my tasks involves calculating user rankings. Currently, this task is accomplished using a relational database management system (RDBMS), but it leads to deadlocks when aroun ...

Sending a PHP string formatted as JSON to be processed by jQuery

When attempting to echo a string with a JSON structure, I am encountering issues with the jQuery ajax post not parsing it correctly. My question is whether this string will be recognized as JSON by the jQuery json parse function when echoed in a similar ma ...

information transmitted via an ajax call

I am facing an issue while trying to send my form data through an ajax request. The code for insertion runs, but the data is not getting stored in the database. var form_data = $('#form_company').serialize(); var request = $.ajax({ url: ...

Extract data from a json response to use highcharts

Hey everyone, I could really use your help. I'm currently struggling to parse a json response in a way that highcharts can display it as a column chart. Despite my efforts, I haven't been successful in finding a solution. Here is an example of h ...

Accessing information upon clicking on a tab

Recently, I came across a really interesting tabbed content page that caught my attention. When you click on the tabs, content is displayed. The link to the page is http://codepen.io/unasAquila/pen/nDjgI. What stood out to me was that the tabs were loaded ...

Receiving 'Error: Unable to locate file or directory' while attempting to establish a connection to a MySQL Server on Google App Engine using PHP

Currently facing an issue while attempting to connect to my Google Cloud SQL instance using PHP with PDO. The error message encountered when running the PHP file from within the Google Cloud terminal is as follows: Uncaught PDOException: SQLSTATE[HY000] [2 ...

How can you style an HTML tag using esc_html in CSS?

I've encountered an issue while editing the woocommerce orders.php template. This template is responsible for displaying the user's placed orders. I have identified several variables that require secure coding, such as $date_created or $view_orde ...

Mastering jQuery Ajax: Techniques for Handling Asynchronous Requests with Grace

In my PHP code, I have implemented a jQuery AJAX request that fetches data from the database and exports it into an Excel file. Once the AJAX request is successful, a download box for the Excel file pops up. This functionality works well across all browser ...

Retrieve information from the database upon clicking the submit button

My table is not getting data from the database when I click a button. I tried using this code but it returned an error. <div class="wrapper wrapper-content animated fadeInRight"> <div class="row"> <div class="col ...

Set parameters in the environment.prod.ts configuration file

view image description here Can anyone provide guidance on how to implement the condition in the code snippet above within the environment.ts file? export const environment = { production: true, if(our condiion == "impdev.something.com"){ API_url: ...

Fetching data from an uploaded file and transmitting it to a specified URL

<? if(isset($_POST["submit"])) { $f_name = $_FILES["filetoupload"]["name"]; $f_tmp = $_FILES["filetoupload"]["tmp_name"]; $store = "uploads/".$f_name; if(move_uploaded_file($f_tmp,$store)) echo "file uploaded successfully"; echo"<br>"; ...

Invalid File Format using Spout PHP on CentOS Linux operating system

I am encountering an issue when trying to export data in xlsx format using the Spout library. I am unsure of where the problem lies within my code, as I have limited knowledge of the Spout library. Despite multiple attempts, the same error persists wheneve ...

Utilizing AJAX and PHP POST to dynamically refresh innerHTML content with MySQL data updates

I've been trying to solve this problem for a long time now, and I've reached a point where I can't seem to figure it out. On a page, there are several forms where users input different information that gets submitted to a mySQL database usin ...

Troubleshooting issue: PHP INSERT Prepared statement not working when using AJAX

Currently, I am trying to construct an INSERT statement using AJAX along with a prepared statement in PDO. This is my first attempt at combining AJAX and PDO, so there might be mistakes due to lack of experience. In its current state, I keep encountering ...