Forum Discussion

pc2's avatar
pc2
Helper I
7 years ago

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

  • 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-connectors/)

     

    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

    • pc2's avatar
      pc2
      Helper I

      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?

      • d_gosbell's avatar
        d_gosbell
        Super User

        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.