Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 4fd9a8f

Browse files
committed
docs(cb-azure-ad-auth): Add new cookbook
1 parent eff7e04 commit 4fd9a8f

19 files changed

+428
-13
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference path='../_protractor/e2e.d.ts' />
2+
'use strict';
3+
describe('Azure AD Auth E2E tests', function () {
4+
5+
let expectedMsg = 'Simple app demonstrates';
6+
7+
beforeEach(function () {
8+
browser.get('');
9+
});
10+
11+
it(`should display: ${expectedMsg}`, function () {
12+
expect(element(by.css('p')).getText()).toContain(expectedMsg);
13+
});
14+
15+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
authSettings.config.ts
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Component } from '@angular/core';
2+
@Component({
3+
selector: 'app',
4+
template: `
5+
<a [routerLink]="['']">About</a> | <a [routerLink]="['login']">Login</a> | <a [routerLink]="['status']">Status</a> <br/>
6+
<router-outlet></router-outlet>`
7+
})
8+
9+
export class AppComponent { }
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { NgModule } from '@angular/core';
2+
import { HttpModule } from '@angular/http';
3+
import { BrowserModule } from '@angular/platform-browser';
4+
import { routing } from './app.routing';
5+
import { AppComponent } from './app.component';
6+
7+
import { HomeComponent } from './home/home.component';
8+
import { LoginComponent } from './login/login.component';
9+
import { StatusComponent } from './status/status.component';
10+
11+
import {AzureADServiceConstants} from './ngAuth/authenticators/AzureADServiceConstants';
12+
import {AzureADAuthService} from './ngAuth/authenticators/AzureADAuthService';
13+
import {AuthenticatedHttpService} from './ngAuth/AuthenticatedHttpService';
14+
15+
import {serviceConstants} from './authsettings.config';
16+
17+
let authenticator = new AzureADAuthService(serviceConstants);
18+
19+
@NgModule({
20+
providers: [
21+
AuthenticatedHttpService,
22+
{
23+
provide: AzureADAuthService,
24+
useValue: authenticator
25+
}],
26+
imports: [
27+
routing,
28+
BrowserModule,
29+
HttpModule
30+
],
31+
declarations: [
32+
AppComponent,
33+
HomeComponent,
34+
LoginComponent,
35+
StatusComponent
36+
],
37+
bootstrap: [AppComponent]
38+
})
39+
export class AppModule { }
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ModuleWithProviders } from '@angular/core';
2+
import { Routes, RouterModule } from '@angular/router';
3+
import { LoginComponent } from './login/login.component';
4+
import {HomeComponent} from './home/home.component';
5+
import { StatusComponent } from './status/status.component';
6+
7+
export const routes: Routes = [
8+
{ path: '', component: HomeComponent },
9+
{ path: 'login', component: LoginComponent },
10+
{ path: 'status', component: StatusComponent },
11+
];
12+
export const routing: ModuleWithProviders = RouterModule.forRoot(routes);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Component } from '@angular/core';
2+
import { Inject, Injectable } from '@angular/core';
3+
4+
@Component({
5+
template:'<p>Simple app demonstrates logging into AzureAD/Office365 and running a command against the MS graph</p>'
6+
})
7+
export class HomeComponent { }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Component, Inject } from '@angular/core';
2+
import { Router } from '@angular/router';
3+
4+
import {AzureADAuthService} from '../ngAuth/authenticators/AzureADAuthService';
5+
6+
@Component({
7+
template: `
8+
<button (click)="logIn()">
9+
Sign In
10+
</button>`
11+
})
12+
13+
export class LoginComponent {
14+
constructor(
15+
@Inject(AzureADAuthService) private _authService: AzureADAuthService,
16+
private _router: Router) { }
17+
18+
logIn() {
19+
this._authService.logIn("/");
20+
}
21+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
2+
import {AppModule} from './app.module';
3+
platformBrowserDynamic().bootstrapModule(AppModule);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Injectable, Inject } from '@angular/core';
2+
import { Http, Headers, Response } from '@angular/http';
3+
import { AzureADAuthService } from './authenticators/AzureADAuthService';
4+
import { Observable, Subscriber } from 'rxjs';
5+
6+
@Injectable()
7+
export class AuthenticatedHttpService {
8+
private _authenticator: AzureADAuthService;
9+
private _http: Http;
10+
constructor( @Inject(Http) http: Http, @Inject(AzureADAuthService) authenticator: AzureADAuthService) {
11+
this._authenticator = authenticator;
12+
this._http = http;
13+
}
14+
15+
createAuthorizationHeader(headers: Headers) {
16+
headers.append('Authorization', 'Bearer ' + this._authenticator.getAccessToken());
17+
}
18+
19+
get(url: string) {
20+
let headers = new Headers();
21+
this.createAuthorizationHeader(headers);
22+
return this._http.get(url, { headers: headers });
23+
}
24+
25+
post(url: string, data: any) {
26+
let headers = new Headers();
27+
this.createAuthorizationHeader(headers);
28+
return this._http.post(url, data, {
29+
headers: headers,
30+
});
31+
}
32+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Injectable } from '@angular/core';
2+
3+
@Injectable()
4+
export class JwtHelper {
5+
private urlBase64Decode(str: string) {
6+
var output = str.replace(/-/g, '+').replace(/_/g, '/');
7+
switch (output.length % 4) {
8+
case 0: { break; }
9+
case 2: { output += '=='; break; }
10+
case 3: { output += '='; break; }
11+
default: {
12+
throw 'Illegal base64url string!';
13+
}
14+
}
15+
return decodeURIComponent((<any>window).escape(window.atob(output)));
16+
}
17+
18+
public decodeToken(token: string = "") {
19+
if (token === null || token === "") return {"upn": ""};
20+
var parts = token.split('.');
21+
if (parts.length !== 3) {
22+
throw new Error('JWT must have 3 parts');
23+
}
24+
var decoded = this.urlBase64Decode(parts[1]);
25+
if (!decoded) {
26+
throw new Error('Cannot decode the token');
27+
}
28+
return JSON.parse(decoded);
29+
}
30+
}

0 commit comments

Comments
 (0)