Skip to content

Commit

Permalink
feat: add enrich token method
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkelim committed May 4, 2022
1 parent 03c8042 commit 1298a5e
Show file tree
Hide file tree
Showing 13 changed files with 441 additions and 178 deletions.
22 changes: 19 additions & 3 deletions jarvis_sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,23 @@ optional arguments:
-h, --help show this help message and exit
```
12. To see all available options, run
12. Enrich token
Sends out a enrich token request with the specified active token (login token).
```shell
usage: api.py enrich-token [-h] user_token --token_claims key=value --session_claims key=value
positional arguments:
user_token JWT bearer token
optional arguments:
--token_claims token claims to be added
--session_claims session claims to be added
-h, --help show this help message and exit
```
13. To see all available options, run
```shell
python3 api.py --help
Expand All @@ -300,13 +316,13 @@ optional arguments:
-l, --local make the request to localhost
```
11. To see the subcommands help page, run
14. To see the subcommands help page, run
```shell
python3 api.py <sub_command> --help
```
12. To execute the functions against the local instance, add the `-l` flag to the command:
15. To execute the functions against the local instance, add the `-l` flag to the command:
```shell
python api.py -l introspect USER_TOKEN
Expand Down
31 changes: 29 additions & 2 deletions jarvis_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
from jarvis_sdk.cmd import IdentityClient


class ParseKwargs(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, dict())
for value in values:
key, value = value.split('=')
getattr(namespace, self.dest)[key] = value


def main():
# Create parent parser
parser = argparse.ArgumentParser(description="Identity client API.")
Expand Down Expand Up @@ -92,6 +100,14 @@ def main():
del_dt_by_token = subparsers.add_parser("del-dt-by-token")
del_dt_by_token.add_argument("user_token", help="JWT bearer token")

# ENRICH-TOKEN
enrich_token = subparsers.add_parser("enrich-token")
enrich_token.add_argument("user_token", help="JWT bearer token")
enrich_token.add_argument("--token_claims", nargs='*',
help="Token claims to add (--token_claims key=value)", action=ParseKwargs)
enrich_token.add_argument("--session_claims", nargs='*',
help="Session claims to add (--session_claims key=value)", action=ParseKwargs)

args = parser.parse_args()

local = args.local
Expand Down Expand Up @@ -213,6 +229,16 @@ def main():
if dt is not None:
print_response(dt)

elif command == "enrich-token":
user_token = args.user_token
token_claims = args.token_claims
session_claims = args.session_claims
response = client.enrich_token(user_token, token_claims, session_claims)
if response is not None:
print("Successfully enriched token")
else:
print("Invalid token")


def print_verify_info(digital_twin_info):
print("Digital twin info")
Expand Down Expand Up @@ -247,9 +273,10 @@ def prettify(js):
elif isinstance(v, type(list())):
for val in v:
if isinstance(val, type(str())):
js[k] = format_convert(k, v)
val = format_convert(k, val)
pass
elif isinstance(val, type(list())):
elif isinstance(val, type(list())) | isinstance(val, type(float())) | isinstance(val, type(
bool())) | isinstance(val, type(None)):
pass
else:
prettify(val)
Expand Down
1 change: 1 addition & 0 deletions jarvis_sdk/cmd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@ def __init__(self, local=False):
from ._delete import \
del_digital_twin, \
del_digital_twin_by_token
from ._enrich_token import enrich_token
28 changes: 28 additions & 0 deletions jarvis_sdk/cmd/_enrich_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from jarvis_sdk.indykite.identity.v1beta1 import identity_management_api_pb2 as pb2
from google.protobuf import struct_pb2

def enrich_token(self, user_token: str, token_claims: dict, session_claims: dict):
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)

response = self.stub.EnrichToken(
pb2.EnrichTokenRequest(
access_token=user_token,
token_claims=token_struct,
session_claims=session_struct,
)
)
except Exception as exception:
print(exception)
return None

if not response:
return None

return response
21 changes: 21 additions & 0 deletions jarvis_sdk/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,24 @@ def digital_twin_by_token():
if (property.object_value and property.object_value.string_value):
print("Value: " + property.object_value.string_value)
print()

def enrich_token():
token = "JWT TOKEN"
claims = {
"string_claim": "string_value",
"number_claim": 42,
"bool_claim": True,
"null_claim": None,
"map_claim": {
"key": "value",
},
"array_claim": [
"string_value",
]
}
client = IdentityClient()
response = client.enrich_token(token, claims, claims)
if response is not None:
print("Successfully enriched token")
else:
print("Invalid token")
50 changes: 26 additions & 24 deletions jarvis_sdk/indykite/identity/v1beta1/attributes_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1298a5e

Please sign in to comment.