Skip to content

Commit

Permalink
Merge pull request #208 from Malte96/tud-add_authorization_header
Browse files Browse the repository at this point in the history
Add authorization field to fiware-header
  • Loading branch information
djs0109 authored Dec 7, 2023
2 parents f5f1d9f + 2f9f5d4 commit 5becdce
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#### v0.3.1
- add tutorial for protected endpoint with bearer authentication ([#208](https://github.com/RWTH-EBC/FiLiP/issues/208))

#### v0.3.0
- fixed inconsistency of `entity_type` as required argument ([#188](https://github.com/RWTH-EBC/FiLiP/issues/188))
- BREAKING CHANGE: Migration of pydantic v1 to v2 ([#199](https://github.com/RWTH-EBC/FiLiP/issues/199))
Expand Down
31 changes: 31 additions & 0 deletions examples/basics/e02_baerer_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
# Examples for initializing a contextbroker client with authorization via bearer
token.
- The authorization key is provided via the FiwareHeaderSecure class.
- The parameters should be provided via environment variables or a .env file.
"""

from filip.models.base import FiwareHeaderSecure
from filip.clients.ngsi_v2 import ContextBrokerClient

# ## Parameters
# Host address of Context Broker
CB_URL = "https://localhost:1026"
# FIWARE-Service
fiware_service = 'filip'
# FIWARE-Servicepath
fiware_service_path = '/example'
# FIWARE-Bearer token
# TODO it has to be replaced with the token of your protected endpoint
fiware_baerer_token = 'BAERER_TOKEN'

if __name__ == '__main__':
fiware_header = FiwareHeaderSecure(service=fiware_service,
service_path=fiware_service_path,
authorization=f"""Bearer {
fiware_baerer_token}""")
cb_client = ContextBrokerClient(url=CB_URL,
fiware_header=fiware_header)
# query entities from protected orion endpoint
entity_list = cb_client.get_entity_list()
print(entity_list)
15 changes: 15 additions & 0 deletions filip/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ class FiwareHeader(BaseModel):
validate_fiware_service_path)


class FiwareHeaderSecure(FiwareHeader):
"""
Defines entity service paths and a autorization via Baerer-Token which are supported by the NGSI
Context Brokers to support hierarchical scopes:
https://fiware-orion.readthedocs.io/en/master/user/service_path/index.html
"""
authorization: str = Field(
alias="authorization",
default="",
max_length=3000,
description="authorization key",
regex=r".*"
)


class LogLevel(str, Enum):
CRITICAL = 'CRITICAL'
ERROR = 'ERROR'
Expand Down

0 comments on commit 5becdce

Please sign in to comment.