当前位置: 首页>>代码示例>>PHP>>正文


PHP JWT::urlsafeB64Encode方法代码示例

本文整理汇总了PHP中Firebase\JWT\JWT::urlsafeB64Encode方法的典型用法代码示例。如果您正苦于以下问题:PHP JWT::urlsafeB64Encode方法的具体用法?PHP JWT::urlsafeB64Encode怎么用?PHP JWT::urlsafeB64Encode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Firebase\JWT\JWT的用法示例。


在下文中一共展示了JWT::urlsafeB64Encode方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: encode

 /**
  * Converts and signs a PHP object or array into a JWT string.
  *
  * @param object|array  $payload    PHP object or array
  * @param string        $key        The secret key.
  *                                  If the algorithm used is asymmetric, this is the private key
  * @param string        $alg        The signing algorithm.
  *                                  Supported algorithms are 'HS256', 'HS384', 'HS512' and 'RS256'
  * @param array         $head       An array with header elements to attach
  *
  * @return string A signed JWT
  *
  * @uses jsonEncode
  * @uses urlsafeB64Encode
  */
 public static function encode($payload, $key, $alg = 'HS256', $keyId = null, $head = null)
 {
     $header = array('typ' => 'JWT', 'alg' => $alg);
     if ($keyId !== null) {
         $header['kid'] = $keyId;
     }
     if (isset($head) && is_array($head)) {
         $header = array_merge($head, $header);
     }
     $segments = array();
     $segments[] = JWT::urlsafeB64Encode(JWT::jsonEncode($header));
     $segments[] = JWT::urlsafeB64Encode(JWT::jsonEncode($payload));
     $signing_input = implode('.', $segments);
     $signature = JWT::sign($signing_input, $key, $alg);
     $segments[] = JWT::urlsafeB64Encode($signature);
     return implode('.', $segments);
 }
开发者ID:ivyhappy,项目名称:PHYMAN,代码行数:32,代码来源:JWT.php

示例2: DateTime

    if (!isset($server['PHP_AUTH_USER'], $server['PHP_AUTH_PW'])) {
        return $this->api->json($res, ['error' => 'BadArguments', 'message' => 'You need to give a username and a password'], 400);
    }
    $username = $server['PHP_AUTH_USER'];
    $password = $server['PHP_AUTH_PW'];
    if (!($user = $users->read($username))) {
        return $this->api->json($res, ['error' => 'BadCredentials', 'message' => 'User or password not ok'], 403);
    }
    if (!$user->login($password)) {
        return $this->api->json($res, ['error' => 'BadCredentials', 'message' => 'User or password not ok'], 403);
    }
    $now = new DateTime();
    $future = new DateTime('now +2 hours');
    $server = $req->getServerParams();
    try {
        $payload = ['iat' => $now->getTimeStamp(), 'exp' => $future->getTimeStamp(), 'jti' => JWT::urlsafeB64Encode(random_bytes(32)), 'sub' => $username, 'scope' => $user->getRoles()];
    } catch (Exception $e) {
        die('Could not generate a random string. Is our OS secure?');
    }
    $secret = $app['config']->get('secretToken');
    $token = JWT::encode($payload, $secret, 'HS256');
    $data['status'] = 'ok';
    $data['token'] = $token;
    return $res->withStatus(201)->withHeader('Content-Type', 'application/json')->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
});
$this->get('/credentials', function ($req, $res) {
    if (!isset($this->token)) {
        return $res->withStatus(401)->withJson([]);
    }
    $res->withJson($this->token->scope);
});
开发者ID:bafs,项目名称:parvula,代码行数:31,代码来源:index.php


注:本文中的Firebase\JWT\JWT::urlsafeB64Encode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。