AWS STS: Obtain an AWS-Signed JWT for an External Service
See more AWS Security Token Service Examples
Uses the AWS STS Query API version 2011-06-15 and a verified TLS connection to the commercial-partition regional endpoint sts.us-west-2.amazonaws.com. Change the endpoint and signing region together for your deployment. Assumes Chilkat is unlocked.
Keep qa_data/aws/sts.json private and outside web roots/source control. Signed examples require nonempty access_key and secret_key strings; temporary source credentials also require session_token and ISO 8601 expiration. Federation exchanges load their specific fields without AWS keys. The examples do not print credentials, bearer tokens, full responses or diagnostic dumps. Credential-producing examples build a complete in-memory session object with access_key, secret_key, session_token and expiration; integrate that object into your application before it goes out of scope.
Set audience to the intended external service. Requests RS256 with a five-minute lifetime from a regional STS endpoint; outbound federation must be enabled for the account. Optional subject_mode is AWSIdentity or OriginIdentity. OriginIdentity requires the caller's trusted identity propagation context. Optional scope is permitted only for OriginIdentity and this single audience, and requires sts:SetWebIdentityTokenScope in addition to sts:GetWebIdentityToken. Validates scope spacing, characters and duplicate values.
The returned token is an external identity JWT, not an AWS access key/session-token credential set. The recipient must validate its signature using the trusted issuer's JWKS, issuer, audience and lifetime; merely decoding it is insufficient. The request cannot extend the calling session's lifetime.
AWS references: GetWebIdentityToken
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoStringCompare
Handle hoCredentials
Boolean iStringMatch1
Boolean iStringMatch2
Handle hoExpires
Variant vNow
Handle hoNow
String sRegion
Boolean iStringMatch3
Boolean iStringMatch4
Boolean iStringMatch5
String sSubjectMode
Boolean iStringMatch6
Boolean iStringMatch7
Boolean iStringMatch8
Boolean iStringMatch9
Boolean iStringMatch10
Handle hoScopeText
Handle hoAllowedScope
Integer iScopeIndex
Handle hoScopeValues
Handle hoSeenScopes
String sScopeValue
Handle hoEndpoint
Variant vAws
Handle hoAws
Handle hoRest
Variant vConnection
Handle hoConnection
Handle hoXml
String sValue
String sSessionToken
String sExpiration
String sResponseXml
Boolean iStringMatch11
Boolean iStringMatch12
Boolean iStringMatch13
Boolean iStringMatch14
String sTemp1
Integer iTemp1
Boolean bTemp1
Boolean bTemp2
Boolean bTemp3
Move False To iSuccess
Get Create (RefClass(cComChilkatStringBuilder)) To hoStringCompare
If (Not(IsComObjectCreated(hoStringCompare))) Begin
Send CreateComObject of hoStringCompare
End
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Keep this file private and outside public web content/source control.
Get Create (RefClass(cComChilkatJsonObject)) To hoCredentials
If (Not(IsComObjectCreated(hoCredentials))) Begin
Send CreateComObject of hoCredentials
End
Set ComStrict Of hoCredentials To True
Get ComLoadFile Of hoCredentials "qa_data/aws/sts.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not load private STS configuration."
Procedure_Return
End
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch1
Get ComJsonTypeOf Of hoCredentials "access_key" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch1 = True)) Begin
Showln "Missing or invalid access_key."
Procedure_Return
End
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch2
Get ComJsonTypeOf Of hoCredentials "secret_key" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch2 = True)) Begin
Showln "Missing or invalid secret_key."
Procedure_Return
End
Get Create (RefClass(cComCkDateTime)) To hoExpires
If (Not(IsComObjectCreated(hoExpires))) Begin
Send CreateComObject of hoExpires
End
Get Create (RefClass(cComCkDateTime)) To hoNow
If (Not(IsComObjectCreated(hoNow))) Begin
Send CreateComObject of hoNow
End
Move "us-west-2" To sRegion
Get ComHasMember Of hoCredentials "session_token" To bTemp1
If (bTemp1 = True) Begin
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch3
Get ComJsonTypeOf Of hoCredentials "session_token" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch3 = True)) Begin
Showln "Missing or invalid session_token."
Procedure_Return
End
Get ComStringOf Of hoCredentials "expiration" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch4
Get ComJsonTypeOf Of hoCredentials "expiration" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch4 = True)) Begin
Showln "Missing or invalid expiration."
Procedure_Return
End
Get ComStringOf Of hoCredentials "expiration" To sTemp1
Get ComSetFromTimestamp Of hoExpires sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Source expiration is invalid."
Procedure_Return
End
Get ComSetFromCurrentSystemTime Of hoNow To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read current time."
Procedure_Return
End
Get ComDiffSeconds Of hoExpires Get pvComObject of hoNow to vNow
vNow To iTemp1
If (iTemp1 <= 60) Begin
Showln "Source credentials are expired or expire within 60 seconds; obtain fresh credentials."
Procedure_Return
End
End
Get ComStringOf Of hoCredentials "audience" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch5
Get ComJsonTypeOf Of hoCredentials "audience" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch5 = True)) Begin
Showln "Missing or invalid audience."
Procedure_Return
End
// Use AWSIdentity by default. OriginIdentity requires trusted propagation context.
Move "AWSIdentity" To sSubjectMode
Get ComHasMember Of hoCredentials "subject_mode" To bTemp1
If (bTemp1 = True) Begin
Get ComStringOf Of hoCredentials "subject_mode" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch6
Get ComJsonTypeOf Of hoCredentials "subject_mode" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch6 = True)) Begin
Showln "Missing or invalid subject_mode."
Procedure_Return
End
Get ComStringOf Of hoCredentials "subject_mode" To sSubjectMode
End
Get ComSetString Of hoStringCompare sSubjectMode To iSuccess
Get ComContentsEqual Of hoStringCompare "AWSIdentity" True To iStringMatch7
Get ComSetString Of hoStringCompare sSubjectMode To iSuccess
Get ComContentsEqual Of hoStringCompare "OriginIdentity" True To iStringMatch8
If ((iStringMatch7 <> True) And (iStringMatch8 <> True)) Begin
Showln "Invalid subject_mode."
Procedure_Return
End
Get ComHasMember Of hoCredentials "scope" To bTemp1
If (bTemp1 = True) Begin
Get ComStringOf Of hoCredentials "scope" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch9
Get ComJsonTypeOf Of hoCredentials "scope" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch9 = True)) Begin
Showln "Missing or invalid scope."
Procedure_Return
End
Get ComSetString Of hoStringCompare sSubjectMode To iSuccess
Get ComContentsEqual Of hoStringCompare "OriginIdentity" True To iStringMatch10
If (iStringMatch10 <> True) Begin
Showln "scope requires OriginIdentity."
Procedure_Return
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoScopeText
If (Not(IsComObjectCreated(hoScopeText))) Begin
Send CreateComObject of hoScopeText
End
Get ComStringOf Of hoCredentials "scope" To sTemp1
Get ComAppend Of hoScopeText sTemp1 To iSuccess
Get ComLength Of hoScopeText To iTemp1
Get ComStartsWith Of hoScopeText " " True To bTemp1
Get ComEndsWith Of hoScopeText " " True To bTemp2
Get ComContains Of hoScopeText " " True To bTemp3
If ((iTemp1 > 256) Or (bTemp1 = True) Or (bTemp2 = True) Or (bTemp3 = True)) Begin
Showln "Invalid scope length or spacing."
Procedure_Return
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoAllowedScope
If (Not(IsComObjectCreated(hoAllowedScope))) Begin
Send CreateComObject of hoAllowedScope
End
Get ComAppend Of hoAllowedScope "!#$%&'()*+-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~ " To iSuccess
Move 0 To iScopeIndex
While (iScopeIndex < (ComLength(hoScopeText)))
Get ComContains Of hoAllowedScope (ComGetRange(hoScopeText, iScopeIndex, 1, False)) True To bTemp1
If (bTemp1 <> True) Begin
Showln "Invalid scope character."
Procedure_Return
End
Move (iScopeIndex + 1) To iScopeIndex
Loop
Get Create (RefClass(cComChilkatStringTable)) To hoScopeValues
If (Not(IsComObjectCreated(hoScopeValues))) Begin
Send CreateComObject of hoScopeValues
End
Get ComGetAsString Of hoScopeText To sTemp1
Get ComSplitAndAppend Of hoScopeValues sTemp1 " " False False To iSuccess
If (iSuccess <> True) Begin
Showln "Could not split scope values."
Procedure_Return
End
Get Create (RefClass(cComChilkatHashtable)) To hoSeenScopes
If (Not(IsComObjectCreated(hoSeenScopes))) Begin
Send CreateComObject of hoSeenScopes
End
Move 0 To iScopeIndex
While (iScopeIndex < (ComCount(hoScopeValues)))
Get ComStringAt Of hoScopeValues iScopeIndex To sScopeValue
Get ComContains Of hoSeenScopes sScopeValue To bTemp1
If (bTemp1 = True) Begin
Showln "Duplicate scope value."
Procedure_Return
End
Get ComAddStr Of hoSeenScopes sScopeValue "seen" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not track scope values."
Procedure_Return
End
Move (iScopeIndex + 1) To iScopeIndex
Loop
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoEndpoint
If (Not(IsComObjectCreated(hoEndpoint))) Begin
Send CreateComObject of hoEndpoint
End
Get ComAppend Of hoEndpoint "sts." To iSuccess
Get ComAppend Of hoEndpoint sRegion To iSuccess
Get ComAppend Of hoEndpoint ".amazonaws.com" To iSuccess
Get Create (RefClass(cComChilkatAuthAws)) To hoAws
If (Not(IsComObjectCreated(hoAws))) Begin
Send CreateComObject of hoAws
End
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Set ComAccessKey Of hoAws To sTemp1
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Set ComSecretKey Of hoAws To sTemp1
Set ComRegion Of hoAws To sRegion
Set ComServiceName Of hoAws To "sts"
Set ComSignatureVersion Of hoAws To 4
Get Create (RefClass(cComChilkatRest)) To hoRest
If (Not(IsComObjectCreated(hoRest))) Begin
Send CreateComObject of hoRest
End
Set ComConnectTimeoutMs Of hoRest To 20000
Set ComIdleTimeoutMs Of hoRest To 60000
Set ComVerboseLogging Of hoRest To False
Get pvComObject of hoAws to vAws
Get ComSetAuthAws Of hoRest vAws To iSuccess
If (iSuccess <> True) Begin
Showln "Could not configure request signing."
Procedure_Return
End
// Keep this verified Socket alive while Rest uses its connection.
Get Create (RefClass(cComChilkatSocket)) To hoConnection
If (Not(IsComObjectCreated(hoConnection))) Begin
Send CreateComObject of hoConnection
End
Set ComRequireSslCertVerify Of hoConnection To True
Set ComMaxReadIdleMs Of hoConnection To 60000
Set ComMaxSendIdleMs Of hoConnection To 60000
Get ComGetAsString Of hoEndpoint To sTemp1
Get ComConnect Of hoConnection sTemp1 443 True 20000 To iSuccess
If (iSuccess <> True) Begin
Showln "TLS connection failed; inspect diagnostics privately."
Procedure_Return
End
Get pvComObject of hoConnection to vConnection
Get ComUseConnection Of hoRest vConnection True To iSuccess
If (iSuccess <> True) Begin
Showln "Could not attach TLS connection."
Procedure_Return
End
Get Create (RefClass(cComChilkatXml)) To hoXml
If (Not(IsComObjectCreated(hoXml))) Begin
Send CreateComObject of hoXml
End
Set ComStrict Of hoXml To True
Move "" To sResponseXml
Get ComClearAllQueryParams Of hoRest To iSuccess
If (iSuccess <> True) Begin
Showln "Could not reset request parameters."
Procedure_Return
End
Get ComAddQueryParam Of hoRest "Action" "GetWebIdentityToken" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set Action."
Procedure_Return
End
Get ComAddQueryParam Of hoRest "Version" "2011-06-15" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set Version."
Procedure_Return
End
Get ComHasMember Of hoCredentials "session_token" To bTemp1
If (bTemp1 = True) Begin
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Get ComAddQueryParam Of hoRest "X-Amz-Security-Token" sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set X-Amz-Security-Token."
Procedure_Return
End
End
Get ComStringOf Of hoCredentials "audience" To sTemp1
Get ComAddQueryParam Of hoRest "Audience.member.1" sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set Audience.member.1."
Procedure_Return
End
Get ComAddQueryParam Of hoRest "SigningAlgorithm" "RS256" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set SigningAlgorithm."
Procedure_Return
End
Get ComAddQueryParam Of hoRest "DurationSeconds" "300" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set DurationSeconds."
Procedure_Return
End
Get ComAddQueryParam Of hoRest "SubjectMode" sSubjectMode To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set SubjectMode."
Procedure_Return
End
Get ComHasMember Of hoCredentials "scope" To bTemp1
If (bTemp1 = True) Begin
Get ComStringOf Of hoCredentials "scope" To sTemp1
Get ComAddQueryParam Of hoRest "Scope" sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set Scope."
Procedure_Return
End
End
Get ComFullRequestFormUrlEncoded Of hoRest "POST" "/" To sResponseXml
Get ComLastMethodSuccess Of hoRest To bTemp1
If (bTemp1 <> True) Begin
Showln "STS transport failed; inspect diagnostics privately."
Procedure_Return
End
Get ComResponseStatusCode Of hoRest To iTemp1
If (iTemp1 <> 200) Begin
Get ComResponseStatusCode Of hoRest To iTemp1
Showln "STS HTTP status: " iTemp1
Get ComLoadXml Of hoXml sResponseXml To iSuccess
If (iSuccess = True) Begin
Get ComGetChildContent Of hoXml "Error|Code" To sValue
Get ComLastMethodSuccess Of hoXml To bTemp1
If (bTemp1 = True) Begin
Showln "AWS error code: " sValue
End
Get ComGetChildContent Of hoXml "RequestId" To sValue
Get ComLastMethodSuccess Of hoXml To bTemp1
If (bTemp1 = True) Begin
Showln "AWS request ID: " sValue
End
End
Procedure_Return
End
Get ComLoadXml Of hoXml sResponseXml To iSuccess
If (iSuccess <> True) Begin
Showln "Invalid STS XML response."
Procedure_Return
End
Get ComTag Of hoXml To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "GetWebIdentityTokenResponse" True To iStringMatch11
If (iStringMatch11 <> True) Begin
Showln "Unexpected STS response root."
Procedure_Return
End
Get ComGetChildContent Of hoXml "ResponseMetadata|RequestId" To sValue
Get ComSetString Of hoStringCompare sValue To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch12
Get ComLastMethodSuccess Of hoXml To bTemp1
If ((bTemp1 <> True) Or (iStringMatch12 = True)) Begin
Showln "Missing or empty ResponseMetadata|RequestId."
Procedure_Return
End
Get ComHasChildWithTag Of hoXml "GetWebIdentityTokenResult" To bTemp1
If (bTemp1 <> True) Begin
Showln "Missing STS result."
Procedure_Return
End
Get ComGetChildContent Of hoXml "GetWebIdentityTokenResult|WebIdentityToken" To sSessionToken
Get ComSetString Of hoStringCompare sSessionToken To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch13
Get ComLastMethodSuccess Of hoXml To bTemp1
If ((bTemp1 <> True) Or (iStringMatch13 = True)) Begin
Showln "Missing or empty GetWebIdentityTokenResult|WebIdentityToken."
Procedure_Return
End
Get ComGetChildContent Of hoXml "GetWebIdentityTokenResult|Expiration" To sExpiration
Get ComSetString Of hoStringCompare sExpiration To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch14
Get ComLastMethodSuccess Of hoXml To bTemp1
If ((bTemp1 <> True) Or (iStringMatch14 = True)) Begin
Showln "Missing or empty GetWebIdentityTokenResult|Expiration."
Procedure_Return
End
Get ComSetFromTimestamp Of hoExpires sExpiration To iSuccess
If (iSuccess <> True) Begin
Showln "JWT expiration is invalid."
Procedure_Return
End
Get ComSetFromCurrentSystemTime Of hoNow To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read current time."
Procedure_Return
End
Get ComDiffSeconds Of hoExpires Get pvComObject of hoNow to vNow
vNow To iTemp1
If (iTemp1 <= 60) Begin
Showln "JWT credentials are expired or expire within 60 seconds; obtain fresh credentials."
Procedure_Return
End
// Hand sessionToken to the intended external service over TLS; never log it.
Showln "Web identity token obtained; expiration: " sExpiration
Showln "Example completed."
End_Procedure