|
| 1 | +# JWT Parser Function |
| 2 | + |
| 3 | +The JWT Parser function allows SonataFlow workflows to parse and extract information from JWT (JSON Web Token) tokens. This is particularly useful for accessing user information and claims from authentication tokens passed in workflow headers. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This function provides three main operations: |
| 8 | +- **parse**: Extract the complete JWT payload as a JSON object |
| 9 | +- **extractUser**: Extract standard user information from JWT claims (sub, preferred_username, email, etc.) |
| 10 | +- **extractClaim**: Extract a specific claim by name |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +### Basic JWT Parsing |
| 15 | + |
| 16 | +```json |
| 17 | +{ |
| 18 | + "functions": [ |
| 19 | + { |
| 20 | + "name": "parseJWT", |
| 21 | + "type": "custom", |
| 22 | + "operation": "jwt-parser" |
| 23 | + } |
| 24 | + ], |
| 25 | + "states": [ |
| 26 | + { |
| 27 | + "name": "parseToken", |
| 28 | + "type": "operation", |
| 29 | + "actions": [ |
| 30 | + { |
| 31 | + "name": "parseAction", |
| 32 | + "functionRef": { |
| 33 | + "refName": "parseJWT", |
| 34 | + "arguments": { |
| 35 | + "token": "${ $WORKFLOW.headers.\"Authorization\" }", |
| 36 | + "operation": "parse" |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + ] |
| 41 | + } |
| 42 | + ] |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +### Extract User Information |
| 47 | + |
| 48 | +```json |
| 49 | +{ |
| 50 | + "functions": [ |
| 51 | + { |
| 52 | + "name": "extractUser", |
| 53 | + "type": "custom", |
| 54 | + "operation": "jwt-parser:extractUser" |
| 55 | + } |
| 56 | + ], |
| 57 | + "states": [ |
| 58 | + { |
| 59 | + "name": "extractUserName", |
| 60 | + "type": "operation", |
| 61 | + "actions": [ |
| 62 | + { |
| 63 | + "name": "extractUserAction", |
| 64 | + "functionRef": { |
| 65 | + "refName": "extractUser", |
| 66 | + "arguments": { |
| 67 | + "token": "${ $WORKFLOW.headers.\"X-Authorization-acme_financial_auth\" }" |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + ], |
| 72 | + "stateDataFilter": { |
| 73 | + "output": "${ { user: .result.preferred_username } }" |
| 74 | + } |
| 75 | + } |
| 76 | + ] |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +### Extract Specific Claim |
| 81 | + |
| 82 | +```json |
| 83 | +{ |
| 84 | + "functions": [ |
| 85 | + { |
| 86 | + "name": "extractClaim", |
| 87 | + "type": "custom", |
| 88 | + "operation": "jwt-parser:extractClaim" |
| 89 | + } |
| 90 | + ], |
| 91 | + "states": [ |
| 92 | + { |
| 93 | + "name": "extractRole", |
| 94 | + "type": "operation", |
| 95 | + "actions": [ |
| 96 | + { |
| 97 | + "name": "extractRoleAction", |
| 98 | + "functionRef": { |
| 99 | + "refName": "extractClaim", |
| 100 | + "arguments": { |
| 101 | + "token": "${ $WORKFLOW.headers.\"Authorization\" }", |
| 102 | + "claim": "role" |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + ] |
| 107 | + } |
| 108 | + ] |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +## Complete Example |
| 113 | + |
| 114 | +Here's a complete workflow that demonstrates JWT parsing for user personalization: |
| 115 | + |
| 116 | +```json |
| 117 | +{ |
| 118 | + "id": "jwt_example", |
| 119 | + "version": "1.0", |
| 120 | + "name": "JWT Token Processing Example", |
| 121 | + "start": "extractUser", |
| 122 | + "functions": [ |
| 123 | + { |
| 124 | + "name": "extractUser", |
| 125 | + "type": "custom", |
| 126 | + "operation": "jwt-parser:extractUser" |
| 127 | + } |
| 128 | + ], |
| 129 | + "states": [ |
| 130 | + { |
| 131 | + "name": "extractUser", |
| 132 | + "type": "operation", |
| 133 | + "actions": [ |
| 134 | + { |
| 135 | + "name": "extractUserAction", |
| 136 | + "functionRef": { |
| 137 | + "refName": "extractUser", |
| 138 | + "arguments": { |
| 139 | + "token": "${ $WORKFLOW.headers.\"X-Authorization-acme_financial_auth\" }" |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + ], |
| 144 | + "stateDataFilter": { |
| 145 | + "output": "${ { user: .result.preferred_username } }" |
| 146 | + }, |
| 147 | + "transition": "personalizedResponse" |
| 148 | + }, |
| 149 | + { |
| 150 | + "name": "personalizedResponse", |
| 151 | + "type": "inject", |
| 152 | + "data": { |
| 153 | + "approved": true |
| 154 | + }, |
| 155 | + "stateDataFilter": { |
| 156 | + "output": "${ { message: \"Congrats \\(.user)! Your request has been approved!\", approved } }" |
| 157 | + }, |
| 158 | + "end": true |
| 159 | + } |
| 160 | + ] |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +## Parameters |
| 165 | + |
| 166 | +| Parameter | Type | Required | Description | |
| 167 | +|-----------|------|----------|-------------| |
| 168 | +| `token` | string | Yes | The JWT token to parse (Bearer prefix will be automatically removed) | |
| 169 | +| `operation` | string | No | Operation to perform: "parse", "extractUser", or "extractClaim" (default: "parse") | |
| 170 | +| `claim` | string | No | Name of specific claim to extract (required when operation is "extractClaim") | |
| 171 | + |
| 172 | +## Output |
| 173 | + |
| 174 | +The function returns a JSON object containing: |
| 175 | +- For `parse`: Complete JWT payload |
| 176 | +- For `extractUser`: Standard user claims (sub, preferred_username, email, name, etc.) |
| 177 | +- For `extractClaim`: The specific claim value |
| 178 | + |
| 179 | +## Token Format Support |
| 180 | + |
| 181 | +The function supports JWT tokens in various formats: |
| 182 | +- Raw JWT token: `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...` |
| 183 | +- Bearer token: `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...` |
| 184 | + |
| 185 | +## Error Handling |
| 186 | + |
| 187 | +The function will fail if: |
| 188 | +- Token is null or empty |
| 189 | +- Token format is invalid |
| 190 | +- Required claim parameter is missing for extractClaim operation |
| 191 | + |
| 192 | +## Security Note |
| 193 | + |
| 194 | +This function extracts claims from JWT tokens without signature verification. In production environments, ensure proper token validation is performed at the API gateway or authentication layer before tokens reach the workflow. |
0 commit comments