python-如何使用用户默认凭据以编程方式向云身份识别代理(Cloud IAP)保护的资源进行身份验证?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-如何使用用户默认凭据以编程方式向云身份识别代理(Cloud IAP)保护的资源进行身份验证?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3383字,纯文字阅读大概需要5分钟。
内容图文
![python-如何使用用户默认凭据以编程方式向云身份识别代理(Cloud IAP)保护的资源进行身份验证?](/upload/InfoBanner/zyjiaocheng/686/b394fa8c1b0242a9a98e77e877050921.jpg)
我希望能够在开发环境(即我自己的安装并登录了Google Cloud SDK的笔记本电脑)上使用用户默认凭据,以编程方式为iap生成ID令牌.
当遵循documentation时,我设法使用服务帐户文件生成了授权令牌.
在个人计算机上使用google.auth.default时,我可以看到google.oauth2.credentials类型的凭据.凭据具有refresh_token.我想用它来生成令牌,因为它是在documentation下的curl中完成的
从桌面应用程序进行身份验证->正在访问该应用程序,但无法使其正常运行.有人知道是否可以通过这种方式进行身份验证吗?
解决方法:
如Matthew所述,用于获取刷新令牌的Client ID项目应与IAP Client ID项目匹配. Gcloud使用在path_to / google-cloud-sdk / lib / googlecloudsdk / api_lib / auth / util.py中定义的客户端ID和密码作为默认凭据(DEFAULT_CREDENTIALS_DEFAULT_CLIENT_ID和DEFAULT_CREDENTIALS_DEFAULT_CLIENT_SECRET).因此,如果不更改util.py,就无法使用来自google.auth.default()的刷新令牌,因为获取ID令牌的尝试将失败:
{
"error": "invalid_audience",
"error_description": "The audience client and the client need to be in the same project."
}
您的选择是:
>根据Matthew的回复/文档获取刷新令牌(将其缓存以避免每次都需要用户授予权限)和ID令牌.
> gcloud util.py中存在的补丁程序客户端ID和密码(可能会随gcloud更新而更改).
这两个选项的示例代码:
import google.auth
import requests
import json
from webbrowser import open_new_tab
from time import sleep
# use gcloud app default credentials if gcloud's util.py is patched
def id_token_from_default_creds(audience):
cred, proj = google.auth.default()
# data necessary for ID token
client_id = cred.client_id
client_secret= cred.client_secret
refresh_token = str(cred.refresh_token)
return id_token_from_refresh_token(client_id, client_secret, refresh_token, audience)
def id_token_from_refresh_token(client_id, client_secret, refresh_token, audience):
oauth_token_base_URL = "https://www.googleapis.com/oauth2/v4/token"
payload = {"client_id": client_id, "client_secret": client_secret,
"refresh_token": refresh_token, "grant_type": "refresh_token",
"audience": audience}
res = requests.post(oauth_token_base_URL, data=payload)
return (str(json.loads(res.text)[u"id_token"]))
# obtain ID token for provided Client ID: get authorization code -> exchange for refresh token -> obtain and return ID token
def id_token_from_client_id(client_id, client_secret, audience):
auth_code = get_auth_code(client_id)
refresh_token = get_refresh_token_from_code(auth_code, client_id, client_secret)
return id_token_from_refresh_token(client_id, client_secret, refresh_token, audience)
def get_auth_code(client_id):
auth_url = "https://accounts.google.com/o/oauth2/v2/auth?client_id=%s&response_type=code&scope=openid%%20email&access_type=offline&redirect_uri=urn:ietf:wg:oauth:2.0:oob"%client_id
open_new_tab(auth_url)
sleep(1)
return raw_input("Authorization code: ")
def get_refresh_token_from_code(auth_code, client_id, client_secret):
oauth_token_base_URL = 'https://www.googleapis.com/oauth2/v4/token'
payload = {"code": auth_code, "client_id": client_id, "client_secret": client_secret,
"redirect_uri": "urn:ietf:wg:oauth:2.0:oob", "grant_type": "authorization_code"}
res = requests.post(oauth_token_base_URL, data=payload)
return (str(json.loads(res.text)[u"refresh_token"]))
print("ID token from client ID: %s" % id_token_from_client_id("<Other client ID>", "<Other client secret>", "<IAP Client ID>")) # other client ID should be from the same project as IAP Client ID
print("ID token from \"default\" credentials: %s" % id_token_from_default_creds("<IAP Client ID>"))
内容总结
以上是互联网集市为您收集整理的python-如何使用用户默认凭据以编程方式向云身份识别代理(Cloud IAP)保护的资源进行身份验证?全部内容,希望文章能够帮你解决python-如何使用用户默认凭据以编程方式向云身份识别代理(Cloud IAP)保护的资源进行身份验证?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。