-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/besked fordeler #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
cf679dc
Dump
ghbm-itk a16906b
Merge branch 'develop' into feature/besked-fordeler
ghbm-itk 56b763a
Fixed doc string
ghbm-itk 1b23aa1
Added SAML token cache
ghbm-itk 40f8ebd
Updated test of auth
ghbm-itk f892df7
Implemented message broker with pika
ghbm-itk b460142
Updated dependencies
ghbm-itk 378cfe4
Updated changelog
ghbm-itk 2727e8b
Updated readme
ghbm-itk cb71cbb
Lint
ghbm-itk 27bb71a
Updated readme
ghbm-itk 6ce377d
Added Token dataclass
ghbm-itk dd6d8f9
Added docstring
ghbm-itk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,87 @@ | ||
| """This module contains helper function to Kombit's Beskedfordeler SF1461. | ||
| https://digitaliseringskataloget.dk/integration/sf1461 | ||
| """ | ||
|
|
||
| import base64 | ||
| import ssl | ||
| from collections.abc import Generator | ||
|
|
||
| import pika | ||
| from pika.credentials import ExternalCredentials | ||
|
|
||
| from python_serviceplatformen.authentication import KombitAccess | ||
|
|
||
| PORT = 5671 | ||
| VIRTUAL_HOST = "BF" | ||
| PROD_HOST = "beskedfordeler.stoettesystemerne.dk" | ||
| TEST_HOST = "beskedfordeler.eksterntest-stoettesystemerne.dk" | ||
|
|
||
|
|
||
| class TokenCredentials(ExternalCredentials): | ||
| """A class for handling AMQP authorization using an | ||
| external token. | ||
| """ | ||
| def __init__(self, token: bytes): | ||
| super().__init__() | ||
| self.token = token | ||
|
|
||
| def response_for(self, start): | ||
| """Handle AMQP auth challenge.""" | ||
| return self.TYPE, self.token | ||
|
|
||
|
|
||
| def _setup_pika_params(kombit_access: KombitAccess) -> pika.ConnectionParameters: | ||
| """Setup parameters used by Pika to connect to the AMQP server. | ||
|
|
||
| Args: | ||
| kombit_access: The KombitAccess object used to authenticate. | ||
|
|
||
| Returns: | ||
| A pika.ConnectionsParameters object that can be used to connect. | ||
| """ | ||
| saml_token = kombit_access.get_saml_token("http://entityid.kombit.dk/service/bfo_modtag/2") | ||
| saml_decoded = base64.b64decode(saml_token) | ||
|
|
||
| if kombit_access.test: | ||
| host = TEST_HOST | ||
| else: | ||
| host = PROD_HOST | ||
|
|
||
| ssl_context = ssl.create_default_context() | ||
| ssl_options = pika.SSLOptions(context=ssl_context, server_hostname=host) | ||
| credentials = TokenCredentials(token=saml_decoded) | ||
|
|
||
| return pika.ConnectionParameters( | ||
| host=host, | ||
| port=PORT, | ||
| virtual_host=VIRTUAL_HOST, | ||
| ssl_options=ssl_options, | ||
| credentials=credentials | ||
| ) | ||
|
|
||
|
|
||
| def iterate_queue_messages(queue_id: str, kombit_access: KombitAccess, auto_acknowledge: bool = True) -> Generator[bytes]: | ||
| """Iterate over message in the given queue. | ||
| Messages are retrieved one at a time from the queue server. | ||
| If auto_acknowledge is true messages are removed from the queue server | ||
| and cannot be retrieved again. | ||
|
|
||
| Args: | ||
| queue_id: The id of the queue to get messages from. Most likely a UUID. | ||
| kombit_access: The KombitAccess object used for authentication. | ||
| auto_acknowledge: Whether to mark messages as read after receiving them. Defaults to True. | ||
|
|
||
| Yields: | ||
| The message body as bytes. | ||
| """ | ||
| params = _setup_pika_params(kombit_access) | ||
|
|
||
| with pika.BlockingConnection(parameters=params) as connection: | ||
| channel = connection.channel() | ||
|
|
||
| while True: | ||
| method_frame, header_frame, body = channel.basic_get(queue_id, auto_ack=auto_acknowledge) | ||
| if not any((method_frame, header_frame, body)): | ||
| return # Queue is empty | ||
|
|
||
| yield body |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.