我对在rest api上实现安全认证是不熟悉的。我正在尝试从AWS Cognito用户池中实现oauth2授权。
from flask import Flask,jsonify,render_template,request
import requests
import socket
app = Flask(__name__)
name='Umesh'
App_client_id = 'cleintId'
App_client_secret = 'clientSecret'
url = 'https://myurl.auth.eu-west-1.amazoncognito.com'
targetApi = 'https://myTargetApi/v1/product'
class setTokenCache():
cache=''
obj= setTokenCache()
#print("Acces token is ", obj.cache)
@app.route('/login')
def index():
try:
grant_type = 'client_credentials'
response = requests.post(url + '/oauth2/token',
auth=(App_client_id, App_client_secret),
data={'grant_type': grant_type, 'client_id': App_client_id,
'client_secret': App_client_secret})
print("Staus code", response)
if response.status_code!=200:
return "You are not authenticated"
else:
result = response.json()
obj.cache = result['access_token']
print("Access token is",obj.cache)
return "You are Logined"
except socket.gaierror as e:
print("Unable to get the r")
这里是api返回#'You are Logined',如果它刚刚通过访问令牌进行了身份验证。只需考虑其登录部分。
现在我有了这个API,我想使用上面生成的访问令牌获得有效的响应。
@app.route('/getProductDetails', methods=['GET', 'POST'])
def productDetails():
print("Access token is",obj.cache)
headers = {'Authorization': 'Bearer ' + accessToken, 'Accept': 'application/json'}
try:
response = requests.get(targetApi, headers=headers)
return response.json()
except socket.gaierror as e:
print("Unable to get the r")
这里我需要传递#accessToken才能获得有效的响应,否则它将返回,#unauthrized
现在,我想知道我应该如何以这种方式在此api上实现这种安全性
http://127.0.0.1:5000/getProductDetails
或2.如何确保浏览器/客户端从API 1导航到API直至过期之前如何使用相同的令牌
是否有人可以提供帮助?
就Cognito令牌验证而言,这是主要步骤,您应该为您的编程语言找到一个库。以下是我的一些资源可能会有所帮助: