Authenticated Request
Web PKI can send web requests authenticated with the user's digital certificate through mutual SSL/TLS. This is very useful when communicating with services from Receita Federal (the Brazilian federal revenue service), SEFAZ (the state revenue offices) or city governments.
NFe - Electronic invoices
Services involving electronic invoices (NFe) are another area that requires authenticated requests. When an NFe is transmitted, the national standard allows authentication with a certificate from a company other than the one that issued the NFe. This lets the transmission be done on the backend, for example with an A1 certificate issued to the company that offers the web NFe signing service.
However, other services, such as queries and recipient manifestations (manifestação do destinatário), require authentication with the certificate of the company that issued the NFe. In those cases, authenticated requests with Web PKI let you implement the communication from the user's computer while following good security practices: they allow certificates stored on cryptographic devices (A3) to be used, and they avoid sending A1 certificates to be kept by third parties.
Examples
The example below shows a simple authenticated request that checks the operational status of the SEFAZ-RS service in the staging (homologação) environment.
// Request parameters
var request = {
// Service URL
url: 'https://nfe-homologacao.svrs.rs.gov.br/ws/NfeStatusServico/NfeStatusServico4.asmx',
method: 'post',
headers: {
'Content-Type': 'application/soap+xml; charset=utf-8'
},
// SOAP envelope of the request
body: '<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"><soap12:Body><nfeDadosMsg xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/NFeStatusServico4"><consStatServ xmlns="http://www.portalfiscal.inf.br/nfe" versao="4.00"><tpAmb>2</tpAmb><cUF>53</cUF><xServ>STATUS</xServ></consStatServ></nfeDadosMsg></soap12:Body></soap12:Envelope>'
};
// Sets the user's certificate used to authenticate the request
request.certificateThumbprint = $('#certificateSelect').val();
// Sends the request
pki.sendAuthenticatedRequest(request).success(function (response) {
// Use the response
});
See also
- Working example on JSFiddle: Sending an authenticated request
- Documentation of the
sendAuthenticatedRequest()method (available since API version 1.4)