Created
July 28, 2019 13:26
Exploring Linked / Ordered Structs In Lucee 5.3.2.77
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
signature = generateSignature( | |
"PUT", | |
"https://some-end-point", | |
// NOTE: When passing the end-point parameters into the signer, I am using an | |
// ORDERED STRUCT literal. This way, the order of the parameters, as applied to | |
// the underlying message, will be predictable. | |
[ | |
d: "dee", | |
c: "see", | |
z: "zeta", | |
a: "anna" | |
] | |
); | |
echo( "Message Signature: #signature.lcase()# <br />" ); | |
// ------------------------------------------------------------------------------- // | |
// ------------------------------------------------------------------------------- // | |
/** | |
* I generate a signature for the request to the secure end-point. | |
* | |
* @method I am the HTTP method being used. | |
* @scriptName I am the end-point being accessed. | |
* @parameters I am the collection of ORDERED key-value pairs being passed through. | |
*/ | |
public string function generateSignature( | |
required string method, | |
required string scriptName, | |
required struct parameters | |
) { | |
// CAUTION: Since the parameters are assumed to be an ORDERED / LINKED struct, | |
// the keys can be iterated-over in a predictable, repeatable manner. | |
var pairs = parameters | |
.keyArray() | |
.map( | |
( key ) => { | |
return( "#key.lcase()#=#parameters[ key ]#" ); | |
} | |
) | |
.toList( "&" ) | |
; | |
var message = "#method.ucase()#:#scriptName#?#pairs#"; | |
var secretKey = "tot3sMaGoate$"; | |
// For debugging the demo, log pre-signature message. | |
systemOutput( "Signing: #message#", true ); | |
return( hmac( message, secretKey, "hmacSha256" ) ); | |
} | |
</cfscript> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
// As a control, let's see what happens when we insert keys in a descending | |
// alphabetical order using a struct literal. When output, they _should_ be listed in | |
// ascending order. | |
data = { | |
zomething: "different", | |
hello: "world", | |
foo: "bar", | |
anna: "banana" | |
}; | |
dump( label = "Struct Literal - {}", var = data ); | |
echo( "<br />" ); | |
// ------------------------------------------------------------------------------- // | |
// ------------------------------------------------------------------------------- // | |
// Lucee allows us to define an ORDERED struct literal by mixing Array and Struct | |
// notation. Notice that the enclosing syntax uses brackets; but, that the internal | |
// key-value pairs are using the normal struct syntax. | |
orderedData = [ | |
zomething: "different", | |
hello: "world", | |
foo: "bar", | |
anna: "banana" | |
]; | |
dump( label = "Ordered Struct Literal - []", var = orderedData ); | |
echo( "<br />" ); | |
// ------------------------------------------------------------------------------- // | |
// ------------------------------------------------------------------------------- // | |
// Lucee also offers the option to define a Type of struct when using structNew(). | |
// In this case we are creating a "linked" (ordered) struct that will record the | |
// order in which the keys are subsequently inserted. | |
orderedData2 = structNew( "linked" ); | |
orderedData2.zomething = "different"; | |
orderedData2.hello = "world"; | |
orderedData2.foo = "bar"; | |
orderedData2.anna = "banana"; | |
dump( label = "StructNew( 'linked' )", var = orderedData2 ); | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment