-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb07b18
commit dc9e77a
Showing
10 changed files
with
1,008 additions
and
669 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ omit = | |
indykite_sdk/utils/logger.py | ||
tests/* | ||
examples/* | ||
prepare_docs/* | ||
|
||
[report] | ||
exclude_lines = | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -25,3 +25,4 @@ ignore: | |
- "indykite_sdk/utils/logger.py" | ||
- "tests" | ||
- "examples" | ||
- "prepare_docs" |
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from indykite_sdk.indykite.identity.v1beta2 import identity_management_api_pb2 as pb2 | ||
from google.protobuf import struct_pb2 | ||
import sys | ||
import indykite_sdk.utils.logger as logger | ||
from indykite_sdk.indykite.identity.v1beta2 import attributes_pb2 as attributes | ||
from indykite_sdk.indykite.identity.v1beta2 import model_pb2 as model | ||
from indykite_sdk.indykite.identity.v1beta2 import import_pb2 | ||
from indykite_sdk.model.create_custom_login_token import CreateCustomLoginToken | ||
|
||
|
||
def create_custom_login_token(self, uid: any, token_claims: dict, session_claims: dict): | ||
""" | ||
CreateCustomLoginToken creates a signed custom authentication token with the specified user ID | ||
:param self: | ||
:param uid: DigitalTwin, PropertyFilter or CredentialReference | ||
:param token_claims: dictionary | ||
:param session_claims: dict | ||
:return: deserialized CreateCustomLoginTokenResponse | ||
""" | ||
sys.excepthook = logger.handle_excepthook | ||
try: | ||
token_struct = struct_pb2.Struct() | ||
if token_claims is not None: | ||
token_struct.update(token_claims) | ||
|
||
session_struct = struct_pb2.Struct() | ||
if session_claims is not None: | ||
session_struct.update(session_claims) | ||
|
||
if isinstance(uid, model.DigitalTwin): | ||
response = self.stub.CreateCustomLoginToken( | ||
pb2.CreateCustomLoginTokenRequest( | ||
digital_twin=uid, | ||
token_claims=token_struct, | ||
session_claims=session_struct, | ||
) | ||
) | ||
elif isinstance(uid, attributes.PropertyFilter): | ||
response = self.stub.CreateCustomLoginToken( | ||
pb2.CreateCustomLoginTokenRequest( | ||
property_filter=uid, | ||
token_claims=token_struct, | ||
session_claims=session_struct, | ||
) | ||
) | ||
elif isinstance(uid, import_pb2.CredentialReference): | ||
response = self.stub.CreateCustomLoginToken( | ||
pb2.CreateCustomLoginTokenRequest( | ||
credential_reference=uid, | ||
token_claims=token_struct, | ||
session_claims=session_struct, | ||
) | ||
) | ||
else: | ||
raise Exception("uid is not valid") | ||
return CreateCustomLoginToken.deserialize(response) if response else None | ||
|
||
except Exception as exception: | ||
return logger.logger_error(exception) | ||
|
||
if not response: | ||
return None |
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,17 @@ | ||
from indykite_sdk.model.digital_twin import DigitalTwinCore | ||
|
||
|
||
class CreateCustomLoginToken: | ||
@classmethod | ||
def deserialize(cls, message): | ||
if message is None: | ||
return None | ||
create_custom_login_token = CreateCustomLoginToken( | ||
str(message.token), | ||
DigitalTwinCore.deserialize(message.digital_twin) | ||
) | ||
return create_custom_login_token | ||
|
||
def __init__(self, token, digital_twin): | ||
self.token = token | ||
self.digital_twin = digital_twin |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from indykite_sdk.identity import IdentityClient | ||
from indykite_sdk.indykite.identity.v1beta2 import identity_management_api_pb2 as pb2 | ||
from indykite_sdk.indykite.identity.v1beta2 import model_pb2 as model | ||
from indykite_sdk.indykite.identity.v1beta2.import_pb2 import CredentialReference | ||
import os | ||
|
||
|
||
def test_create_custom_login_token_error(capsys): | ||
client = IdentityClient() | ||
assert client is not None | ||
digital_twin = model.DigitalTwin( | ||
id=str("ANYTHING"), | ||
tenant_id=str(os.getenv('ANYTHING')) | ||
) | ||
|
||
def mocked_create_custom_login_token_error(request: pb2.CreateCustomLoginTokenRequest): | ||
raise Exception("something went wrong") | ||
|
||
client.stub.CreateCustomLoginToken = mocked_create_custom_login_token_error | ||
|
||
response = client.create_custom_login_token(digital_twin, None, None) | ||
captured = capsys.readouterr() | ||
assert "something went wrong" in captured.err | ||
|
||
|
||
def test_create_custom_login_token(): | ||
digital_twin = model.DigitalTwin( | ||
id=str(os.getenv('DIGITAL_TWIN')), | ||
tenant_id=str(os.getenv('TENANT_ID')) | ||
) | ||
client = IdentityClient() | ||
assert client is not None | ||
|
||
def mocked_create_custom_login_token(request: pb2.CreateCustomLoginTokenRequest): | ||
assert request.digital_twin == digital_twin | ||
return pb2.CreateCustomLoginTokenResponse() | ||
|
||
client.stub.CreateCustomLoginToken = mocked_create_custom_login_token | ||
response = client.create_custom_login_token(digital_twin, {"t_claim": "test"}, {"s_claim": "test"}) | ||
|
||
assert response is not None | ||
|
||
|
||
def test_create_custom_login_token_credential(): | ||
credential_reference = CredentialReference( | ||
provider_id=str(os.getenv('OAUTH2_PROVIDER')), | ||
uid=str("unique_identifier") | ||
) | ||
client = IdentityClient() | ||
assert client is not None | ||
|
||
def mocked_create_custom_login_token(request: pb2.CreateCustomLoginTokenRequest): | ||
assert request.credential_reference == credential_reference | ||
return pb2.CreateCustomLoginTokenResponse() | ||
|
||
client.stub.CreateCustomLoginToken = mocked_create_custom_login_token | ||
response = client.create_custom_login_token(credential_reference, {"t_claim": "test"}, {"s_claim": "test"}) | ||
|
||
assert response is not None | ||
|
||
|
||
def test_create_custom_login_token_property(): | ||
client = IdentityClient() | ||
assert client is not None | ||
property_filter = client.property_filter("email", "testem@example.com", os.getenv('TENANT_ID')) | ||
|
||
def mocked_create_custom_login_token(request: pb2.CreateCustomLoginTokenRequest): | ||
assert request.property_filter == property_filter | ||
return pb2.CreateCustomLoginTokenResponse() | ||
|
||
client.stub.CreateCustomLoginToken = mocked_create_custom_login_token | ||
response = client.create_custom_login_token(property_filter, {"t_claim": "test"}, {"s_claim": "test"}) | ||
|
||
assert response is not None |