如何在空手道中生成 auth 2.0 我在空手道演示项目中看到了一个示例,但在我们的例子中,我们需要将其发送为 "Authorization Code"

how to generate auth 2.0 in karate I saw a sample in karate Demo project but in our case we need to send it as "Authorization Code"

如何通过空手道生成 OAuth 2.0 令牌。

我们在 Postman 中的尝试方式:

  1. 在授权选项卡上 select OAuth 2.0
  2. Select Header 前缀承载
  3. Grant-Type 是“授权码”
  4. 回调 URL 被 select 编辑,因为当我们点击提交时它会重定向到我们必须输入凭据的浏览器,一旦用户通过验证,浏览器就会重定向回 Postman
  5. 添加“Auth URL”和“Access Token URL”
  6. 输入“客户端 ID”和“客户端密码”
  7. Select“客户端身份验证”作为基本身份验证发送Header。

Postman 然后重定向到我们输入用户名和密码的浏览器,一旦通过身份验证,它将用户重定向回带有访问令牌的 postman。

问题: 当我们在 Karate 中提供 grant_type 作为“授权码”时,我们收到一个错误 {"error":"unsupported_grant_type","error_description":"Unsupported grant_type"}。在这里提供什么,当我们提供“密码”时,我们得到 401,当我们提供“授权码”时,我们得到 400。

其次,我们是否可以自动执行这样的场景,其中也调用浏览器并且我们必须输入凭据我们可以通过空手道实现它,因为我们必须存储令牌并传入 APIs 吗?

  Background:
    * url 'http://localhost:8080/pathdetails'

  Scenario: get all users and then get the first user by id
    * path 'token'
    * form field grant_type = 'authorization code'
    * form field client_id = 'ourapiclient'
    * form field client_secret = '324243324-3334-334-343-3432423424'
    * method post
    * status 200

    * def accessToken = response.access_token

已编辑**********

我现在尝试向 Auth URL 发送 API 请求,该请求重定向到浏览器和 returns HTML 页面。

    Given url 'http://localhost:8080/myurlpath/auth'
    * form field response_type = 'code'
    * form field client_id = 'abcc'
    * form field scope = 'openconnect'
    * form field redirect_uri = 'http://localhost:8080/redirecturlpath'
    * form field state = 'cEY3R-YfsoM9232diS72COdHTA8uPv9K49pjZaPag5M.8akinzwobn8.abcd4'
    * method get
    * status 200
    * print 'Response is........',response

此 return 编辑了一个 HTML 页面,该页面与我从 Postman 发送请求时看到的页面完全相同。现在如何在此 html 页面上用空手道输入用户名和密码,因为此页面已 return 编辑为上述 API.

响应的一部分

我期待上面会 return 给我一个代码,然后我会调用请求令牌端点,但上面将我重定向到我输入用户名和密码的地方,然后一旦成功,它就会重定向回 Postman在 URL 中我也可以看到代码。

curl --request POST \
  --url 'https://YOUR_DOMAIN/oauth/token' \
  --header 'content-type: application/x-www-form-urlencoded' \
  --data grant_type=authorization_code \
  --data 'client_id=YOUR_CLIENT_ID' \
  --data client_secret=YOUR_CLIENT_SECRET \
  --data code=YOUR_AUTHORIZATION_CODE \
  --data 'redirect_uri=https://YOUR_APP/callback'

如何获取令牌API所需的代码?

我尝试发送 Auth API 进行如下访问,但没有代码或令牌在响应中得到 returned。

Given driver 'http://localhost:8080/myurlpath/auth?scope=openconnect&state=cEY3R-YfsoM9232diS72COdHTA8uPv9K49pjZaPag5M.8akinzwobn8.abcd4&response_type=code&client_id=abcc&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fauth%2Fmyurlpath'
* fullscreen()
And input('#username', 'username')
And input('#password', 'password')
When click('#login')

以上 return 没有任何错误,但 return 我正在寻找的代码也没有

@Maddy 要查看授权类型,您需要访问 auth0,或者让您的开发人员告诉您此处实施了哪些授权,您可以阅读更多内容: https://auth0.com/docs/configure/applications/application-grant-types

在这里您可以阅读如何实现授权代码流: https://auth0.com/docs/login/authentication/add-login-auth-code-flow

为了让您的生活更轻松您可以要求开发人员实施 Password-realm-grant 但不推荐这样做。

以下是纠正 oAuth 2.0 令牌生成之一的方法

* def cid = 'client_id'
* def csec = 'token_secret'
* def AuthCode = Java.type('com.test.qa.aut.authCode')  
* print AuthCode.Code()  
* def authentication = 'Basic ' + AuthCode.Code(cid, csec)
* print authentication

* url 'https://acpint.online.com/default/np/oauth2/'
* header Authorization = authentication
And header Content-Type = 'application/x-www-form-urlencoded; charset=utf-8'
* form field grant_type = 'client_credentials'
Then method post
And status 200
Then print response

Java class:

package com.test.qa.aut;
import java.util.Base64;
public class authCode {
    public static String Code(String clientId, String clientSecret) {
        String auth = clientId + ":" + clientSecret;
        String authentication = Base64.getEncoder().encodeToString(auth.getBytes());
        return authentication;
    }
}