generated from bcgov/EPIC.scaffold
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from dinesh-aot/EPICSYSTEM-227
/staff-users - POST, PATCH
- Loading branch information
Showing
11 changed files
with
239 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletions
10
compliance-api/src/compliance_api/services/auth_service.py
This file was deleted.
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
57
compliance-api/src/compliance_api/services/authorize_service/auth_service.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
"""Service to call epic.authorize endpoints.""" | ||
|
||
import requests | ||
from flask import current_app, g | ||
|
||
from compliance_api.exceptions import BusinessError | ||
from compliance_api.utils.enum import HttpMethod | ||
|
||
from .auth_user_schema import AuthUserSchema | ||
from .constant import API_REQUEST_TIMEOUT | ||
|
||
|
||
class AuthService: | ||
"""Handle service request for epic.authorize.""" | ||
|
||
@staticmethod | ||
def get_epic_user_by_id(auth_user_id: str): | ||
"""Return the user representation from epic.authorize.""" | ||
auth_user_response_json = _request_auth_service(f"/users/{auth_user_id}") | ||
return AuthUserSchema().load(auth_user_response_json) | ||
|
||
@staticmethod | ||
def update_user_group(auth_user_id: str, payload: dict): | ||
"""Update the group of the user in the identity server.""" | ||
update_group_response = _request_auth_service( | ||
f"/users/{auth_user_id}/group", HttpMethod.PATCH, payload | ||
) | ||
return update_group_response | ||
|
||
|
||
def _request_auth_service( | ||
self, relative_url, http_method: HttpMethod = HttpMethod.GET, data=None | ||
): | ||
"""REST Api call to authorize service.""" | ||
token = getattr(g, "access_token", None) | ||
if not token: | ||
raise BusinessError("No access token found", 401) | ||
auth_base_url = current_app.config["AUTH_BASE_URL"] | ||
headers = { | ||
"Content-Type": "application/json", | ||
"Authorization": f"Bearer {token}", | ||
} | ||
|
||
url = f"{auth_base_url}/{relative_url}" | ||
|
||
if http_method == HttpMethod.GET: | ||
response = requests.get(url, headers=headers, timeout=API_REQUEST_TIMEOUT) | ||
elif http_method == HttpMethod.PUT: | ||
response = requests.put( | ||
url, headers=headers, data=data, timeout=API_REQUEST_TIMEOUT | ||
) | ||
elif http_method == HttpMethod.DELETE: | ||
response = requests.delete(url, headers=headers, timeout=API_REQUEST_TIMEOUT) | ||
else: | ||
raise ValueError("Invalid HTTP method") | ||
response.raise_for_status() | ||
return response |
23 changes: 23 additions & 0 deletions
23
compliance-api/src/compliance_api/services/authorize_service/auth_user_schema.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Copyright © 2024 Province of British Columbia | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the 'License'); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an 'AS IS' BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Schema for user representation from epic.authorize.""" | ||
from marshmallow import Schema, fields | ||
|
||
|
||
class AuthUserSchema(Schema): | ||
"""Schema for the auth user.""" | ||
|
||
first_name = fields.Str(metadata={"description": "The first name of the user"}, required=True) | ||
last_name = fields.Str(metadata={"description": "The lastname of the user"}, required=True) | ||
id = fields.Str(metadata={"description": "The unique id of the user"}, required=True) |
3 changes: 3 additions & 0 deletions
3
compliance-api/src/compliance_api/services/authorize_service/constant.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"""Constant used for authorize service.""" | ||
|
||
API_REQUEST_TIMEOUT = 60 |
Oops, something went wrong.