params
string pToken : in ;JSON Web Token
(JWT)
string pList : out ;Uniface list of
data
endparams
variables
string vToken,vHeader,vHeaderJson,vHeaderList,vAlgorithm,vTokenMode,vTokenType
string vEncryption,vKeyId,vKeyUrl,vPartTwo,vPartTwoJson,vPartTwoList,vPartThree
endvariables
;check parameters
if ( pToken = "" )
return -101 ;no token
endif
;split token into
3 parts
vToken = $replace(pToken,1,".","·;",-1)
if ( $itemcount(vToken) != 3 )
return -102 ;token doesn't have 3 parts
endif
getitem vHeader,vToken,1
getitem vPartTwo,vToken,2
getitem vPartThree,vToken,3
if ( vHeader = "" | vPartTwo = "" )
return -103 ;token parts are missing (check third part later, depends on mode)
endif
;decode header
vHeaderJson = $replace($replace(vHeader,1,"_","/",-1),1,"-","+",-1)
vHeaderJson = $encode("USTRING",$decode("BASE64",vHeaderJson))
if ( $status < 0 | $procerror < 0 |
vHeaderJson = "" )
return -104 ;header could not be decoded
endif
call json_to_list(vHeaderJson,vHeaderList)
if ( vHeaderList = "" )
return -105 ;header JSON is invalid
endif
;extract header
values
getitem/id
vTokenType,vHeaderList,"typ"
delitem/id vHeaderList,"typ"
getitem/id vAlgorithm,vHeaderList,"alg"
delitem/id vHeaderList,"alg"
getitem/id vEncryption,vHeaderList,"enc"
delitem/id vHeaderList,"enc"
getitem/id vKeyId,vHeaderList,"kid"
delitem/id vHeaderList,"kid"
getitem/id vKeyUrl,vHeaderList,"jku"
delitem/id vHeaderList,"jku"
if ( vHeaderList != "" )
return -106 ;unknown header values
endif
;check signature
algorithm
selectcase ( vAlgorithm )
case "none"
vAlgorithm = "" ;plaintext token
vTokenMode = "JWT"
case "HS256"
vAlgorithm = "HMAC_SHA256" ;HMAC using SHA-256 hash
vTokenMode = "JWS"
case "HS384"
vAlgorithm = "HMAC_SHA384" ;HMAC using SHA-384 hash
vTokenMode = "JWS"
case "HS512"
vAlgorithm = "HMAC_SHA512" ;HMAC using SHA-512 hash
vTokenMode = "JWS"
case "RS256"
vAlgorithm = "RSASSA_PKCS1V15_SHA256" ;RSA SSA (PKCS) using SHA-256 hash
vTokenMode = "JWS"
case "RS384"
vAlgorithm = "RSASSA_PKCS1V15_SHA384" ;RSA SSA (PKCS) using SHA-384 hash
vTokenMode = "JWS"
case "RS512"
vAlgorithm = "RSASSA_PKCS1V15_SHA512" ;RSA SSA (PKCS) using SHA-512 hash
vTokenMode = "JWS"
case "PS256"
vAlgorithm = "RSASSA_PSS_SHA256" ;RSA SSA (PSS) using SHA-256 hash
vTokenMode = "JWS"
case "PS384"
vAlgorithm = "RSASSA_PSS_SHA384" ;RSA SSA (PSS) using SHA-384 hash
vTokenMode = "JWS"
case "PS512"
vAlgorithm = "RSASSA_PSS_SHA512" ;RSA SSA (PSS) using SHA-512 hash
vTokenMode = "JWS"
case "RAS1_5"
vAlgorithm = "RSAES_PKCS1V15" ;RSA ES (PKCS)
vTokenMode = "JWE"
case "RSA-OAEP-256"
vAlgorithm = "RSAES_OAEP_SHA256" ;RSA ES (OAEP) using SHA-256 hash
vTokenMode = "JWE"
case "ES256","ES384","ES512","RSA-OAEP","A128KW","A192KW","A256KW","dir"
return -107 ;valid, but not supported by Uniface
case "","alg"
return -108 ;no algorithm (mandatory value)
elsecase
return -109 ;unknown algorithm specified
endselectcase
;check mode/third
part
selectcase ( vTokenMode )
case "JWT"
if ( vPartThree != "" )
return -110 ;third part specified in plaintext token
endif
case "JWS"
if ( vPartThree = "" )
return -111 ;third part missing in signed token
endif
case "JWE"
if ( vPartThree = "" )
return -111 ;third part missing in encrypted token
endif
return -112 ;todo - handle this type
elsecase
return -112 ;unknown token mode
endselectcase
;decode second
part
vPartTwoJson = $replace($replace(vPartTwo,1,"_","/",-1),1,"-","+",-1)
vPartTwoJson = $encode("USTRING",$decode("BASE64",vPartTwoJson))
if ( $status < 0 | $procerror < 0 |
vPartTwoJson = "" )
return -113 ;second part could not be decoded
endif
call json_to_list(vPartTwoJson,vPartTwoList)
if ( vPartTwoList = "" )
return -114 ;second part JSON is invalid
endif
;signature mode
if ( vTokenMode = "JWS" )
;todo – validate signature
endif
;return data
if ( vTokenType = "JWS" | vTokenType = "JWE" )
call jwt_to_list(vPartTwoJson,pList) ;handle nested tokens
else
pList = vPartTwoList ;no nesting, just return data
endif
return 0
end
Summary: It is possible to handle JSON Web Tokens (JWTs), but so far I've only looked at plaintext and signed tokens, and I've not managed to validate the signature for signed tokens yet.