forked from mattiLeBlanc/cfml8-jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.cfm
74 lines (61 loc) · 2.7 KB
/
example.cfm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<!---
Example of using cfml-jwt component to encode and decode in plaintext
Author: Mattijs Spierings
Date: 13/5/2014
--->
<cfscript>
/*
Create our component and preset some of the required JWT fields. ISS and AUD will automatically be added to the payload while decoding.
*/
Variables.JWT = createObject( "component", "cfml-jwt" ).init(
iss = "the issuer party"
, aud = "the audience"
, exp = 3600
);
Variables.secretKey = "example_key";
/*-----------------------------------------------------------------------------------------------------------------
Encoding example
-----------------------------------------------------------------------------------------------------------------*/
// create our plainttext payload
//
Variables.payload = StructNew();
Variables.payload[ "subject" ] = "parcels";
Variables.payload[ "count" ] = 3;
// create our token
//
// JWT.encode( object payload, string secretKey, string algorithm:not required )
Variables.result = JWT.encode( payload, Variables.secretKey );
// let's see it
//
writeOutput( "The generated JWT:" );
writeOutput( "<pre style='white-space: pre-wrap;'>" );
writeOutput( Variables.result );
writeOutput( "</pre>" );
/*-----------------------------------------------------------------------------------------------------------------
Decoding example
-----------------------------------------------------------------------------------------------------------------*/
// if queryparam jwt does not exists, use jwt created in encoding example above
//
if ( StructKeyExists( Url, "jwt" ) )
{
Variables.jwtStr = Url.jwt;
}
else
{
Variables.jwtStr = Variables.result;
}
// JWT.decode( string jwt, string secretKey, bool verifySignature )
Variables.decoded = JWT.decode( Variables.jwtStr , Variables.secretKey, true );
// let's see the package
//
writeOutput( "The decoded payload" );
dump( decoded );
</cfscript>
<!------------------------------------------------------------------------------------------------------------------->
<!---
annoyingly CFML8 doesnt support dump in cfscript
--->
<cffunction name="dump" access="public" returntype="void">
<cfargument name="value" type="any" required="true">
<cfdump var="#Arguments.value#">
</cffunction>