使用 Auth0 授权用户访问某些页面的正确方法
Correct method of authorising users to certain pages using Auth0
我正在使用 Auth0 创建用户授权,我还将用户分配给组,然后在组内分配角色。限制用户访问某些页面和路由的正确约定是什么?
目前我正在做以下事情:
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
if 'profile' not in session:
# Redirect to Login page here
return redirect('/')
return f(*args, **kwargs)
return decorated
@app.route('/dashboard')
@requires_auth
def dashboard():
if not authorization_check(['GroupNameExample']):
return redirect(url_for('logout')) # instead of an error page for now
else:
return render_template('dashboard.html')
def authorization_check(groups):
user_group = session['profile']['security']['groups'][0]
if user_group not in groups:
return False
else:
return True
所以我只是在做一个基本的 IF 语句,这样对吗?
这可以通过角色和直接用户 ID 主动完成。下面是一个利用 ID 令牌处理拒绝访问错误的示例:
function (user, context, callback) {
if (context.clientID === "BANNED_CLIENT_ID") {
return callback(new UnauthorizedError('Access to this application has been temporarily revoked'));
}
callback(null, user, context);
}
这将导致重定向到您的回调 URL,并带有包含您设置的消息的错误查询字符串参数。 (例如 https://yourapp.com/callback?error=unauthorized&error_description=Access%20to%20this%20application%20has%20been%20temporarily%20revoked)。确保使用 UnauthorizedError(不是 Error)的实例调用回调。
我正在使用 Auth0 创建用户授权,我还将用户分配给组,然后在组内分配角色。限制用户访问某些页面和路由的正确约定是什么?
目前我正在做以下事情:
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
if 'profile' not in session:
# Redirect to Login page here
return redirect('/')
return f(*args, **kwargs)
return decorated
@app.route('/dashboard')
@requires_auth
def dashboard():
if not authorization_check(['GroupNameExample']):
return redirect(url_for('logout')) # instead of an error page for now
else:
return render_template('dashboard.html')
def authorization_check(groups):
user_group = session['profile']['security']['groups'][0]
if user_group not in groups:
return False
else:
return True
所以我只是在做一个基本的 IF 语句,这样对吗?
这可以通过角色和直接用户 ID 主动完成。下面是一个利用 ID 令牌处理拒绝访问错误的示例:
function (user, context, callback) {
if (context.clientID === "BANNED_CLIENT_ID") {
return callback(new UnauthorizedError('Access to this application has been temporarily revoked'));
}
callback(null, user, context);
}
这将导致重定向到您的回调 URL,并带有包含您设置的消息的错误查询字符串参数。 (例如 https://yourapp.com/callback?error=unauthorized&error_description=Access%20to%20this%20application%20has%20been%20temporarily%20revoked)。确保使用 UnauthorizedError(不是 Error)的实例调用回调。