MCPcopy Index your code
hub / github.com/aiscriptdev/aiscript / create_access_token

Function create_access_token

aiscript-vm/src/stdlib/auth/jwt.rs:245–347  ·  view source on GitHub ↗
(
    state: &mut State<'gc>,
    args: Vec<Value<'gc>>,
)

Source from the content-addressed store, hash-verified

243}
244
245fn create_access_token<'gc>(
246 state: &mut State<'gc>,
247 args: Vec<Value<'gc>>,
248) -> Result<Value<'gc>, VmError> {
249 match args.len() {
250 2..=4 => { /* Valid number of arguments */ }
251 _ => return Err(VmError::RuntimeError(
252 "create_access_token() requires payload object and duration in seconds. Secret and algorithm are optional.".into()
253 )),
254 }
255
256 // Get payload object
257 let payload = match &args[0] {
258 Value::Object(obj) => obj,
259 _ => {
260 return Err(VmError::RuntimeError(
261 "First argument must be payload object".into(),
262 ));
263 }
264 };
265
266 // Get duration
267 let duration_secs = args[1].as_number()?;
268 if duration_secs <= 0.0 {
269 return Err(VmError::RuntimeError("Duration must be positive".into()));
270 }
271
272 // Get secret (required)
273 let secret = if args.len() >= 3 {
274 args[2].as_string()?
275 } else {
276 return Err(VmError::RuntimeError("Secret key is required".into()));
277 };
278 let secret_str = secret.to_str().unwrap();
279
280 // Get algorithm (optional, defaults to HS256)
281 let algorithm = if args.len() == 4 {
282 let alg = args[3].as_string()?;
283 parse_algorithm(alg.to_str().unwrap())?
284 } else {
285 Algorithm::HS256
286 };
287
288 // Create a claims object from the payload
289 let payload_obj = payload.borrow();
290 let now = Utc::now();
291 let exp = now + Duration::seconds(duration_secs as i64);
292
293 let mut claims = Claims {
294 iss: None,
295 sub: None,
296 aud: None,
297 exp: Some(exp.timestamp()),
298 nbf: Some(now.timestamp()),
299 iat: Some(now.timestamp()),
300 jti: None,
301 extra: HashMap::new(),
302 };

Callers

nothing calls this directly

Calls 9

RuntimeErrorClass · 0.85
parse_algorithmFunction · 0.85
lenMethod · 0.80
as_numberMethod · 0.80
as_stringMethod · 0.80
to_strMethod · 0.80
as_bytesMethod · 0.80
borrowMethod · 0.45
internMethod · 0.45

Tested by

no test coverage detected