P粉7397060892023-08-04 09:11:43
Looks like the problem is with the secret= parameter you passed to jwt_encode_hmac(). The charToRaw function cannot understand hexadecimal numbers, it only uses ASCII character codes. To do the conversion, you need to use one of the hex_to_raw functions from the existing question. I'm using a function here to do the conversion.
hex_to_raw <- function(x) { digits <- strtoi(strsplit(x, "")[[1]], base=16L) as.raw(bitwShiftL(digits[c(TRUE, FALSE)],4) + digits[c(FALSE, TRUE)]) }
Also, you don't need to specify alg and typ in the header, because these will be added automatically by the function. So you can build your token using:
api_admin_key <- "adam:12bd18f2cd12" api_admin_key <- unlist(strsplit(x = api_admin_key, split = ":")) names(api_admin_key) <- c("id", "secret") # Prepare header and payload iat <- as.integer(Sys.time()) header <- list(kid = api_admin_key[["id"]]) # Create the token (including decoding secret) payload <- jose::jwt_claim(iat = iat, exp = iat + 5 * 60, aud = '/admin/') token <- jose::jwt_encode_hmac( claim = payload, secret = hex_to_raw(api_admin_key[["secret"]]), size = 256, header = header )
I tested each token using the debugger at https://jwt.io/ and they appear to be equivalent. In the debugger, the Base64 encoded value of the hexadecimal value "12bd18f2cd12" is "Er0Y8s0S".