SQL Server
SQL Server
Load and Validate a JWS from a String
See more JSON Web Signatures (JWS) Examples
Demonstrates Jws.LoadJws, which loads a JWS from its compact serialization string. The example then supplies the MAC key and validates the signature.
Background. Validate returns 1 when the signature or MAC is cryptographically valid, 0 when it is not, or a negative value on error. The key for the signer index must be provided before validating.
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 @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
-- Load the JWS compact serialization.
DECLARE @jwsCompact nvarchar(4000)
SELECT @jwsCompact = 'eyJhbGciOiJIUzI1NiJ9.VGhpcyBpcyB0aGUgY29udGVudCB0byBzaWduLg.<signature>'
EXEC sp_OAMethod @jws, 'LoadJws', @success OUT, @jwsCompact
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @jws, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @jws
RETURN
END
-- Provide the HMAC key for signer 0, then validate the signature. Validate returns 1 when the
-- signature is cryptographically valid, 0 when it is not, or a negative value on error.
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
RETURN
END
DECLARE @v int
EXEC sp_OAMethod @jws, 'Validate', @v OUT, 0
IF @v < 0
BEGIN
EXEC sp_OAGetProperty @jws, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @jws
RETURN
END
IF @v <> 1
BEGIN
PRINT 'The signature is not valid.'
EXEC @hr = sp_OADestroy @jws
RETURN
END
PRINT 'The signature is valid.'
EXEC @hr = sp_OADestroy @jws
END
GO