This library can be used in conjunction with the HTTP authentication library. The keyId should be the base58 encoded public key.
For POST and PUT requests, it's recommended to create an HTTP Digest (RFC 3230). This is a hash of the body, which manages to indirectly include the body in the signature. See the HTTP Digest library.
Creating the HTTP Authentication service
useJasny\HttpSignature\HttpSignature;$secretKey ='wJ4WH8dD88fSkNdFQRjaAhjFUZzZhV5yiDLDwNUnp6bYwRXrvWV8MJhQ9HL9uqMDG1n7XpTGZx7PafqaayQV8Rp';$factory =newLTO\AccountFactory('T'); // 'T' for testnet, 'L' for mainnet$ourAccount = $factory->create($secretKey);$service =newHttpSignature( ['ed25519','ed25519-sha256'],newSignCallback($ourAccount),newVerifyCallback($accountFactory));
Server middleware
Create server middleware to verify incoming requests.
The LTO\Account\ServerMiddleware can be used to set the account attribute for a server request that contains a signature_key_id attribute.
useJasny\HttpDigest\HttpDigest;useJasny\HttpDigest\ServerMiddlewareas DigestMiddleware;useJasny\HttpDigest\Negitiation\DigestNegotiator;useJasny\HttpSignature\HttpSignature;useJasny\HttpSignature\ServerMiddlewareas SignatureMiddleware;useLTO\Account\ServerMiddlewareas AccountMiddleware;useRelay\RelayBuilder;$factory =newLTO\AccountFactory('T'); // 'T' for testnet, 'L' for mainnet$ourAccount = $factory->create($secretKey);$digestService =HttpDigest(newDigestNegotiator(), ["SHA-256"]);$signatureService =newHttpSignature( ['ed25519','ed25519-sha256'],function() { thrownew\LogicException('sign not supported'); },newVerifyCallback($accountFactory));$relayBuilder =newRelayBuilder($resolver);$relay = $relayBuilder->newInstance([ (newDigestMiddleware($digestService))->asDoublePass(), (newSignatureMiddleware($signatureService))->asDoublePass(), (newAccountMiddleware($factory))->asDoublePass(),]);
The server middleware implements the PSR-15 MiddlewareInterface for single pass support and returns a callback for double pass with the asDoublePass() method.
Client middleware
Create client middleware to sign outgoing requests.
useGuzzleHttp\HandlerStack;useGuzzleHttp\Client;useJasny\HttpDigest\HttpDigest;useJasny\HttpDigest\ClientMiddlewareas DigestMiddleware;useJasny\HttpDigest\Negitiation\DigestNegotiator;useJasny\HttpSignature\HttpSignature;useJasny\HttpSignature\ClientMiddlewareas SignatureMiddleware;$secretKey ='wJ4WH8dD88fSkNdFQRjaAhjFUZzZhV5yiDLDwNUnp6bYwRXrvWV8MJhQ9HL9uqMDG1n7XpTGZx7PafqaayQV8Rp';$factory =newLTO\AccountFactory('T'); // 'T' for testnet, 'L' for mainnet$ourAccount = $factory->create($secretKey);$digestService =HttpDigest(newDigestNegotiator(), ["SHA-256"]);$signatureService =newHttpSignature( ['ed25519','ed25519-sha256'],newSignCallback($ourAccount),function() { thrownew\LogicException('verify not supported'); });$signatureMiddleware =newSignatureMiddleware( $service->withAlgorithm('ed25519-sha256'), $ourAccount->getPublicKey());$stack =newHandlerStack();$stack->push((newDigestMiddleware($digestService))->forGuzzle());$stack->push($signatureMiddleware->forGuzzle());$client =newClient(['handler'=> $stack]);