Skip to content

Commit

Permalink
JWT token improvement (#41)
Browse files Browse the repository at this point in the history
* Changed token

Signed-off-by: Paolo Salvatori <[email protected]>

* Fixed problem with JWT missing resource and expires_on

Signed-off-by: Paolo Salvatori <[email protected]>

---------

Signed-off-by: Paolo Salvatori <[email protected]>
Co-authored-by: Paolo Salvatori <[email protected]>
  • Loading branch information
meziantou and paolosalvatori authored Jun 4, 2024
1 parent a75a2bc commit 797b307
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@

// Can be consumed by ManagedIdentityCredential by specifying IDENTITY_ENDPOINT and IMDS_ENDPOINT environment variables to this action URL
// See https://github.com/Azure/azure-sdk-for-net/blob/Azure.Identity_1.8.0/sdk/identity/Azure.Identity/src/AzureArcManagedIdentitySource.cs
app.MapGet("/token", async (string resource) =>

app.MapGet("/token", async (HttpContext context, string resource) =>
{
var token = await tokenCredential.GetTokenAsync(new TokenRequestContext(new[] { resource }));
return new Dictionary<string, string> { ["access_token"] = token.Token, ["expires_on"] = token.ExpiresOn.ToString("O", CultureInfo.InvariantCulture) };
context.Response.Headers.Add("Content-Type", "application/json"); // Set the Content-Type header to JSON
return new Dictionary<string, string>
{
["access_token"] = token.Token,
["expiresOn"] = token.ExpiresOn.ToString("O", CultureInfo.InvariantCulture),
["expires_on"] = token.ExpiresOn.ToUnixTimeSeconds().ToString(),
["tokenType"] = "Bearer",
["resource"] = resource
};
});

app.Run();

0 comments on commit 797b307

Please sign in to comment.