AuthToken.php
public static function generate(array $payload, $key)
    {
        // Check if payload is array
        if (!is_array($payload)) {
            throw new InvalidArgumentException('Token payload must be an array');
        }

        // Check if payload or key is empty
        if (empty($payload) || empty($key)) {
            throw new InvalidArgumentException('Both payload and key must have a value');
        }

        // Encode with json and base64
        $payloadEncoded = base64_encode(json_encode($payload, JSON_FORCE_OBJECT));

        // Create signature
        $signature = hash_hmac($algo = 'SHA256', $data = $payloadEncoded, $key);

        // Return combined key
        return implode('.', [$payloadEncoded, $signature]);
    }
public static function generate(array $payload, $key)
    {
        // Check if payload is array
        if (!is_array($payload)) {
            throw new InvalidArgumentException('Token payload must be an array');
        }

        // Check if payload or key is empty
        if (empty($payload) || empty($key)) {
            throw new InvalidArgumentException('Both payload and key must have a value');
        }

        // Encode with json and base64
        $payloadEncoded = base64_encode(json_encode($payload, JSON_FORCE_OBJECT));

        // Create signature
        $signature = hash_hmac($algo = 'SHA256', $data = $payloadEncoded, $key);

        // Return combined key
        return implode('.', [$payloadEncoded, $signature]);
    }
public static function generate ( array $ payload , $ key ) { // Check if payload is array if ( ! is_array ( $ payload ) ) { throw new InvalidArgumentException ( 'Token payload must be an array' ) ; } // Check if payload or key is empty if ( empty ( $ payload ) || empty ( $ key ) ) { throw new InvalidArgumentException ( 'Both payload and key must have a value' ) ; } // Encode with json and base64 $ payloadEncoded = base64_encode ( json_encode ( $ payload , JSON_FORCE_OBJECT ) ) ; // Create signature $ signature = hash_hmac ( $ algo = 'SHA256' , $ data = $ payloadEncoded , $ key ) ; // Return combined key return implode ( '.' , [ $ payloadEncoded , $ signature ] ) ; }
Generate an authentication token.

@param array  $payload The payload in the form of array.
@param string $key     The key to sign the token.

@return string
@throws InvalidArgumentException
Generate an authentication token .