Psecio\Jwt\Jwt::encode PHP Метод

encode() публичный Метод

Encode the data, either given or from current object
public encode ( string $claims = null, $addIssued = false ) : string
$claims string Claims string to encode [optional]
Результат string Encoded data, appended by periods
    public function encode($claims = null, $addIssued = false)
    {
        $header = $this->getHeader();
        $claimData = $this->getClaims()->toArray();
        // If we don't have an "issued at" make one
        if (!isset($claimData['iat']) && $addIssued === true) {
            $claimData['iat'] = time();
        }
        ksort($claimData);
        $claims = $claims !== null ? $claims : $this->base64Encode(json_encode($claimData, JSON_UNESCAPED_SLASHES));
        $headerData = $header->toArray();
        ksort($headerData);
        $sections = array($this->base64Encode(json_encode($headerData, JSON_UNESCAPED_SLASHES)), $claims);
        $signWith = implode('.', $sections);
        $signature = $this->sign($signWith, $header->getKey(), $header->getAlgorithm());
        if ($signature !== null) {
            $sections[] = $this->base64Encode($signature);
        }
        $result = implode('.', $sections);
        return $result;
    }

Usage Example

Пример #1
0
 public function authorize($authorizationCode, &$failureMessage = null)
 {
     $body = "code={$authorizationCode}&client_id={$this->clientId}&client_secret={$this->clientSecret}&redirect_uri={$this->redirectUri}&grant_type=authorization_code";
     $response = file_get_contents('https://github.com/login/oauth/access_token', null, stream_context_create(['http' => ['ignore_errors' => true, 'method' => 'POST', 'header' => 'Content-Type: application/x-www-form-urlencoded', 'content' => $body]]));
     parse_str($response, $responseParams);
     if (isset($responseParams['error'])) {
         $failureMessage = $responseParams['error_description'];
         return false;
     }
     $accessToken = $responseParams['access_token'];
     $tokenInfo = file_get_contents('https://api.github.com/user', null, stream_context_create(['http' => ['ignore_errors' => true, 'header' => "User-Agent: hardcoreforking.org\r\nAuthorization: token {$accessToken}"]]));
     $tokenData = json_decode($tokenInfo, true);
     if (!isset($tokenData['login'])) {
         $failureMessage = 'login not found in user lookup';
         return false;
     }
     $jwt = new Jwt\Jwt(new Jwt\Header('hardcoreforking'));
     $jwt->issuer('http://hardcoreforking.org')->audience('http://hardcoreforking.org')->issuedAt(time())->notBefore(time())->expireTime(time() + 3600)->jwtId($tokenData['login'])->type($this->redirectUri);
     return $jwt->encode();
 }
All Usage Examples Of Psecio\Jwt\Jwt::encode