src/SimpleJWT/JWT.php
/**
     * Signs and serialises the JWT.
     *
     * @param \SimpleJWT\Keys\KeySet $keys the key set containing the key to sign the
     * JWT
     * @param string $kid the ID of the key to use to sign. If null, this
     * is automatically retrieved
     * @param bool|array $auto_complete an array of headers or claims that
     * should be automatically completed, or false if no auto-completion is
     * to be performed
     * @param string $alg if not null, override the `alg` header
     * @param string $format the JWT serialisation format
     * @return string the signed and serialised JWT
     * @throws \SimpleJWT\Keys\KeyException if there is an error obtaining the key
     * to sign the JWT
     * @throws \SimpleJWT\Crypt\CryptException if there is a cryptographic error
     */
Signs and serialises the JWT.
@param \SimpleJWT\Keys\KeySet $keys the key set containing the key to sign the
JWT
@param string $kid the ID of the key to use to sign.
Signs and serialises the JWT . @param \ SimpleJWT \ Keys \ KeySet $keys the key set containing the key to sign the JWT @param string $kid the ID of the key to use to sign .
public function encode($keys, $kid = null, $auto_complete = ['iat', 'kid'], $alg = null, $format = self::COMPACT_FORMAT) {
        if ($auto_complete === false) $auto_complete = [];
        if ($alg != null) $this->headers['alg'] = $alg;
        if (in_array('iat', $auto_complete) && !isset($this->claims['iat'])) $this->claims['iat'] = time();
        try {
            $signer = AlgorithmFactory::create($this->headers['alg']);
        } catch (\UnexpectedValueException $e) {
            throw new CryptException($e->getMessage(), 0, $e);
        }
        $key = $signer->getSigningKey($keys, $kid);
        if (($key != null) && in_array('kid', $auto_complete)) $this->headers['kid'] = $key->getKeyId();
        $protected = Util::base64url_encode(json_encode($this->headers));
        $payload = Util::base64url_encode(json_encode($this->claims));
        $signing_input = $protected . '.' . $payload;
        $signature = $signer->sign($signing_input, $keys, $kid);
        switch ($format) {
            case self::COMPACT_FORMAT:
                return $signing_input . '.' . $signature;
            case self::JSON_FORMAT:
                return json_encode([
                    'protected' => $protected,
                    'payload' => $payload,
                    'signature' => $signature
                ]);
            default:
                throw new \InvalidArgumentException('Incorrect format');
        }
    }
public function encode ( $ keys , $ kid = null , $ auto_complete = [ 'iat' , 'kid' ] , $ alg = null , $ format = self :: COMPACT_FORMAT ) { if ( $ auto_complete === false ) $ auto_complete = [ ] ; if ( $ alg != null ) $ this -> headers [ 'alg' ] = $ alg ; if ( in_array ( 'iat' , $ auto_complete ) && ! isset ( $ this -> claims [ 'iat' ] ) ) $ this -> claims [ 'iat' ] = time ( ) ; try { $ signer = AlgorithmFactory :: create ( $ this -> headers [ 'alg' ] ) ; } catch ( \ UnexpectedValueException $ e ) { throw new CryptException ( $ e -> getMessage ( ) , 0 , $ e ) ; } $ key = $ signer -> getSigningKey ( $ keys , $ kid ) ; if ( ( $ key != null ) && in_array ( 'kid' , $ auto_complete ) ) $ this -> headers [ 'kid' ] = $ key -> getKeyId ( ) ; $ protected = Util :: base64url_encode ( json_encode ( $ this -> headers ) ) ; $ payload = Util :: base64url_encode ( json_encode ( $ this -> claims ) ) ; $ signing_input = $ protected . '.' . $ payload ; $ signature = $ signer -> sign ( $ signing_input , $ keys , $ kid ) ; switch ( $ format ) { case self :: COMPACT_FORMAT : return $ signing_input . '.' . $ signature ; case self :: JSON_FORMAT : return json_encode ( [ 'protected' => $ protected , 'payload' => $ payload , 'signature' => $ signature ] ) ; default : throw new \ InvalidArgumentException ( 'Incorrect format' ) ; } }
Signs and serialises the JWT.
Signs and serialises the JWT .