Python 2.7 使用GitLab对终端进行身份验证
我有一个终端,在webbrowser和wetty服务。我想对gitlab中的用户进行身份验证,以允许用户与终端交互(它位于docker容器内。当用户进行身份验证时,我将允许他查看容器终端) 我正在尝试使用OAuth2.0,但无法实现 这就是我所尝试的Python 2.7 使用GitLab对终端进行身份验证,python-2.7,flask,oauth-2.0,gitlab,Python 2.7,Flask,Oauth 2.0,Gitlab,我有一个终端,在webbrowser和wetty服务。我想对gitlab中的用户进行身份验证,以允许用户与终端交互(它位于docker容器内。当用户进行身份验证时,我将允许他查看容器终端) 我正在尝试使用OAuth2.0,但无法实现 这就是我所尝试的 我在gitlab上创建了一个应用程序 获取代码和密码,并使用python脚本进行http调用 脚本指示我进入登录和身份验证页面 我试图获取代码,但失败了(我认为他们的代码没有错) 现在问题从这里开始。我需要从重定向的url获取身份验证代码以获得访问
from flask import Flask, abort, request
from uuid import uuid4
import requests
import requests.auth
import urllib2
import urllib
CLIENT_ID = "clientid"
CLIENT_SECRET = "clientsecret"
REDIRECT_URI = "https://UnrelevantFromGitlabLink.com/console"
def user_agent():
raise NotImplementedError()
def base_headers():
return {"User-Agent": user_agent()}
app = Flask(__name__)
@app.route('/')
def homepage():
text = '<a href="%s">Authenticate with gitlab</a>'
return text % make_authorization_url()
def make_authorization_url():
# Generate a random string for the state parameter
# Save it for use later to prevent xsrf attacks
state = str(uuid4())
save_created_state(state)
params = {"client_id": CLIENT_ID,
"response_type": "code",
"state": state,
"redirect_uri": REDIRECT_URI,
"scope": "api"}
url = "https://GitlapDomain/oauth/authorize?" + urllib.urlencode(params)
print get_redirected_url(url)
print(url)
return url
# Left as an exercise to the reader.
# You may want to store valid states in a database or memcache.
def save_created_state(state):
pass
def is_valid_state(state):
return True
@app.route('/console')
def reddit_callback():
print("-----------------")
error = request.args.get('error', '')
if error:
return "Error: " + error
state = request.args.get('state', '')
if not is_valid_state(state):
# Uh-oh, this request wasn't started by us!
abort(403)
code = request.args.get('code')
print(code.json())
access_token = get_token(code)
# Note: In most cases, you'll want to store the access token, in, say,
# a session for use in other parts of your web app.
return "Your gitlab username is: %s" % get_username(access_token)
def get_token(code):
client_auth = requests.auth.HTTPBasicAuth(CLIENT_ID, CLIENT_SECRET)
post_data = {"grant_type": "authorization_code",
"code": code,
"redirect_uri": REDIRECT_URI}
headers = base_headers()
response = requests.post("https://MyGitlabDomain/oauth/token",
auth=client_auth,
headers=headers,
data=post_data)
token_json = response.json()
return token_json["access_token"]
if __name__ == '__main__':
app.run(host="0.0.0.0",debug=True, port=65010)
从烧瓶导入烧瓶,中止,请求
从uuid导入uuid4
导入请求
导入请求.auth
导入urllib2
导入URL库
客户端\u ID=“clientid”
客户_SECRET=“clientsecret”
重定向_URI=”https://UnrelevantFromGitlabLink.com/console"
def user_agent():
引发未实现的错误()
def base_头()
返回{“用户代理”:User_Agent()}
app=烧瓶(名称)
@应用程序路径(“/”)
def homepage():
文本=“”
返回文本%make\u authorization\u url()
def make_authorization_url():
#为状态参数生成随机字符串
#保存它以供以后使用,以防止xsrf攻击
state=str(uuid4())
保存创建的状态(状态)
params={“client_id”:client_id,
“响应类型”:“代码”,
“国家”:国家,
“重定向uri”:重定向uri,
“范围”:“api”}
url=”https://GitlapDomain/oauth/authorize?“+urllib.urlencode(参数)
打印获取重定向url(url)
打印(url)
返回url
#留给读者作为练习。
#您可能希望在数据库或memcache中存储有效状态。
def save_created_状态(状态):
通过
def为有效状态(状态):
返回真值
@app.route(“/console”)
def reddit_回调():
打印(----------------------)
error=request.args.get('error','')
如果出现错误:
返回“Error:+Error”
state=request.args.get('state','')
如果不是有效状态(状态):
#哦,这个请求不是我们提出的!
中止(403)
code=request.args.get('code')
打印(code.json())
访问令牌=获取令牌(代码)
#注意:在大多数情况下,您需要将访问令牌存储在,
#用于web应用程序其他部分的会话。
return“您的gitlab用户名是:%s”%get\u username(访问\u令牌)
def get_令牌(代码):
client\u auth=requests.auth.HTTPBasicAuth(client\u ID,client\u SECRET)
post_数据={“授权类型”:“授权代码”,
“代码”:代码,
“重定向uri”:重定向uri}
headers=base_headers()
响应=请求。发布(“https://MyGitlabDomain/oauth/token",
auth=客户机的auth,
标题=标题,
数据=post_数据)
token_json=response.json()
返回令牌\u json[“访问令牌”]
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app.run(host=“0.0.0.0”,debug=True,port=65010)
我想我的问题是我的重定向url。因为它只是来自GitLab的一个无关链接,并且没有API可以调用
如果我能开火
@app.route(“/console”)
Python上的这一行我的问题可能会得到解决
我需要修改我的Python脚本或从不同的角度来解决我的问题。请帮忙 我完全无法理解auth2的概念。主要目的是获得访问令牌。当我将回调url更正为localhost时,它工作得非常出色。您的服务器托管在哪里?本地主机还是可从云访问?