exercise5 covers how to build complex objects as CEL literals. Given the input `now`, construct a JWT with an expiry of 5 minutes.
()
| 200 | // |
| 201 | // Given the input `now`, construct a JWT with an expiry of 5 minutes. |
| 202 | func exercise5() { |
| 203 | fmt.Println("=== Exercise 5: Building JSON ===\n") |
| 204 | // Note the quoted keys in the CEL map literal. For proto messages the |
| 205 | // field names are unquoted as they represent well-defined identifiers. |
| 206 | env, _ := cel.NewEnv( |
| 207 | cel.Variable("now", cel.TimestampType), |
| 208 | ) |
| 209 | ast := compile(env, ` |
| 210 | {'aud': 'my-project', |
| 211 | 'exp': now + duration('300s'), |
| 212 | 'extra_claims': { |
| 213 | 'group': 'admin' |
| 214 | }, |
| 215 | 'iat': now, |
| 216 | 'iss': 'auth.acme.com:12350', |
| 217 | 'nbf': now, |
| 218 | 'sub': 'serviceAccount:delegate@acme.co' |
| 219 | }`, |
| 220 | cel.MapType(cel.StringType, cel.DynType)) |
| 221 | |
| 222 | program, _ := env.Program(ast) |
| 223 | out, _, _ := eval( |
| 224 | program, |
| 225 | map[string]any{ |
| 226 | "now": time.Now(), |
| 227 | }, |
| 228 | ) |
| 229 | // The output of the program is a CEL map type, but it can be converted |
| 230 | // to a JSON representation using the `ConvertToNative` method. |
| 231 | fmt.Printf("------ type conversion ------\n%v\n", valueToJSON(out)) |
| 232 | fmt.Println() |
| 233 | } |
| 234 | |
| 235 | // exercise6 describes how to build proto message types within CEL. |
| 236 | // |