PHP Firebase帮助 - 设置JWT [英] PHP Firebase help - Set up JWT

查看:2538
本文介绍了PHP Firebase帮助 - 设置JWT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的服务器上,我正在运行几个读取我的Firebase实时数据库的PHP文件。根据 Firebase的文档,我需要设置自定义令牌来运行我的 Firebase PHP客户端。 Firebase文档说我需要返回这个;

  return JWT :: encode($ payload,$ private_key,RS256) ; 

我如何引用JWT类?我下载了一个智威汤逊图书馆,但我不知道如何实现这个到我的项目。任何帮助将是伟大的,我主要是一个移动开发人员,并没有什么经验与PHP。

解决方案

firebase / php-jwt库使用作曲家。如果你来自android开发背景,Composer是PHP的依赖管理器,与Java中的Maven类似。你将需要知道如何使用php的require / include函数在php中导入类。您需要一些使用PHP的经验来使用作曲家。



为了使用没有作曲者的firebase / php-jwt库,你可以使用下面的示例代码: jwt文件夹中的库)

$ pre $ $ $
$ b require_once'jwt / src / BeforeValidException .PHP';
require_once'jwt / src / ExpiredException.php';
require_once'jwt / src / SignatureInvalidException.php';
require_once'jwt / src / JWT.php';


使用\Firebase\JWT\JWT;

$ key =example_key;
$ token = array(
iss=>http://example.org,
aud=>http://example.com,
iat=> 1356999524,
nbf=> 1357000000
);

/ **
*重要:
*您必须为应用程序指定支持的算法。请参阅
* https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
*获取符合规范的算法列表。
* /
$ jwt = JWT :: encode($ token,$ key);
$ encoded = JWT :: decode($ jwt,$ key,array('HS256'));

print_r($ decode);

/ *
注意:这将成为一个对象而不是关联数组。要得到
的关联数组,你需要像下面这样强制转换:
* /

$ decoded_array =(array)$ decode;
$ b $ / **
*当签名和验证服务器之间的时钟偏差时间在
*之间时,您可以添加一个余地来考虑。建议这个回扣应该是
*不能大于几分钟。
*
*资料来源:http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html#nbfDef
* /
智威汤逊:: $ leeway = 60; $ $
$ encoded = JWT :: decode($ jwt,$ key,array('HS256'));
?>


On my server I am running a few PHP files that read my Firebase Realtime Database. According to Firebase's documents I need to set up custom token to get my Firebase PHP Client running. The Firebase document says I need to return this;

  return JWT::encode($payload, $private_key, "RS256");

How exactly do I reference the JWT class? I downloaded a JWT library but I am not sure how to implement this into my project. Any help would be great, I am mainly a mobile developer and have little experience with PHP.

解决方案

firebase/php-jwt library uses composer. Composer is a dependency manager for PHP similar to Maven in java if you come from android development background. You would need to know how to import classes in php using require/include functions of php. You would need some experience with php to use composer.

In order to use firebase/php-jwt library without composer you could use the following sample code: (I downloaded the library inside 'jwt' folder)

<?php

require_once 'jwt/src/BeforeValidException.php';
require_once 'jwt/src/ExpiredException.php';
require_once 'jwt/src/SignatureInvalidException.php';
require_once 'jwt/src/JWT.php';


use \Firebase\JWT\JWT;

$key = "example_key";
$token = array(
   "iss" => "http://example.org",
   "aud" => "http://example.com",
   "iat" => 1356999524,
   "nbf" => 1357000000
);

/**
 * IMPORTANT:
 * You must specify supported algorithms for your application. See
 * https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
 * for a list of spec-compliant algorithms.
*/
$jwt = JWT::encode($token, $key);
$decoded = JWT::decode($jwt, $key, array('HS256'));

print_r($decoded);

/*
 NOTE: This will now be an object instead of an associative array. To get
 an associative array, you will need to cast it as such:
*/

$decoded_array = (array) $decoded;

/**
* You can add a leeway to account for when there is a clock skew times   between
* the signing and verifying servers. It is recommended that this leeway should
* not be bigger than a few minutes.
*
* Source: http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html#nbfDef
*/
   JWT::$leeway = 60; // $leeway in seconds
   $decoded = JWT::decode($jwt, $key, array('HS256'));
 ?>

这篇关于PHP Firebase帮助 - 设置JWT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