mirror of
https://github.com/ansible/awx.git
synced 2026-05-08 01:47:35 -02:30
validate token scope
This commit is contained in:
@@ -1095,6 +1095,7 @@ class OAuth2TokenSerializer(BaseSerializer):
|
|||||||
|
|
||||||
refresh_token = serializers.SerializerMethodField()
|
refresh_token = serializers.SerializerMethodField()
|
||||||
token = serializers.SerializerMethodField()
|
token = serializers.SerializerMethodField()
|
||||||
|
ALLOWED_SCOPES = ['read', 'write']
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = OAuth2AccessToken
|
model = OAuth2AccessToken
|
||||||
@@ -1142,6 +1143,24 @@ class OAuth2TokenSerializer(BaseSerializer):
|
|||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
|
def _is_valid_scope(self, value):
|
||||||
|
if not value or (not isinstance(value, six.string_types)):
|
||||||
|
return False
|
||||||
|
words = value.split()
|
||||||
|
for word in words:
|
||||||
|
if words.count(word) > 1:
|
||||||
|
return False # do not allow duplicates
|
||||||
|
if word not in self.ALLOWED_SCOPES:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def validate_scope(self, value):
|
||||||
|
if not self._is_valid_scope(value):
|
||||||
|
raise serializers.ValidationError(_(
|
||||||
|
'Must be a simple space-separated string with allowed scopes {}.'
|
||||||
|
).format(self.ALLOWED_SCOPES))
|
||||||
|
return value
|
||||||
|
|
||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
validated_data['user'] = self.context['request'].user
|
validated_data['user'] = self.context['request'].user
|
||||||
validated_data['token'] = generate_token()
|
validated_data['token'] = generate_token()
|
||||||
|
|||||||
14
awx/main/tests/unit/api/serializers/test_token_serializer.py
Normal file
14
awx/main/tests/unit/api/serializers/test_token_serializer.py
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
from awx.api.serializers import OAuth2TokenSerializer
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('scope, expect', [
|
||||||
|
('', False),
|
||||||
|
('read', True),
|
||||||
|
('read read', False),
|
||||||
|
('write read', True),
|
||||||
|
('read rainbow', False)
|
||||||
|
])
|
||||||
|
def test_invalid_scopes(scope, expect):
|
||||||
|
assert OAuth2TokenSerializer()._is_valid_scope(scope) is expect
|
||||||
Reference in New Issue
Block a user