Skip to content

Commit d85309f

Browse files
committed
Add_security_Azure_AD_B2C
1 parent 9050d02 commit d85309f

31 files changed

+438
-46
lines changed

.github/workflows/CD-time-tracker-ui.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,26 @@ jobs:
2424
with:
2525
node-version: '12.x'
2626

27+
- name: Inject Secrets
28+
shell: python
29+
env:
30+
SCOPES: ${{ secrets.scopes }}
31+
CLIENT_ID: ${{ secrets.client_id }}
32+
AUTHORITY: ${{ secrets.authority }}
33+
BASE_PATH: "src/environments/"
34+
run: |
35+
import os
36+
import json
37+
data = {}
38+
base_path = os.environ.get('BASE_PATH', 'src/environments/')
39+
with open(base_path + "keys.example.json", "r+") as jsonFileRead:
40+
data = json.load(jsonFileRead)
41+
data["scopes"] = str(os.environ['SCOPES']).split(",")
42+
data["client_id"] = os.environ['CLIENT_ID']
43+
data["authority"] = os.environ['AUTHORITY']
44+
with open(base_path + ".keys.json", "w+") as jsonFileWrite:
45+
json.dump(data, jsonFileWrite)
46+
2747
- name: 'run: npm install and build'
2848
run: |
2949
npm install

.github/workflows/CI-time-tracker-ui.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,25 @@ jobs:
3939
- name: Install dependencies
4040
run: npm install
4141

42+
- name: Inject Secrets
43+
shell: python
44+
env:
45+
SCOPES: ${{ secrets.scopes }}
46+
CLIENT_ID: ${{ secrets.client_id }}
47+
AUTHORITY: ${{ secrets.authority }}
48+
BASE_PATH: "src/environments/"
49+
run: |
50+
import os
51+
import json
52+
data = {}
53+
base_path = os.environ.get('BASE_PATH', 'src/environments/')
54+
with open(base_path + "keys.example.json", "r+") as jsonFileRead:
55+
data = json.load(jsonFileRead)
56+
data["scopes"] = str(os.environ['SCOPES']).split(",")
57+
data["client_id"] = os.environ['CLIENT_ID']
58+
data["authority"] = os.environ['AUTHORITY']
59+
with open(base_path + ".keys.json", "w+") as jsonFileWrite:
60+
json.dump(data, jsonFileWrite)
61+
4262
- name: Run the test
4363
run: npm run ci-test --if-present

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ npm-debug.log
4040
yarn-error.log
4141
testem.log
4242
/typings
43-
43+
.keys.json
4444
# System Files
4545
.DS_Store
4646
Thumbs.db

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ Run `npm install` to install the required node_modules for this project.
2323
Run `ng serve` to run the app in dev mode. After executing this command, you can navigate to `http://localhost:4200/` to see the app working.
2424
The app will automatically reload if you change anything in the source files.
2525

26-
## Prepare your environment for vscode
26+
## Prepare your environment
2727

28+
### Set environment variables
29+
Create file .keys.json from keys.example.ts into environments folder
30+
31+
### Prepare your environment for vscode
2832
Install the following extensions:
2933

3034
- `EditorConfig for Visual Studio Code`.

package-lock.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"bootstrap": "^4.4.1",
2424
"jquery": "^3.4.1",
2525
"minimist": "^1.2.5",
26+
"msal": "^1.2.1",
2627
"rxjs": "~6.5.4",
2728
"tslib": "^1.10.0",
2829
"zone.js": "~0.10.2"

src/app/app-routing.module.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
import { NgModule } from '@angular/core';
22
import { Routes, RouterModule } from '@angular/router';
3+
4+
import { AzureGuardService } from './guards/azure-guard.service'
35
import { ReportsComponent } from './modules/reports/pages/reports.component';
46
import { TimeClockComponent } from './modules/time-clock/pages/time-clock.component';
57
import { TimeEntriesComponent } from './modules/time-entries/pages/time-entries.component';
68
import { ProjectManagementComponent } from './modules/project-management/pages/project-management.component';
79
import { ActivitiesManagementComponent } from './modules/activities-management/pages/activities-management.component';
10+
import { HomeComponent } from './modules/home/home.component';
11+
import { LoginComponent } from './modules/login/login.component';
812

