【问题标题】:How to generate JWS in R language如何用R语言生成JWS
【发布时间】:2015-11-09 23:47:29
【问题描述】:

我尝试为 google-oauth2.0 ServiceAccount 生成 JWT。

我设置了标头和有效负载(声明)。但是,当我尝试使用 RSASHA256 感叹 base64header.base64claim 时,我得到了不正确的签名。 我在 PKI 包中发现只有一个函数允许使用具有指定散列函数的 RSA 对 contet 进行签名。

我是如何发现我的签名不正确的?我发现 resource 可以从输入和私有 KEY 生成 JWT。

所以我可以看到,我来自 R 函数的签名与 jwt.io 签名不同。 我已经使用两个 JWT 令牌测试了对 https://www.googleapis.com/oauth2/v3/token 的请求,并且 jwt.io 一个正在工作。

这部分用于 JWT 标头。

library(base64enc)
library(jsonlite)
library(PKI)
#JWT header set up
    alg <- "RS256"
    typ <- "JWT"
    header <- list("alg" = alg, "typ" = typ)
    h <- toJSON(header, auto_unbox=TRUE)
    enc.header <- base64encode(charToRaw(h))

此部分用于 JWT 声明(有效负载)

iss <- "165724828594-mkuchqogmjapbl7mpfn0e7f7o3qlrqsr@developer.gserviceaccount.com"
        scope <- "https://www.googleapis.com/auth/analytics.readonly"
    aud <- "https://www.googleapis.com/oauth2/v3/token"
    iat <- as.integer(as.POSIXct(Sys.time()))
    exp <- iat+3600
    claim <- list("iss" = iss, "scope" = scope, "aud" = aud, "exp" = exp, "iat" = iat)
    cl <- toJSON(claim, auto_unbox=TRUE)
    enc.claim <- base64encode(charToRaw(cl))

这是我的问题。

y <- file("~/keys/euroset-test-70c2d0d4eed1.pem")
key <- PKI.load.key(y)
what <- paste(enc.header,enc.claim, sep=".")
JWS <- PKI.sign(what, key, "SHA256")
enc.sign <- base64encode(JWS)
JWT <- paste(what,enc.sign, sep=".")
JWT

有什么帮助吗? 我已经用 JWS 坚持了 4 天了。(

【问题讨论】:

    标签: r google-oauth jwt


    【解决方案1】:

    我终于找到了问题所在。 它一直是关于 base64 编码的。 我检查了 Correct 和 Inctorrect JWT 并找到了一些模式。 错误的有效载荷和签名中有"==",我已将其替换为""。 同样在签名中,所有"/" 我都替换为"_",所有"+" 都替换为"-"。 希望它能给有同样问题的人一个提示。

    【讨论】:

      【解决方案2】:

      https://github.com/hadley/httr/blob/master/R/oauth-server-side.R

      这是获取适用于某些 Google API 的令牌的代码(但不适用于我需要的云......如果你能正常工作,请告诉我)。此外,httr oauth_service_token 比自己编写代码更容易使用。

      init_oauth_service_account <- function(endpoint, secrets, scope = NULL) {
        signature <- jwt_signature(secrets, scope = scope)
      
        res <- POST(endpoint$access, body = list(
          grant_type = "urn:ietf:params:oauth:grant-type:jwt-bearer",
          assertion = signature
        ), encode = "form")
        stop_for_status(res)
      
        content(res, type = "application/json")
      }
      ...
      jwt_base64 <- function(x) base64url(jwt_json(x))
      jwt_json <- function(x) jsonlite::toJSON(x, auto_unbox = TRUE)
      base64url <- function(x) {
        if (is.character(x)) {
          x <- charToRaw(x)
        }
        out <- chartr('+/', '-_', base64enc::base64encode(x))
        gsub("=+$", "", out)
      }
      

      【讨论】:

      • 感谢有用的链接。但是这里有一件事,我没有找到如何为这个功能设置代理使用。我工作场所的防火墙不允许我在没有代理设置的情况下发送请求。在 httr::POST 中,我能够将代理设置设置为配置。 out &lt;- chartr('+/', '-_', base64enc::base64encode(x)) gsub("=+$", "", out) 这部分告诉我我的回答是正确的,JWT 不正确,因为 +,/,=.
      猜你喜欢
      • 2017-02-09
      • 2020-04-23
      • 1970-01-01
      • 2012-11-29
      • 2020-11-17
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      • 2019-08-06
      相关资源
      最近更新 更多