Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5e99a9a

Browse files
committedFeb 7, 2025
Add FCM web app for testing
1 parent b3b75a7 commit 5e99a9a

File tree

3 files changed

+863
-1
lines changed

3 files changed

+863
-1
lines changed
 

‎examples/Messaging/WebClient/auth.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
error_reporting(E_ERROR | E_PARSE);
4+
5+
function base64url_encode($data){
6+
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
7+
}
8+
9+
function generateToken($email, $privateKey){
10+
11+
if($email=="" || $privateKey=="")
12+
return "";
13+
14+
$base64UrlHeader = base64url_encode(
15+
json_encode(
16+
array(
17+
'alg' => 'RS256',
18+
'typ' => 'JWT'
19+
)
20+
)
21+
);
22+
23+
$now = time();
24+
$str= json_encode(
25+
array(
26+
"iss" => $email,
27+
"sub" => $email,
28+
"aud" => "https://oauth2.googleapis.com/token",
29+
"iat" => $now,
30+
"exp" => $now + 3600,
31+
"scope" =>"https://www.googleapis.com/auth/devstorage.full_control https://www.googleapis.com/auth/datastore https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/firebase.database https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/iam"
32+
)
33+
);
34+
35+
$base64UrlPayload = base64url_encode(str_replace ("\\","",$str));
36+
37+
openssl_sign(
38+
$base64UrlHeader.".".$base64UrlPayload,
39+
$RSA256,
40+
$privateKey,
41+
"sha256WithRSAEncryption"
42+
);
43+
44+
$keyRSA256 = base64url_encode($RSA256);
45+
$jwt = $base64UrlHeader . "." . $base64UrlPayload . "." . $keyRSA256;
46+
47+
return $jwt;
48+
}
49+
50+
function getAccessToken($email,$privateKey)
51+
{
52+
if($email=="" || $privateKey=="")
53+
return "";
54+
55+
$ch = curl_init();
56+
$post = [
57+
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
58+
'assertion' => generateToken($email,$privateKey)
59+
];
60+
61+
curl_setopt($ch, CURLOPT_URL,"https://oauth2.googleapis.com/token");
62+
curl_setopt($ch, CURLOPT_POST, true);
63+
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
64+
65+
// Receive server response ...
66+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
67+
$server_output = curl_exec($ch);
68+
$json = json_decode($server_output, true);
69+
curl_close($ch);
70+
return isset($json['access_token']) ?$json['access_token']:"";
71+
}
72+
73+
$actual_link = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
74+
75+
$parts = parse_url($actual_link);
76+
parse_str($parts['query'], $query);
77+
$email= urldecode($query['email']);
78+
$privateKey= str_replace(" ", "+",urldecode($query['private_key']));
79+
$privateKey= str_replace("BEGIN+PRIVATE+KEY", "BEGIN PRIVATE KEY",$privateKey);
80+
$privateKey= str_replace("END+PRIVATE+KEY", "END PRIVATE KEY",$privateKey);
81+
$privateKey= str_replace("\\n", "\n",$privateKey);
82+
echo getAccessToken($email,$privateKey);
83+
84+
?>
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Please sign in to comment.