Skip to content

Commit

Permalink
Merge pull request #21 from christian-hawk/configuration_endpoint_for…
Browse files Browse the repository at this point in the history
…_client_id_secret

feat: configuration endpoint for client id and secret
  • Loading branch information
christian-hawk authored Sep 25, 2020
2 parents b8e461b + f84fe68 commit 36dc54b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 20 deletions.
27 changes: 19 additions & 8 deletions clientapp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
})
'''


def get_preselected_provider():
provider_id_string = cfg.PRE_SELECTED_PROVIDER_ID
provider_object = '{ "provider" : "%s" }' % provider_id_string
Expand Down Expand Up @@ -105,17 +106,22 @@ def index():

@app.route('/register', methods=['POST'])
def register():
app.logger.info('/register called')
content = request.json
app.logger.debug('data = %s' % content)
app.logger.info('Trying to register client %s on %s' %
(content['client_url'], content['op_url']))
status = 0
data = ''
if request.json is None:
if content is None:
status = 400
# message = 'No json data posted'
elif 'op_url' and 'client_url' not in request.json:
elif 'op_url' and 'client_url' not in content:
status = 400
# message = 'Not needed keys found in json'
else:
op_url = request.json['op_url']
client_url = request.json['client_url']
op_url = content['op_url']
client_url = content['client_url']

op_parsed_url = urlparse(op_url)
client_parsed_url = urlparse(client_url)
Expand All @@ -128,14 +134,13 @@ def register():

else:
client_handler = ClientHandler(
request.json['op_url'],
request.json['client_url']
content['op_url'],
content['client_url']
)
data = client_handler.get_client_dict()
status = 200
return jsonify(data), status


@app.route('/protected-content', methods=['GET'])
def protected_content():
app.logger.debug('/protected-content - cookies = %s' % request.cookies)
Expand Down Expand Up @@ -196,7 +201,7 @@ def callback():

@app.route("/configuration", methods=["POST"])
def configuration():
'''Receives client configuration via API'''
# Receives client configuration via API
app.logger.info('/configuration called')
content = request.json
app.logger.debug("content = %s" % content)
Expand All @@ -208,6 +213,12 @@ def configuration():
content['provider_id'])

return jsonify({"provider_id": content['provider_id']}), 200

if "client_id" in content and "client_secret" in content:
# Setup client_id and client_secret
oauth.op.client_id = content['client_id']
oauth.op.client_secret = content['client_secret']
return {}, 200
else:
return {}, 400

Expand Down
6 changes: 3 additions & 3 deletions clientapp/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CLIENT_ID = "6fffd04d-8cbb-4989-8e17-ece3d92ff7c5"
CLIENT_SECRET = "92f7c172-6d51-4159-98a5-58d9ead7c9e1"
CLIENT_ID = "a7e1125da-b164-4aa9-85e9-456f510c7eda"
CLIENT_SECRET = "a911acfa2-c4e2-4258-987c-af47864be058"
CLIENT_AUTH_URI = "https://t1.techno24x7.com/oxauth/restv1/authorize"
TOKEN_URI = "https://t1.techno24x7.com/oxauth/restv1/token"
USERINFO_URI = "https://t1.techno24x7.com/oxauth/restv1/userinfo"
Expand All @@ -20,7 +20,7 @@
# for gluu
ACR_VALUES = 'passport_saml'
PRE_SELECTED_PROVIDER = True
PRE_SELECTED_PROVIDER_ID = 'saml-default'
PRE_SELECTED_PROVIDER_ID = ''

# SYSTEM SETTINGS
# use with caution, unsecure requests, for develpment environments
Expand Down
52 changes: 46 additions & 6 deletions tests/unit_integration/test_configuration_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ def app_endpoints(app: Flask) -> List[str]:
return endpoints


def valid_client_configuration():
return {
"client_id": "my-client-id",
"client_secret": "my-client-secret",
"op_metadata_url": "https://op.com/.well-known/openidconfiguration"
}


class FlaskBaseTestCase(TestCase):
def setUp(self):
self.app = clientapp.create_app()
Expand Down Expand Up @@ -61,9 +69,9 @@ def test_endpoint_should_setup_cfg_with_provider_id(self):
headers = {'Content-type': 'application/json'}
data = {'provider_id': 'whatever'}
json_data = json.dumps(data)
response = self.client.post(url_for('configuration'),
data=json_data,
headers=headers)
self.client.post(url_for('configuration'),
data=json_data,
headers=headers)

self.assertEqual(clientapp.cfg.PRE_SELECTED_PROVIDER_ID, 'whatever')

Expand All @@ -72,8 +80,40 @@ def test_endpoint_should_setup_cfg_with_pre_selected_provider_true(self):
headers = {'Content-type': 'application/json'}
data = {'provider_id': 'whatever'}
json_data = json.dumps(data)
response = self.client.post(url_for('configuration'),
data=json_data,
headers=headers)
self.client.post(url_for('configuration'),
data=json_data,
headers=headers)

self.assertTrue(clientapp.cfg.PRE_SELECTED_PROVIDER, )

def test_endpoint_should_return_200_if_valid_client_config(self):
headers = {'Content-type': 'application/json'}
json_data = json.dumps(valid_client_configuration())
response = self.client.post(
url_for('configuration'), data=json_data, headers=headers)
self.assertEqual(response.status_code, 200,
'endpoint is NOT returning 200 for valid client configuration')

def test_endpoint_should_register_new_oauth_client_id(self):
headers = {'Content-type': 'application/json'}
client_id = "my-client-id"
client_secret = "my-client-secret"
op_metadata_url = "https://op.com/.well-known/openidconfiguration"
json_data = json.dumps({
"client_id": client_id,
"client_secret": client_secret,
"op_metadata_url": op_metadata_url
})
self.client.post(
url_for('configuration'), data=json_data, headers=headers)
self.assertTrue(clientapp.oauth.op.client_id == client_id,
'endpoint is NOT changing op.client_id')

def test_endpoint_should_register_new_oauth_client_secret(self):
headers = {'Content-type': 'application/json'}
json_data = json.dumps(valid_client_configuration())
client_secret = valid_client_configuration()['client_secret']
self.client.post(
url_for('configuration'), data=json_data, headers=headers)
self.assertTrue(clientapp.oauth.op.client_secret == client_secret,
'%s is is not %s' % (clientapp.oauth.op.client_secret, client_secret))
6 changes: 3 additions & 3 deletions tests/unit_integration/test_dynamic_client_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
ClientHandler = client_handler.ClientHandler


#helper
# helper
def get_class_instance(op_url='https://t1.techno24x7.com',
client_url='https://mock.test.com'):
client_handler_obj = ClientHandler(op_url, client_url)
Expand Down Expand Up @@ -83,8 +83,8 @@ def test_if_class_has_initial_expected_attrs(self):
'_ClientHandler__client_secret',
'_ClientHandler__client_url',
'_ClientHandler__metadata_url',
'discover', #method
'register_client' #method
'discover', # method
'register_client' # method
]

self.assertTrue(
Expand Down

0 comments on commit 36dc54b

Please sign in to comment.