1
0
mirror of https://github.com/dkmstr/openuds.git synced 2024-12-23 17:34:17 +03:00

Adding support for oauth2 authenticator tests

This commit is contained in:
Adolfo Gómez García 2024-09-18 18:57:56 +02:00
parent ac72b88554
commit c6d9dac602
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
4 changed files with 103 additions and 1 deletions

View File

@ -0,0 +1,62 @@
#
# Copyright (c) 2024 Virtual Cable S.L.U.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import contextlib
import typing
from uds.core.environment import Environment
from uds.core.util import security
from uds.auths.OAuth2.authenticator import OAuth2Authenticator
PRIVATE_KEY, PUBLIC_KEY = security.generate_rsa_keypair()
DATA_TEMPLATE: dict[str, str] = {
'name': 'oauth2',
'authorization_endpoint': 'https://auth_endpoint.com',
'client_id': 'client_id',
'client_secret': 'client_secret',
'scope': 'openid email profile', # Default scopes
'common_groups': 'common_group',
'redirection_endpoint': 'https://redirect_endpoint.com',
'response_type': 'code',
'token_endpoint': 'https://oauth2.googleapis.com/token',
'info_endpoint': 'https://openidconnect.googleapis.com/v1/userinfo',
'public_key': PUBLIC_KEY,
'username_attr': 'username_attr',
'groupname_attr': 'groupname_attr',
'realname_attr': 'realname_attr',
}
ResponseTypes: typing.TypeAlias = typing.Literal['code', 'pkce', 'token', 'openid+token_id', 'openid+code']
@contextlib.contextmanager
def create_authenticator(response_type: ResponseTypes) -> typing.Iterator[OAuth2Authenticator]:
with Environment.temporary_environment() as env:
data = DATA_TEMPLATE.copy()
data['response_type'] = response_type
instance = OAuth2Authenticator(environment=env, values=data)
yield instance

View File

@ -0,0 +1,41 @@
# pylint: disable=no-member # ldap module gives errors to pylint
#
# Copyright (c) 2024 Virtual Cable S.L.U.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''
Author: Adolfo Gómez, dkmaster at dkmon dot com
'''
from tests.utils.test import UDSTestCase
from . import fixtures
class OAuthCodeFlowTest(UDSTestCase):
def test_auth(self) -> None:
with fixtures.create_authenticator('code') as oauth2:
self.assertIsInstance(oauth2, fixtures.OAuth2Authenticator)

View File

@ -1,4 +1,3 @@
# pylint: disable=no-member # ldap module gives errors to pylint
#
# Copyright (c) 2024 Virtual Cable S.L.U.
# All rights reserved.