权限
Django OAuth工具包提供了一些实用程序类,可与Django rest框架中的其他权限一起使用,因此您可以轻松地将基于作用域的权限检查添加到您的API视图中。
有关如何向您的API终结点添加自定义权限的更多详细信息,请参阅官方 Django REST Framework documentation
TokenHasScope
这个 TokenHasScope 仅当当前访问令牌被授权用于时,权限类才允许访问 all 中列出的作用域 required_scopes 视野的范围。
例如:
class SongView(views.APIView):
authentication_classes = [OAuth2Authentication]
permission_classes = [TokenHasScope]
required_scopes = ['music']
这个 required_scopes 属性是必需的。
TokenHasReadWriteScope
这个 TokenHasReadWriteScope 权限类允许基于 READ_SCOPE 和 WRITE_SCOPE 在设置中配置。
当当前请求的方法是“安全”方法之一时 GET , HEAD , OPTIONS 仅当访问令牌已被授权用于 READ_SCOPE 作用域。当请求的方法是 POST , PUT , PATCH , DELETE 如果访问令牌已被授权用于 WRITE_SCOPE 。
这个 required_scopes 属性是可选的,可以由视图中需要的其他作用域使用。
例如:
class SongView(views.APIView):
authentication_classes = [OAuth2Authentication]
permission_classes = [TokenHasReadWriteScope]
required_scopes = ['music']
当执行请求时, READ_SCOPE \ WRITE_SCOPE 并且需要为当前访问令牌授权‘音乐’作用域。
TokenHasResourceScope
这个 TokenHasResourceScope 仅当当前访问令牌被授权用于时,权限类才允许访问 all 中列出的作用域 required_scopes 视野,但根据要求的方法。
当当前请求的方法是“安全”方法之一时,只有在访问令牌已被授权用于 scope:read 范围(例如 music:read )。当请求的方法是“非安全”方法之一时,只有在访问令牌已被授权用于 scope:write 范围(例如 music:write )。
class SongView(views.APIView):
authentication_classes = [OAuth2Authentication]
permission_classes = [TokenHasResourceScope]
required_scopes = ['music']
这个 required_scopes 属性是必填的(您只需要通知资源范围)。
IsAuthenticatedOrTokenHasScope
这个 IsAuthenticatedOrTokenHasScope 仅当当前访问令牌被授权用于时,权限类才允许访问 all 中列出的作用域 required_scopes 视域,但根据请求的方法。它还允许访问在Django中经过身份验证但未通过OAuth2Authentication类进行身份验证的经过身份验证的用户。这允许使用作用域保护API,但仍然允许用户浏览完整的可浏览API。要限制用户只浏览他们应该被允许查看的可浏览API的部分,您可以将其与DjangoModelPermission或DjangoObjectPermission结合使用。
例如:
class SongView(views.APIView):
permission_classes = [IsAuthenticatedOrTokenHasScope, DjangoModelPermission]
required_scopes = ['music']
这个 required_scopes 属性是必需的。
TokenMatchesOASRequirements
这个 TokenMatchesOASRequirements permission class allows the access based on a per-method basis and with alternative lists of required scopes. This permission provides full functionality required by REST API specifications like the OpenAPI Specification (OAS) security requirement object 。
这个 required_alternate_scopes 属性是以HTTP方法名称为关键字的必需映射,其中每个值都是必需作用域的可选列表的列表。
在下面的示例中,GET需要“Read”作用域,POST需要“Create”作用域 OR “POST”和“Widget”作用域等。
class SongView(views.APIView):
authentication_classes = [OAuth2Authentication]
permission_classes = [TokenMatchesOASRequirements]
required_alternate_scopes = {
"GET": [["read"]],
"POST": [["create"], ["post", "widget"]],
"PUT": [["update"], ["put", "widget"]],
"DELETE": [["delete"], ["scope2", "scope3"]],
}
下面是一个最小的OAS声明,它显示了相同的所需备用作用域。它足够完整,可以在 swagger editor 。
openapi: "3.0.0"
info:
title: songs
version: v1
components:
securitySchemes:
song_auth:
type: oauth2
flows:
implicit:
authorizationUrl: http://localhost:8000/o/authorize
scopes:
read: read about a song
create: create a new song
update: update an existing song
delete: delete a song
post: create a new song
widget: widget scope
scope2: scope too
scope3: another scope
paths:
/songs:
get:
security:
- song_auth: [read]
responses:
'200':
description: A list of songs.
post:
security:
- song_auth: [create]
- song_auth: [post, widget]
responses:
'201':
description: new song added
put:
security:
- song_auth: [update]
- song_auth: [put, widget]
responses:
'204':
description: song updated
delete:
security:
- song_auth: [delete]
- song_auth: [scope2, scope3]
responses:
'200':
description: song deleted