2017-06-28 1 views
1

Ich habe Schwierigkeiten mit einem JWT-Token, stellt sich heraus, weil Coldfusion ein Int in ein Float konvertiert. Ich bin mir nicht sicher, wo das Problem ist oder wie es zu beheben ist.Warum gibt die Coldfusion diese Werte als Fließkomma-Dezimalzahlen aus?

Token Zeitstempel und Ablauf:

<cfset TimeStamp = '#VAL(int(getTickCount()/1000))#' > 
<cfset Exp = '#VAL(int((getTickCount()/1000)+43200))#' > 

    <cfscript> 
     Variables.payload    = StructNew(); 
     Variables.payload[ "nbf" ]  = "#TimeStamp#"; 
     Variables.payload[ "exp" ]  = "#Exp#"; 
     Variables.result     = JWT.encode(payload, Variables.secretKey); 
    </cfscript> 

Der JWT.Encode Aufruf sieht wie folgt aus:

<cffunction name="encode" access="public" returntype="String"> 
    <!--- ****************** Arguments ************************ ---> 
    <cfargument name="payload" type="any" required="true"> 
    <cfargument name="key" type="string" required="true"> 
    <cfargument name="algo" type="string" required="false" default="HS256"> 
    <!--- ****************** /Arguments *********************** ---> 

    <!--- define our variables here ---> 
    <cfset var currentTime = getCurrentUtcTime()> 
    <cfset var header = createObject("java", "java.util.LinkedHashMap").init() /> <!--- StructNew doesnt work because coldfusion 8 orders the keys ---> 
    <cfset var claims = createObject("java", "java.util.LinkedHashMap").init() /> <!--- StructNew doesnt work because coldfusion 8 orders the keys ---> 
    <cfset var segments = ArrayNew(1)> 

    <!--- 
     creation of first segment of our JWT: the header 
    ---> 
    <cfset header[ "typ" ] = "JWT"> 
    <cfset header[ "alg" ] = "HS256"> 
    <!--- add header an json with base64 encoding to segment array ---> 
    <cfset arrayAppend(segments, replace(toBase64(serializeJSON(header)), "=", "", "all"))> 

    <!--- 
     creation of the middle segment: the claims set 
    ---> 
    <cfset claims = Arguments.payload> 
    <!--- 
     escape forward slashes in generated JSON 
    ---> 
    <cfset claimsJson = replace( serializeJSON(claims), "/", "\/", "all")> 

    <!--- add header and json with base64 encoding (with padding REMOVED!) to segment array ---> 
    <cfset arrayAppend(segments, replace(toBase64(claimsJson), "=", "", "all"))> 

    <!--- 
     create the last segment: the signature 
    ---> 
    <cfset signingInput = ArrayToList(segments, ".")> 
    <cfset signature = sign(signingInput, Arguments.key, Arguments.algo)> 

    <!--- 
     add signature as last the element to our string 
    ---> 
    <cfreturn ListAppend(signingInput, signature, ".")> 

</cffunction> 

Run durch einen base64 Decoder, bekomme ich so etwas wie exp ": 1.498696809E9" NBF „: 1.498653609E9

+0

Schritt 1 - Dump Zeitstempel und sehen, ob es eine ganze Zahl oder Fließkomma ist. Wenn es wie wissenschaftliche Notation aussieht, verwenden Sie numberformat(). –

+0

Nun, es hat nicht die Notation bis die serializeJSON, wenn ich vorher dump, es ist numerisch. –

+0

'serializeJSON' ist berüchtigt für das Schlachten von Zahlen. In welcher Version von CF befinden Sie sich? – Alex

Antwort

0

Get Nathan Mische's JSONUtil.cfc Dann ersetzen Sie die Zeile

.

mit

<cfset jsonTool = createobject("component","jsonutil")> 
<cfset claimsJson = replace(jsonTool.serializeJSON(claims,false,true), "/", "\/", "all")> 
Verwandte Themen