使用 Oauth2 在 Flask 中使用特定托管域 (hd) 对用户进行身份验证
Authenticate user with a specific hosted domain (hd) in Flask with Oauth2
您好,这是我当前的 python 网络应用程序中的身份验证代码。
auth_flow = OAuth2WebServerFlow(client_id=AUTH_CLIENT_ID,
client_secret=AUTH_CLIENT_SECRET,
scope=AUTH_PLUS_SCOPE,
redirect_uri=AUTH_CALLBACK_URI)
AUTH_ARGS = {
'error': fields.Str(),
'code': fields.Str()
}
@auth_api.route('/', methods=['GET'])
def get_auth_uri():
if current_user and current_user.is_authenticated:
return redirect(LOGIN_PATH)
else:
auth_uri = auth_flow.step1_get_authorize_url()
return redirect(auth_uri)
@auth_api.route('/oauth2callback', methods=['GET'])
@use_kwargs(AUTH_ARGS)
def oauth2_callback(error, code):
if error:
return redirect(url_for('login'), code=HTTPStatus.TEMPORARY_REDIRECT)
else:
try:
authenticate(code)
return redirect(LOGIN_PATH)
except Unauthorized:
raise APIError(HTTPStatus.UNAUTHORIZED, AUTH_FAILED_MESSAGE)
@auth_api.route('/me', methods=['GET'])
@login_required
def get_current_user():
return jsonify(current_user.serialized)
@auth_api.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('login'))
def authenticate(code):
try:
credentials = _get_credentials(code)
user = User.get_or_create(credentials)
login_user(user)
except FlowExchangeError:
raise Unauthorized()
def _get_credentials(code):
return auth_flow.step2_exchange(code)
但是,现在,我的应用程序正在接受来自任何 google 帐户地址的登录。我想限制具有指定域的用户登录的访问权限,如 abc@harvard.edu。我看过一些关于高清参数的答案,但我不知道如何使用它。
可以通过将 hd 参数附加到 auth_uri
来设置 hd 参数,如下所示
auth_uri = GOOGLE_AUTH_URI
auth_uri = auth_uri + '?hd=' + 'example.com'
auth_flow = OAuth2WebServerFlow(client_id=AUTH_CLIENT_ID,
client_secret=AUTH_CLIENT_SECRET,
scope=AUTH_PLUS_SCOPE,
redirect_uri=AUTH_CALLBACK_URI,
auth_uri=auth_uri,
)
但是根据我的经验,hd 参数不会限制具有不同电子邮件地址的用户登录。您应该始终验证凭据。
def authenticate(code):
try:
credentials = _get_credentials(code)
if not credentials.id_token['email'].endswith('@example.com')
# abort(401)
raise Unauthorized()
user = User.get_or_create(credentials)
login_user(user)
except FlowExchangeError:
raise Unauthorized()
您好,这是我当前的 python 网络应用程序中的身份验证代码。
auth_flow = OAuth2WebServerFlow(client_id=AUTH_CLIENT_ID,
client_secret=AUTH_CLIENT_SECRET,
scope=AUTH_PLUS_SCOPE,
redirect_uri=AUTH_CALLBACK_URI)
AUTH_ARGS = {
'error': fields.Str(),
'code': fields.Str()
}
@auth_api.route('/', methods=['GET'])
def get_auth_uri():
if current_user and current_user.is_authenticated:
return redirect(LOGIN_PATH)
else:
auth_uri = auth_flow.step1_get_authorize_url()
return redirect(auth_uri)
@auth_api.route('/oauth2callback', methods=['GET'])
@use_kwargs(AUTH_ARGS)
def oauth2_callback(error, code):
if error:
return redirect(url_for('login'), code=HTTPStatus.TEMPORARY_REDIRECT)
else:
try:
authenticate(code)
return redirect(LOGIN_PATH)
except Unauthorized:
raise APIError(HTTPStatus.UNAUTHORIZED, AUTH_FAILED_MESSAGE)
@auth_api.route('/me', methods=['GET'])
@login_required
def get_current_user():
return jsonify(current_user.serialized)
@auth_api.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('login'))
def authenticate(code):
try:
credentials = _get_credentials(code)
user = User.get_or_create(credentials)
login_user(user)
except FlowExchangeError:
raise Unauthorized()
def _get_credentials(code):
return auth_flow.step2_exchange(code)
但是,现在,我的应用程序正在接受来自任何 google 帐户地址的登录。我想限制具有指定域的用户登录的访问权限,如 abc@harvard.edu。我看过一些关于高清参数的答案,但我不知道如何使用它。
可以通过将 hd 参数附加到 auth_uri
来设置 hd 参数,如下所示
auth_uri = GOOGLE_AUTH_URI
auth_uri = auth_uri + '?hd=' + 'example.com'
auth_flow = OAuth2WebServerFlow(client_id=AUTH_CLIENT_ID,
client_secret=AUTH_CLIENT_SECRET,
scope=AUTH_PLUS_SCOPE,
redirect_uri=AUTH_CALLBACK_URI,
auth_uri=auth_uri,
)
但是根据我的经验,hd 参数不会限制具有不同电子邮件地址的用户登录。您应该始终验证凭据。
def authenticate(code):
try:
credentials = _get_credentials(code)
if not credentials.id_token['email'].endswith('@example.com')
# abort(401)
raise Unauthorized()
user = User.get_or_create(credentials)
login_user(user)
except FlowExchangeError:
raise Unauthorized()