马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
Jira设置应用步伐链接
1) 创建应用步伐链接
- 登录 JIRA 管理后台。
- 转到 Administration > Applications > Application Links。
- 在输入框中输入外部应用步伐的 URL(例如 GitLab 或自界说应用),然后点击 Create new link。
2) 设置 Consumer Key 和 Public Key
- 在创建应用链接的过程中:
- Consumer Key:自界说一个唯一的字符串标识应用步伐(例如 jira-oauth-client)。
- Public Key:需要将客户端的公钥粘贴到这里(私钥用于客户端哀求使用)。
- 公钥的格式应为 PEM 格式,示例如下:
- -----BEGIN PUBLIC KEY-----
- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnVOGkhN0P3y+5FNceXGy
- q+YBl9cJpOyYsGnO94RtuOCmj/KFszDvn/dNqg2KjGvB5MgkHslZ8ZfZkXHZwIfB
- <...省略其余内容...>
- -----END PUBLIC KEY-----
复制代码 3) 生存设置
- 确保填写的 Consumer Key 和 Public Key 在客户端代码中保持一致。
- 生存链接。
提示:在Jira中设置应用步伐链接的【链入认证】时没有设置客户回调URL,只设置了客户关键字(consumer_key),RSA加密公钥(public_key)
在 JIRA 中设置 应用步伐链接(Application Link) 时,如果没有 consumer_secret 值,这其实是正常的情况。JIRA 的 OAuth 1.0a 认证机制只需要设置 Consumer Key 和 Public Key。
JIRA OAuth1为什么没有 consumer_secret?
- OAuth 1.0a 认证:
- 在 JIRA 的 OAuth 1.0a 实现中,consumer_secret 并不是必须的部分。
- JIRA 使用 Consumer Key 来标识应用步伐,并通过 Public Key 验证署名。
- 与其他 OAuth 1.0a 实现不同,JIRA 中的密钥对是基于 RSA 的公钥加密系统:
- Public Key 设置在 JIRA 中。
- Private Key 生存在客户端,并用于署名哀求。
python代码示例
- import requests
- from requests_oauthlib import OAuth1Session
- # 这些值来自你在 JIRA 上创建的 OAuth 应用
- # 替换为你的 Consumer Key
- consumer_key = 'your-consumer-key'
- # 替换为你的 Consumer Secret,我在调用调用jira接口认证时,在jira中配置程序链接时,没有地方配置这个secret,因此这里可以空着不用,生成OAuth1Session对象时也不传这个参数即可,通过consumer_key判断请求客户端
- consumer_secret = 'your-consumer-secret'
- # 私钥,对应的公钥是配置在JIRA中的
- PRIVATE_KEY = """
- -----BEGIN RSA PRIVATE KEY-----
- <your_private_key_here>
- -----END RSA PRIVATE KEY-----
- """
- # 替换为你的 JIRA 实例 URL
- jira_url = 'https://your-jira-instance.atlassian.net'
- # 请求 OAuth Token 的 URL
- request_token_url = f'{jira_url}/plugins/servlet/oauth/request-token'
- authorize_url = f'{jira_url}/plugins/servlet/oauth/authorize'
- access_token_url = f'{jira_url}/plugins/servlet/oauth/access-token'
- # 步骤 1: 获取请求令牌(Request Token)
- oauth = OAuth1Session(client_key=consumer_key, rsa_key=private_key,signature_method="RSA-SHA1",callback_uri="oob")
- # 获取请求令牌和请求令牌的 URL
- request_token_response = oauth.fetch_request_token(request_token_url)
- # 解析返回的令牌信息
- request_token = request_token_response.get('oauth_token')
- request_token_secret = request_token_response.get('oauth_token_secret')
- print(f'Request Token: {request_token}')
- print(f'Request Token Secret: {request_token_secret}')
- # 步骤 2: 用户授权
- print(f'Go to the following URL to authorize the application: {authorize_url}?oauth_token={request_token}')
- # 确保用户输入授权码
- oauth_verifier = input('Enter the verification code (oauth_verifier): ')
- # 步骤 3: 使用授权的 Token 获取访问令牌(Access Token)
- # 传递 `verifier` 参数
- oauth_tokens = oauth.fetch_access_token(access_token_url, verifier=oauth_verifier)
- access_token = oauth_tokens.get('oauth_token')
- access_token_secret = oauth_tokens.get('oauth_token_secret')
- print(f'Access Token: {access_token}')
- print(f'Access Token Secret: {access_token_secret}')
复制代码 碰到的问题
天生的JIRA授权URL没有verification code
点击步伐天生的授权链接,然后点击allow按钮后,页面跳转后表现的信息如下:
- You have successfully authorised 'xxxx'. Please close this browser window and click continue in the client.
复制代码 这个信息里没有verification code,跟之前用java步伐天生的授权URL得到的信息不一样,精确的信息应如下
- You have successfully authorised 'xxxx'. Your verification code is 'GPmbXd'. You will need to enter this exact text when prompted. You should write this value down before closing the browser window.
复制代码 通过对比原来的java步伐和新写的python步伐,发现原来的java在获取request token中,会设置callback = "oob"
- JiraOAuthGetTemporaryToken oauthGetTemporaryToken = new JiraOAuthGetTemporaryToken(reuqestTokenUrl);
- // 设置RSA验签私钥
- OAuthRsaSigner oauthRsaSigner = new OAuthRsaSigner();
- oauthRsaSigner.privateKey = xxxxx
- oauthGetTemporaryToken.consumerKey = xxx
- oauthGetTemporaryToken.signer = oauthRsaSigner;
- oauthGetTemporaryToken.transport = new ApacheHttpTransport();
- //设置回调为oob
- oauthGetTemporaryToken.callback="oob";
复制代码 我们看下1.0和1.0a协议的一些要求
- 1.0协议中callback在oauth认证的第二步(哀求用户授权)中通过url参数设置并明文传输。新的1.0a协议中,callback作为oauth第一步(获取request token)的参数(oauth_callback),并参与到署名过程。别的当没有callback时,callback值必须设为oob(out-of-bound)。
- Provider在返回request token的同时,返回一个oauth_callback_confirmed参数,其值为true,表示Provider支持1.0a协议。
- 用户完成授权后,Provider在redirect到callback时,提供一个新的oauth_verifier参数(无法被猜测的随机字串),该参数会被用在request token换取access token的过程中。如果callback值为oob,那么Provider需要提供页面表现oauth_verifier参数值。而Consumer可以引导用户将这个参数值填入得当位置,来完成授权
requests_oauthlib.oauth1_session.VerifierMissing: No client verifier has been set.
这个问题处置惩罚起来就比较简单了,通过调试步伐看到verification_code也的确传到fetch_access_token方法参数中了,但是还是说没有设置verifier,然后进入到oauth1_session.py代码中调试,发现其对verifier的判断逻辑如下
- if verifier:
- self._client.client.verifier = verifier
- if not getattr(self._client.client,"verifier",None):
- raise VerifierMissing("No client verifier has been set.")
- token = self._fetch_token(url,**request_kwargs)
复制代码 发现是参数名写的不对,之前代码中参数设置是
- oauth_tokens = oauth.fetch_access_token(access_token_url, oauth_verifier=oauth_verifier)
复制代码 修改后参数设置是
- oauth_tokens = oauth.fetch_access_token(access_token_url, verifier=oauth_verifier)
复制代码
参考
OAuth协议更新到1.0a
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |