Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
constructor(token: string) {
this.accessToken = token.replace('Bearer ', '');
try {
const tokenClaims = decode(this.accessToken);
// exp claim is in seconds, convert it se to milliseconds
this.expires = tokenClaims.exp * 1000;
} catch (err) {
this.expires = 0;
}
}
}
public static parseUserToken(token: string): IUser {
let user: IUser = { authenticated: false, cultureName: null, displayName: null, email: null, name: null, username: null, roles: [], verified: false };
if (token) {
let decodedToken: IJwtToken = jwtDecode(token);
if (!user.authenticated)
user.authenticated = true;
let name = decodedToken['http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name'];
let roles = decodedToken['http://schemas.microsoft.com/ws/2008/06/identity/claims/role'];
let sid = decodedToken['http://schemas.xmlsoap.org/ws/2005/05/identity/claims/sid'];
let timeZoneId = decodedToken['TimeZoneId'];
user.cultureName = decodedToken['CultureName'];
user.displayName = name ? name[1] : null;
user.email = decodedToken['http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress'] || null;
user.name = user.email;
user.roles = Array.isArray(roles) ? roles : [roles];
user.verified = decodedToken['Verified'] === 'true' ? true : false;
user.exp = new Date(decodedToken.exp * 1000);