913
const routes: Routes = [
10-
{path: 'reports', component: ReportsComponent},
11-
{path: 'time-clock', component: TimeClockComponent},
12-
{path: 'time-entries', component: TimeEntriesComponent},
13-
{path: 'project-management', component: ProjectManagementComponent},
14-
{path: 'activities-management', component: ActivitiesManagementComponent},
15-
{path: '', pathMatch: 'full', redirectTo: 'time-clock'},
16-
{path: '**', pathMatch: 'full', redirectTo: 'time-clock'},
14+
15+
{ path: '', component: HomeComponent, canActivate:[AzureGuardService],
16+
children: [
17+
{ path: 'reports', component: ReportsComponent },
18+
{ path: 'time-clock', component: TimeClockComponent },
19+
{ path: 'time-entries', component: TimeEntriesComponent },
20+
{ path: 'project-management', component: ProjectManagementComponent },
21+
{ path: 'activities-management', component: ActivitiesManagementComponent },
22+
{path: '', pathMatch: 'full', redirectTo: 'time-clock'},
23+
]
24+
},
25+
{ path: 'login', component: LoginComponent },
1726
];
1827

1928
@NgModule({

src/app/app.component.html

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
<app-navbar></app-navbar>
2-
3-
<app-sidebar>
4-
<router-outlet></router-outlet>
5-
</app-sidebar>
1+
<router-outlet></router-outlet>

src/app/app.module.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import { ActivitiesManagementComponent } from './modules/activities-management/p
2525
import { ActivityListComponent } from './modules/activities-management/components/activity-list/activity-list.component';
2626
import { CreateActivityComponent } from './modules/activities-management/components/create-activity/create-activity.component';
2727

28+
import { HomeComponent } from './modules/home/home.component';
29+
import { LoginComponent } from './modules/login/login.component';
2830

2931
@NgModule({
3032
declarations: [
@@ -47,7 +49,9 @@ import { CreateActivityComponent } from './modules/activities-management/compone
4749
GroupByDatePipe,
4850
ActivitiesManagementComponent,
4951
CreateActivityComponent,
50-
ActivityListComponent
52+
ActivityListComponent,
53+
HomeComponent,
54+
LoginComponent
5155
],
5256
imports: [
5357
CommonModule,
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { TestBed, inject } from '@angular/core/testing';
2+
import { RouterTestingModule } from '@angular/router/testing';
3+
import { Router } from '@angular/router'
4+
5+
import { AzureAdB2CService } from '../modules/login/services/azure.ad.b2c.service';
6+
import { AzureGuardService } from './azure-guard.service';
7+
8+
9+
describe('AzureGuardService', () => {
10+
let service: AzureGuardService;
11+
let azureAdB2CService: AzureAdB2CService;
12+
const azureAdB2CServiceStub = {
13+
isLogin() {
14+
return true;
15+
}
16+
};
17+
beforeEach(() => {
18+
TestBed.configureTestingModule({
19+
imports: [ RouterTestingModule ],
20+
providers: [
21+
{ providers: AzureAdB2CService, useValue: azureAdB2CServiceStub},
22+
]
23+
});
24+
service = TestBed.inject(AzureGuardService);
25+
azureAdB2CService = TestBed.inject(AzureAdB2CService);
26+
});
27+
28+
it('should be created', () => {
29+
expect(service).toBeTruthy();
30+
});
31+
32+
it('should check if is login', () => {
33+
spyOn(azureAdB2CService, 'isLogin').and.returnValue(true)
34+
const isAcitve = service.canActivate();
35+
expect(azureAdB2CService.isLogin).toHaveBeenCalled;
36+
expect(isAcitve).toEqual(true);
37+
});
38+
39+
it('should check if not is login', inject([Router], (router: Router) => {
40+
spyOn(azureAdB2CService, 'isLogin').and.returnValue(false)
41+
spyOn(router, 'navigate').and.stub();
42+
const isAcitve = service.canActivate();
43+
expect(azureAdB2CService.isLogin).toHaveBeenCalled;
44+
expect(isAcitve).toEqual(false);
45+
expect(router.navigate).toHaveBeenCalledWith(['login']);
46+
}));
47+
48+
});

0 commit comments

Comments
 (0)