Skip to content

Commit e63e1af

Browse files
committed
fix: #76 save in local storage tenantId and access token
1 parent 485e4c1 commit e63e1af

File tree

4 files changed

+85
-39
lines changed

4 files changed

+85
-39
lines changed

src/app/modules/login/services/azure.ad.b2c.service.spec.ts

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ import { UserAgentApplication, Account } from 'msal';
55
describe('AzureAdB2CService', () => {
66
let service: AzureAdB2CService;
77

8+
const account: Account = {
9+
accountIdentifier: 'abc',
10+
homeAccountIdentifier: 'abc',
11+
userName: 'abc',
12+
name: 'abc',
13+
idToken: {
14+
iss: ' http://hostname.com/12345/v0/',
15+
},
16+
idTokenClaims: {},
17+
sid: 'abc',
18+
environment: 'abc',
19+
};
20+
821
beforeEach(() => {
922
TestBed.configureTestingModule({
1023
imports: [],
@@ -33,16 +46,6 @@ describe('AzureAdB2CService', () => {
3346
});
3447

3548
it('should get Account name from UserAgentApplication', () => {
36-
const account: Account = {
37-
accountIdentifier: 'abc',
38-
homeAccountIdentifier: 'abc',
39-
userName: 'abc',
40-
name: 'abc',
41-
idToken: {},
42-
idTokenClaims: {},
43-
sid: 'abc',
44-
environment: 'abc',
45-
};
4649
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValues(account);
4750

4851
const name = service.getName();
@@ -52,16 +55,6 @@ describe('AzureAdB2CService', () => {
5255
});
5356

5457
it('isLogin returns true if UserAgentApplication has a defined Account', () => {
55-
const account: Account = {
56-
accountIdentifier: 'abc',
57-
homeAccountIdentifier: 'abc',
58-
userName: 'abc',
59-
name: 'abc',
60-
idToken: {},
61-
idTokenClaims: {},
62-
sid: 'abc',
63-
environment: 'abc',
64-
};
6558
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(account);
6659

6760
const isLogin = service.isLogin();
@@ -76,4 +69,45 @@ describe('AzureAdB2CService', () => {
7669
expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
7770
expect(isLogin).toEqual(false);
7871
});
72+
73+
it('setTenantId should save a tenantId in session storage', () => {
74+
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(account);
75+
spyOn(sessionStorage, 'setItem').withArgs('tenant_id', '12345');
76+
77+
const isLogin = service.isLogin();
78+
service.setTenantId();
79+
80+
expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
81+
expect(isLogin).toEqual(true);
82+
expect(sessionStorage.setItem).toHaveBeenCalledWith('tenant_id', '12345');
83+
});
84+
85+
it('setTenantId should not save tenantId if login is false ', () => {
86+
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(null);
87+
spyOn(sessionStorage, 'setItem');
88+
const isLogin = service.isLogin();
89+
expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
90+
expect(isLogin).toEqual(false);
91+
expect(sessionStorage.setItem).not.toHaveBeenCalled();
92+
});
93+
94+
it('getTenantId should get the tenantId from session storage', () => {
95+
const tenantId = '12345';
96+
spyOn(sessionStorage, 'getItem').and.returnValue(tenantId);
97+
98+
const resp = service.getTenantId();
99+
100+
expect(sessionStorage.getItem).toHaveBeenCalled();
101+
expect(resp).toEqual(tenantId);
102+
});
103+
104+
it('getBearerToken should get the bearer token from session storage', () => {
105+
const token = '12345abc';
106+
spyOn(sessionStorage, 'getItem').and.returnValue(token);
107+
108+
const resp = service.getBearerToken();
109+
110+
expect(sessionStorage.getItem).toHaveBeenCalled();
111+
expect(resp).toEqual(token);
112+
});
79113
});

src/app/modules/login/services/azure.ad.b2c.service.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,19 @@ import { CLIENT_ID, AUTHORITY, SCOPES } from '../../../../environments/environme
44
import { UserAgentApplication } from 'msal';
55

66
@Injectable({
7-
providedIn: 'root'
7+
providedIn: 'root',
88
})
9-
109
export class AzureAdB2CService {
11-
1210
msalConfig = {
1311
auth: {
1412
clientId: CLIENT_ID,
1513
authority: AUTHORITY,
16-
validateAuthority: false
17-
}
14+
validateAuthority: false,
15+
},
1816
};
1917

2018
tokenRequest = {
21-
scopes: SCOPES
19+
scopes: SCOPES,
2220
};
2321

2422
msal = new UserAgentApplication(this.msalConfig);
@@ -38,4 +36,19 @@ export class AzureAdB2CService {
3836
isLogin() {
3937
return this.msal.getAccount() ? true : false;
4038
}
39+
40+
setTenantId() {
41+
if (this.msal.getAccount() && this.msal.getAccount().idToken) {
42+
const pathArray = this.msal.getAccount().idToken.iss.split('/');
43+
const tenantId = pathArray[3];
44+
sessionStorage.setItem('tenant_id', tenantId);
45+
}
46+
}
47+
48+
getTenantId(): string {
49+
return sessionStorage.getItem('tenant_id');
50+
}
51+
getBearerToken(): string {
52+
return sessionStorage.getItem('msal.idtoken');
53+
}
4154
}

src/app/modules/shared/components/user/user.component.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@ describe('UserComponent', () => {
1414
},
1515
signIn() {
1616
return of();
17-
}
17+
},
1818
};
1919

2020
beforeEach(async(() => {
2121
TestBed.configureTestingModule({
22-
declarations: [ UserComponent ],
23-
providers: [
24-
{ providers: AzureAdB2CService, useValue: azureAdB2CServiceStub}
25-
]
26-
})
27-
.compileComponents();
22+
declarations: [UserComponent],
23+
providers: [{ providers: AzureAdB2CService, useValue: azureAdB2CServiceStub }],
24+
}).compileComponents();
2825
}));
2926

3027
beforeEach(() => {
@@ -38,20 +35,24 @@ describe('UserComponent', () => {
3835
expect(component).toBeTruthy();
3936
});
4037

41-
it('onInit checks if isLogin and gets the name', () => {
38+
it('onInit checks if isLogin and gets the name and set tenantIn in the storage', () => {
4239
spyOn(azureAdB2CService, 'isLogin').and.returnValue(true);
4340
spyOn(azureAdB2CService, 'getName').and.returnValue('Name');
41+
spyOn(azureAdB2CService, 'setTenantId');
4442
component.ngOnInit();
4543
expect(azureAdB2CService.isLogin).toHaveBeenCalled();
4644
expect(azureAdB2CService.getName).toHaveBeenCalled();
45+
expect(azureAdB2CService.setTenantId).toHaveBeenCalled();
4746
});
4847

4948
it('onInit does not get the name if isLogin false', () => {
5049
spyOn(azureAdB2CService, 'isLogin').and.returnValue(false);
5150
spyOn(azureAdB2CService, 'getName').and.returnValue('Name');
51+
spyOn(azureAdB2CService, 'setTenantId');
5252
component.ngOnInit();
5353
expect(azureAdB2CService.isLogin).toHaveBeenCalled();
5454
expect(azureAdB2CService.getName).toHaveBeenCalledTimes(0);
55+
expect(azureAdB2CService.setTenantId).not.toHaveBeenCalled();
5556
});
5657

5758
it('uses the Azure service on logout', () => {
@@ -61,5 +62,4 @@ describe('UserComponent', () => {
6162

6263
expect(azureAdB2CService.logout).toHaveBeenCalled();
6364
});
64-
6565
});

src/app/modules/shared/components/user/user.component.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,21 @@ import { AzureAdB2CService } from '../../../login/services/azure.ad.b2c.service'
44
@Component({
55
selector: 'app-user',
66
templateUrl: './user.component.html',
7-
styleUrls: ['./user.component.scss']
7+
styleUrls: ['./user.component.scss'],
88
})
99
export class UserComponent implements OnInit {
10-
1110
name: string;
1211

13-
constructor(private azureAdB2CService: AzureAdB2CService) { }
12+
constructor(private azureAdB2CService: AzureAdB2CService) {}
1413

1514
ngOnInit(): void {
1615
if (this.azureAdB2CService.isLogin()) {
1716
this.name = this.azureAdB2CService.getName();
17+
this.azureAdB2CService.setTenantId();
1818
}
1919
}
2020

2121
logout() {
2222
this.azureAdB2CService.logout();
2323
}
24-
2524
}

0 commit comments

Comments
 (0)