cancel
Showing results for 
Search instead for 
Did you mean: 

Getting Token using private key and certificate

aditya_gupta41
Contributor

Hello Experts,

I have a technical requirement as follows:

1. Get Token using information : authority, client_id, scope, thumbprint, endpoint, private key and certificate (.pem)

2. Pass token as flat in the next SOAP request.

My query is how can I achieve this via Snaplogic? They have provided me screenshots of cmd prompt where they used python, java and .NET in order to achieve this.

Thanks in Advance

4 REPLIES 4

bojanvelevski
Valued Contributor

Hey @aditya_gupta41,

Can you share more details about the authentication type, or maybe the chunk of code that generates the key?

Bojan

@bojanvelevski Below is the python files shared to us:

import sys
import json
import msal

from cryptography import x509
from cryptography.hazmat.primitives.serialization import load_pem_private_key

def print_result(result):
    if result['access_token']:
        print(result['access_token'])
    else:
        print(result['error'])
        print(result['error_description'])

def read_file(file_path):   
    return open(file_path, "rb").read()

# configuration file is passed via:
# py Azure\ Certificate\ Authentication.py Azure\ Certificate\ Authentication.json
config = json.load(open(sys.argv[1]))

# get private key from pfx file
private_key_path = config["private_key_path"]
certificate_path = config["certificate_path"]

private_key = load_pem_private_key(read_file(private_key_path), None)
certificate = x509.load_pem_x509_certificate(read_file(certificate_path))

# create application for access token request
app = msal.ConfidentialClientApplication(
    config["client_id"],
    authority=config["authority"],
    client_credential={
    "thumbprint": config["thumbprint"],
    "private_key": private_key}
    )

result = None

# request access token from cache
print("Request access token from cache.")
result = app.acquire_token_silent(config["scope"], account=None)

# request access token from aad
if not result:
    print("No access token in cache exists.\nRequest access token from AAD.")
    result = app.acquire_token_for_client(scopes=config["scope"])

print_result(result)

aditya_gupta41
Contributor

@bojanvelevski I can not share the files on this public post. Please check your Private Message. I have sent you on Monday.

bojanvelevski
Valued Contributor

Hi @aditya_gupta41,

I am not sure how can you replicate this into a snap. Have you tried the Script snap? The script snap supports Python, maybe you can replicate the code there and generate a token using a python script.