Skip to content

Instantly share code, notes, and snippets.

@dtarnawsky
Created February 27, 2023 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtarnawsky/4f2498b48ed51be6d1725157a4fa4cc0 to your computer and use it in GitHub Desktop.
Save dtarnawsky/4f2498b48ed51be6d1725157a4fa4cc0 to your computer and use it in GitHub Desktop.
intune home page
import { Component, OnInit, SimpleChanges } from '@angular/core';
import { Router } from '@angular/router';
import {
IntuneMAM, IntuneMAMAcquireTokenSilentOptions, IntuneMAMAppConfig, IntuneMAMGroupName,
IntuneMAMPolicy, IntuneMAMUser, IntuneMAMVersionInfo
} from '@ionic-enterprise/intune';
import { BehaviorSubject } from 'rxjs';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage implements OnInit {
tokenInfo: any = null;
busy = false;
user$: BehaviorSubject<IntuneMAMUser | null> = new BehaviorSubject(null);
version: IntuneMAMVersionInfo | null = null;
groupName: IntuneMAMGroupName | null = null;
appConfig: IntuneMAMAppConfig | null = null;
policy: IntuneMAMPolicy | null = null;
user: IntuneMAMUser;
constructor(private router: Router) { }
ngOnInit() {
this.user$.subscribe(async user => {
console.log('User changed', user);
if (user?.upn) {
this.appConfig = await IntuneMAM.appConfig(user);
this.groupName = await IntuneMAM.groupName(user);
this.policy = await IntuneMAM.getPolicy(user);
await this.getToken();
}
this.user = user;
});
}
async ionViewDidEnter() {
this.version = await IntuneMAM.sdkVersion();
this.user$.next(await IntuneMAM.enrolledAccount());
IntuneMAM.addListener('appConfigChange', () => {
console.log('Policy change here');
});
IntuneMAM.addListener('policyChange', () => {
console.log('Policy change here');
});
}
async showConsole() {
await IntuneMAM.displayDiagnosticConsole();
}
async getToken() {
if (this.user$.value?.upn) {
try {
this.busy = true;
const opts: IntuneMAMAcquireTokenSilentOptions = {
scopes: ['https://graph.microsoft.com/.default'],
upn: this.user$.value?.upn
};
console.log('acquireTokenSilent for ' + opts.upn);
const tokenInfo = await IntuneMAM.acquireTokenSilent(opts);
this.tokenInfo = tokenInfo;
console.log('Got token info', tokenInfo);
const decoded: Token = this.decodeToken(tokenInfo.accessToken);
console.log('AccessToken',decoded);
let expiryDate = new Date(decoded.exp * 1000);
let nbfDate = new Date(decoded.nbf * 1000);
if (expiryDate < new Date()) {
alert('Microsoft returned an expired access token :(');
} else {
console.log(`Access token will expire in the future: ${expiryDate}`);
console.log(`Access token not before date ${nbfDate}`);
}
const decodedId: Token = this.decodeToken(tokenInfo.idToken);
console.log('IdToken',decodedId);
expiryDate = new Date(decoded.exp * 1000);
nbfDate = new Date(decoded.nbf * 1000);
if (expiryDate < new Date()) {
alert('Microsoft returned an expired id token :(');
} else {
console.log(`Id token will expire in the future: ${expiryDate}`);
console.log(`Id token not before date ${nbfDate}`);
}
} catch (error) {
alert(`Caught error from acquireTokenSilent ${error}`);
console.error(
'Unable to silently acquire token, getting interactive'
);
try {
const tokenInfo = await IntuneMAM.acquireToken({
scopes: ['https://graph.microsoft.com/.default'],
});
this.tokenInfo = tokenInfo;
} catch (error2) {
console.error('second error', error2);
}
} finally {
this.busy = false;
}
}
}
decodeToken(token: string): Token {
const base64Url: string = token.split('.')[1];
const base64: string = base64Url.replace(/-/g, '+').replace(/_/g, '/');
const jsonPayload: string = decodeURIComponent(atob(base64).split('').map((c) =>
'%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)).join(''));
return JSON.parse(jsonPayload);
};
async unEnroll() {
if (this.user$.value) {
try {
await IntuneMAM.deRegisterAndUnenrollAccount(this.user$.value);
} catch (err) {
alert(err);
}
} else {
alert('No user to logout');
}
this.router.navigate(['/login']);
}
getTokenInfoJson() {
return JSON.stringify((this.tokenInfo || {}), null, 2);
}
getPolicyJson() {
return JSON.stringify((this.policy || {}), null, 2);
}
getAppConfigJson() {
return JSON.stringify((this.appConfig || {}), null, 2);
}
}
interface Token {
aud: string;
iss: string;
iat: number;
nbf: number;
exp: number;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment