SQL Server
SQL Server
Set the Optional JWS Unprotected Header
See more JSON Web Signatures (JWS) Examples
Demonstrates Jws.SetUnprotectedHeader, which sets the optional unprotected header for a signature.
Background. Unlike the protected header, the unprotected header is not covered by the signature. It is carried in the JSON serialization forms of a JWS, so producing a JWS with an unprotected header yields a JSON serialization rather than compact form.
Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
DECLARE @jws int
EXEC @hr = sp_OACreate 'Chilkat.Jws', @jws OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Protected header specifying HMAC-SHA256.
DECLARE @header int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @header OUT
EXEC sp_OAMethod @header, 'UpdateString', @success OUT, 'alg', 'HS256'
EXEC sp_OAMethod @jws, 'SetProtectedHeader', @success OUT, 0, @header
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @jws, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @jws
EXEC @hr = sp_OADestroy @header
RETURN
END
-- Set the optional unprotected header for signature 0. Unlike the protected header, it is not
-- covered by the signature. It is carried in the JSON serialization forms of a JWS.
DECLARE @unprotected int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @unprotected OUT
EXEC sp_OAMethod @unprotected, 'UpdateString', @success OUT, 'kid', 'signer-key-1'
EXEC sp_OAMethod @jws, 'SetUnprotectedHeader', @success OUT, 0, @unprotected
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @jws, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @jws
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @unprotected
RETURN
END
DECLARE @bIncludeBom int
SELECT @bIncludeBom = 0
EXEC sp_OAMethod @jws, 'SetPayload', @success OUT, 'This is the content to sign.', 'utf-8', @bIncludeBom
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @jws, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @jws
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @unprotected
RETURN
END
DECLARE @base64Key nvarchar(4000)
SELECT @base64Key = 'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU='
EXEC sp_OAMethod @jws, 'SetMacKey', @success OUT, 0, @base64Key, 'base64'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @jws, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @jws
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @unprotected
RETURN
END
-- Because an unprotected header is present, the JWS is produced in a JSON serialization form.
DECLARE @jwsJson nvarchar(4000)
EXEC sp_OAMethod @jws, 'CreateJws', @jwsJson OUT
EXEC sp_OAGetProperty @jws, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @jws, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @jws
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @unprotected
RETURN
END
PRINT @jwsJson
EXEC @hr = sp_OADestroy @jws
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @unprotected
END
GO