SQL Server
SQL Server
Retrieve the Okta JSON Web Keys (JWK)
See more Okta OAuth/OIDC Examples
Demonstrates how to get the Okta web keys (JWK) that will be needed to locally validate an access token. This example is for the case where Okta is the identity platform for your App or API. (See https://developer.okta.com/docs/reference/api/oidc/#composing-your-base-url)Note: This example uses the URL
https://dev-765951.okta.com/oauth2/${authServerId}/v1/authorize
If you have a developer account, you can use the default authorization server that was created along with your account, in which case the full URL looks like this:
https://dev-765951.okta.com/oauth2/default/v1/authorize
Also: You'll need to change "dev-765951" to use your own Okta domain.
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
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Implements the following CURL command:
-- curl -X GET \
-- https://dev-765951.okta.com/oauth2/${authServerId}/v1/keys?client_id=$my_client_id
DECLARE @sbResponseBody int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT
EXEC sp_OAMethod @http, 'QuickGetSb', @success OUT, 'https://dev-765951.okta.com/oauth2/${authServerId}/v1/keys?client_id=$my_client_id', @sbResponseBody
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @sbResponseBody
RETURN
END
DECLARE @jResp int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT
EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
EXEC sp_OASetProperty @jResp, 'EmitCompact', 0
PRINT 'Response Body:'
EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
PRINT @sTmp0
DECLARE @respStatusCode int
EXEC sp_OAGetProperty @http, 'LastStatus', @respStatusCode OUT
PRINT 'Response Status Code = ' + @respStatusCode
IF @respStatusCode >= 400
BEGIN
PRINT 'Response Header:'
EXEC sp_OAGetProperty @http, 'LastHeader', @sTmp0 OUT
PRINT @sTmp0
PRINT 'Failed.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @jResp
RETURN
END
-- Sample JSON response:
-- (Sample code for parsing the JSON response is shown below)
-- {
-- "keys": [
-- {
-- "kty": "RSA",
-- "alg": "RS256",
-- "kid": "anSaRDPfWGOSCVNZEIZB9quCbNsdsvl5uWGBzxbudWQ",
-- "use": "sig",
-- "e": "AQAB",
-- "n": "jT8uAgd5wF7d3aiCjdxbVXk7EDio2ScqAjWCL2LXbY3bpj6RV4dswZTqBg0-vuIh1SBqjoufDV3fHAJcewv2dfEdfUKawBxTIcLgeSu_NoUsZ6zmHqYKC9U0awgP3s5_Yqcr7YMxurrPRNRdKNsFFF2lhCDJ-4gCO4CWiCUdptlmMfI7_pBxx9bgh1F-ulVKt_RUo-4A7tXwwJhBhWaECKoye04BACccSNkth8flGSvjXNIVKcAMHYQ3oDfB6U5zXcFuDVEBJI__hzA9920QRXrloIv_G0QmYgbaf4iRpd-3B6-PQ4-VK-FGm3t9eTwoFv6CARpyXrjaOeuLB1HaVw"
-- }
-- ]
-- }
-- Sample code for parsing the JSON response...
-- Use the following online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON
DECLARE @kty nvarchar(4000)
DECLARE @alg nvarchar(4000)
DECLARE @kid nvarchar(4000)
DECLARE @use nvarchar(4000)
DECLARE @e nvarchar(4000)
DECLARE @n nvarchar(4000)
DECLARE @i int
SELECT @i = 0
DECLARE @count_i int
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'keys'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jResp, 'I', @i
EXEC sp_OAMethod @jResp, 'StringOf', @kty OUT, 'keys[i].kty'
EXEC sp_OAMethod @jResp, 'StringOf', @alg OUT, 'keys[i].alg'
EXEC sp_OAMethod @jResp, 'StringOf', @kid OUT, 'keys[i].kid'
EXEC sp_OAMethod @jResp, 'StringOf', @use OUT, 'keys[i].use'
EXEC sp_OAMethod @jResp, 'StringOf', @e OUT, 'keys[i].e'
EXEC sp_OAMethod @jResp, 'StringOf', @n OUT, 'keys[i].n'
SELECT @i = @i + 1
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @jResp
END
GO