Reputation: 621
I am using azure active directory OAuth for azure bot authentication.After logged in I got the token successfully, but how can I get the user details based on the token?
So, Is there is any way to parse the azure token in node.js and get the details about the user?
Upvotes: 3
Views: 2806
Reputation: 642
In my project I had a requirement to connect with azure to generate JWT token to validate user to connect with resources. It might require multiple purpose. please find the following to get JWT token for authentication.
public string getToken(){
string requestUri = "<API URL for token>";
string username = "<APP key>";
string password = "<APP secret>";
string encoded = System.Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(username + ":" + password));
//for azure token encoding will be Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password)
//for Basic auth encoding will be System.Text.Encoding.ASCII.GetBytes(username + ":" + password)
//Also you can try ASCIIEncoding.ASCII.GetBytes(username + ":" + password)
List<KeyValuePair<string, string>> Data = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("grant_type", "client_credentials"),
};
var content = new FormUrlEncodedContent(Data);
content.Headers.Clear();
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri);
requestMessage.Headers.Add("Authorization", "Basic " + encoded);
requestMessage.Content = content;
var response = await httpClient.SendAsync(requestMessage).ConfigureAwait(false);
string responJsonText = await response.Content.ReadAsStringAsync();
dynamic responsej = JsonConvert.DeserializeObject(responJsonText);
TokenId = responsej.access_token.ToString();
return TokenId;
}
Upvotes: 0
Reputation: 136196
Basically the token you get is a JWT token base64 encoded. You can use a node package like jwtDecode
to decode the contents of the token and get the claims.
You can do something like:
import jwtDecode from 'jwt-decode';
const claims = jwtDecode('base64 encoded token received from Azure AD');
claims
will be a JavaScript object.
Upvotes: 2