Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
pc2
Helper I
Helper I

M codes for JWT token

Hi,

 

I've successfully wrote M codes for API connections on several occasions, but I now have a new authentication method, which I haven't dealt with previously.  It is JWT (Json Web Token) and I need to write M codes to get JWT token, then use the JWT received to request for Access Token.  

I'm trying to figure out how to do the first part, getting JWT.

The 3rd party vendor (www.SwipeClock.com) has provided a sample code in Javascript, but I cannot replicate this in Power Query (Power BI).

 

Below is Javascript sample the vendor provided on their API Documentation:

<script src=http://kjur.github.io/ijsrasign/jsrsaign-latest-all-min.js”></script>
<script type=”text/javascript”>
	let header = {alg: “HS256”, type: “JWT”};

	let token = {
		iss: 69481,
    		exp: 1556729729,
 		 product: "twpclient",
  siteinfo: {
  			  type: "id",
  site: 69481
  				}
			}

	Let jwt = KJUR.jws.JWS.sign(“HS256”, JSON.stringify(header), JSON.stringify(token), “xxxxxxxSecretCodexxxxx”);

</script>

And here's my attempt to write it in Power BI (note, exp is Epoch time):

let

 GetJson = Json.Document(Web.Contents("https://clock.payrollservers.us/AuthenticationService/oauth2/usertoken",

   [
     Headers = [#"alg"= "HS256", #"typ"="JWT"],
 
    Content = [

#"iss"= 69481,
#"exp"= Number.ToText(Duration.TotalSeconds(DateTime.FixedLocalNow()-#datetime(1970,1,1,0,0,0)) + 60*5),
#"product"= "twpclient",

#"siteInfo"= [#"type"="id",#"site"= 69481]

    ],
    
    Query=   Json.FromValue(Headers)  &"." & Json.FromValue(Content)  &","& "xxxxxSecretCodexxxxxxxxx"
    
    ]
 
  ))

in
GetJson

Any help would be much appreciated!

 

 

 

3 REPLIES 3
d_gosbell
Super User
Super User

I don't think you can do this in straight M, I think you'd need to build a custom connector. You actually do this using M, but there are some additional functions available (see https://blog.crossjoin.co.uk/2017/11/06/which-m-functions-are-only-available-to-custom-data-connecto...)

 

You can currently missing the signing logic from the last line of the javascript example 

Let jwt = KJUR.jws.JWS.sign(“HS256”, ...

And that "HS256" requires SHA256 encoding which is one of the M functions only accessible from a custom connector

Yikes!  Custom Data Connector is way out of my league.  I'll have to hire someone to do this.

Any suggestion where I can find someone for Custom Data Connector for Power BI?  Upwork.com?


@pc2 wrote:

Any suggestion where I can find someone for Custom Data Connector for Power BI?  Upwork.com?


No sorry, I'm not sure where you'd find someone for this.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.