Commit 73fc5d1f authored by sourabh_roy's avatar sourabh_roy
Browse files

updated the authentication code to include ibm login

parent c4e7dafc
Pipeline #30939 passed with stages
in 3 minutes and 20 seconds
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AuthGuard} from './auth.guard';
import { LoginComponent} from './login/login.component';
const routes: Routes = [
{ path: '', redirectTo: 'dashboard', pathMatch: 'full' },
// { path: 'login', loadChildren: () => import('./login/login.module').then(m => m.LoginModule)},
{ path: 'login', component: LoginComponent },
{ path: 'dashboard', loadChildren: () => import('./dasboard/dasboard.module').then(m => m.DasboardModule)},
{ path: 'entitlement', loadChildren: () => import ('./entitlements/entitlements.module').then(m =>m.EntitlementsModule)},
{ path: 'audit-metrics', loadChildren: () => import ('./audit-metrics/audit-metrics.module').then(m =>m.AuditMetricsModule)},
......
......@@ -17,7 +17,7 @@ import { AuthService } from './common/Authentication/auth.service';
import { appInitializer} from './common/app.initializer';
import { NgMultiSelectDropDownModule } from 'ng-multiselect-dropdown';
import { NgSelectModule } from '@ng-select/ng-select';
import { LoginComponent } from './login/login.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
export function initializeApp(appConfig: AppConfig) {
......@@ -30,7 +30,7 @@ export function initializeApp(appConfig: AppConfig) {
HeaderComponent,
FooterComponent,
SidenavComponent,
LoginComponent
],
imports: [
BrowserModule,BrowserAnimationsModule,
......
......@@ -208,14 +208,15 @@ export class AuthService {
}
else if(this.cloud_provider_name=="aws"){
window.location.href=`${this.logout_uri}?client_id=${this.client_id}&logout_uri=${this.redirect_uri}`
// window.location.href = `${this.auth_url}?response_type=code&client_id=${this.client_id}&redirect_uri=${this.redirect_uri}&scope=${this.scope}`;
}
else if(this.cloud_provider_name=="azure"){
window.location.href=`${this.logout_uri}${this.tennant_id}/oauth2/v2.0/logout`
// window.location.href = `${this.auth_url}?response_type=code&client_id=${this.client_id}&redirect_uri=${this.redirect_uri}&scope=${this.scope}`;
}
else if(this.cloud_provider_name=="ibm"){
this.router.navigate(['/login']);
window.location.href = `${this.logout_uri}?redirect_uri=${this.redirect_uri}`;
}
}
......@@ -224,7 +225,7 @@ export class AuthService {
let code;
let token;
token = this.getTokenSession();
if(this.cloud_provider_name != "ibm"){
// if(this.cloud_provider_name != "ibm"){
this.router.events.subscribe((e) => {
if (e instanceof NavigationEnd) {
......@@ -233,6 +234,12 @@ export class AuthService {
if(this.cloud_provider_name=="gcp"){
code= href.split("&")[0].split("=")[1];
}
else if(this.cloud_provider_name=="ibm"){
if(href.includes('&')){
code= href.split("&")[1].split("=")[1];
}
}
else{
code = href.split('=')[1];
}
......@@ -252,7 +259,8 @@ export class AuthService {
window.location.href=`${this.auth_url}${this.tennant_id}/oauth2/v2.0/authorize?response_type=code&client_id=${this.client_id}&redirect_uri=${this.redirect_uri}&scope=${this.scope}`
}
else if(this.cloud_provider_name=="ibm"){
this.router.navigate(['/login']);
window.location.href = `${this.auth_url}?response_type=code&prompt=consent&client_id=${this.client_id}&redirect_uri=${this.redirect_uri}&scope=${this.scope}`;
// this.router.navigate(['/login']);
}
}
......@@ -283,6 +291,9 @@ export class AuthService {
else if(this.cloud_provider_name=="aws" || this.cloud_provider_name=="azure"){
this.subscribeAuthToken(code);
}
else if(this.cloud_provider_name=="ibm"){
this.subscribeAuthToken_ibm(code);
}
// else if(this.cloud_provider_name=="ibm"){
// this.subscribeAuthToken_ibm(code);
// }
......@@ -310,26 +321,8 @@ export class AuthService {
}
}
});
}
else{
if(token==null){
this.router.navigate(['/login']);
}
else{
//this.router.navigate(['/dashboard']);
var checkExpired=this.checkGreaterThanOneDay(this.getrefreshTimer());
if(checkExpired){
let timer = this.refreshTokenTimer(this.getIdToken());
if (timer <= 0) {
this.subscribeRefreshToken();
}
}
else{
this.logout();
}
}
}
// }
}
......@@ -340,17 +333,7 @@ export class AuthService {
this.setIdToken(res.id_token);
this.refreshTokenTimer(res.id_token);
window.location.reload(true);
// this.getRefreshToken_gcp(this.getRefreshSession()).subscribe((result) => {
// this.setTokenSession(result.access_token);
// // this.setRefreshToken(result.refresh_token);
// this.setIdToken(result.id_token);
// this.refreshTokenTimer(result.id_token);
// }, (err) => {
// console.log(err);
// });
}
else if(this.cloud_provider_name=="aws"){
let res=await this.getRefreshToken(this.getRefreshSession()).toPromise();
......@@ -359,17 +342,7 @@ export class AuthService {
this.setIdToken(res.id_token);
this.refreshTokenTimer(res.id_token);
window.location.reload(true);
// this.getRefreshToken(this.getRefreshSession()).subscribe((result) => {
// this.setTokenSession(result.access_token);
// // this.setRefreshToken(result.refresh_token);
// this.setIdToken(result.id_token);
// this.refreshTokenTimer(result.id_token);
// }, (err) => {
// console.log(err);
// });
}
else if(this.cloud_provider_name=="ibm"){
let res=await this.getRefreshToken_ibm(this.getRefreshSession()).toPromise();
......@@ -415,8 +388,8 @@ export class AuthService {
});
}
subscribeAuthToken_ibm(username,password) {
this.getAuthToken_ibm(username,password).subscribe((result) => {
subscribeAuthToken_ibm(code) {
this.getAuthToken_ibm(code).subscribe((result) => {
this.setTokenSession(result.access_token);
this.setRefreshToken(result.refresh_token);
......@@ -448,6 +421,8 @@ export class AuthService {
);
}
getAuthToken_gcp(code){
this.param = '?code='+code+'&redirect_uri='+this.redirect_uri+'&client_id='+this.client_id+'&client_secret='+this.client_secret+'&grant_type=authorization_code';
......@@ -461,14 +436,15 @@ export class AuthService {
);
}
getAuthToken_ibm(username,password){
getAuthToken_ibm(code){
let body = new URLSearchParams();
body.set('grant_type', 'password');
body.set('grant_type', 'authorization_code');
body.set('code', code);
body.set('redirect_uri', this.redirect_uri);
body.set('client_id', this.client_id);
body.set('client_secret', this.client_secret);
body.set('username', username);
body.set('password', password);
body.set('scope', this.scope);
let errorData = {
'api': this.param,
}
......@@ -581,15 +557,19 @@ export class AuthService {
console.log("inside auth redirect");
if (code == undefined) {
if (token == null) {
if (this.cloud_provider_name == "gcp") {
if(this.cloud_provider_name=="gcp"){
window.location.href = `${this.auth_url}?response_type=code&prompt=consent&client_id=${this.client_id}&redirect_uri=${this.redirect_uri}&scope=${this.scope}&access_type=offline`;
}
else if (this.cloud_provider_name == 'azure') {
window.location.href = `${this.auth_url}${this.tennant_id}/oauth2/v2.0/authorize?response_type=code&client_id=${this.client_id}&redirect_uri=${this.redirect_uri}&scope=${this.scope}`
}
else {
else if(this.cloud_provider_name=="aws"){
window.location.href = `${this.auth_url}?response_type=code&client_id=${this.client_id}&redirect_uri=${this.redirect_uri}&scope=${this.scope}`;
}
else if(this.cloud_provider_name=='azure'){
window.location.href=`${this.auth_url}${this.tennant_id}/oauth2/v2.0/authorize?response_type=code&client_id=${this.client_id}&redirect_uri=${this.redirect_uri}&scope=${this.scope}`
}
else if(this.cloud_provider_name=="ibm"){
window.location.href = `${this.auth_url}?response_type=code&prompt=consent&client_id=${this.client_id}&redirect_uri=${this.redirect_uri}&scope=${this.scope}`;
}
}
}
}
......
......@@ -13,7 +13,7 @@ import { environment } from 'src/environments/environment';
selector: 'app-dashboard-main',
templateUrl: './dashboard-main.component.html',
styleUrls: ['./dashboard-main.component.css'],
encapsulation: ViewEncapsulation.None
// encapsulation: ViewEncapsulation.None
})
export class DashboardMainComponent implements OnInit {
......
aside#left-panel {
display: none;
}
footer.fadeIn2 {
display: none;
}
header#header {
display: none;
}
.container-login {
width: 100vw;
justify-content: center;
text-align: center;
max-width: 1600px;
margin-top: 6%;
position: fixed;
left: 0;
}
.txt_header{
/* background: #898989; */
min-height: 40px;
/* border-bottom: 1px solid grey; */
background: lightgray;
}
.submitButton-customizable {
font-size: 14px;
font-weight: bold;
margin: 20px 0px 10px 0px;
height: 40px;
width: 100%;
color: #fff;
background-color: #337ab7;
border-color: #2e6da4;
}
.contmargin{
text-align: center;
justify-content: center;
}
.text-center {
text-align: center!important;
font-size: 23px;
color: #575656;
}
p {
color: #535353;
}
.cont_box{
background: white;
width: 23%;
box-shadow: 2px 2px 20px 1px grey;
}
.login-navbar-header{
width: 100%;
position: fixed;
top: 0;
left: 0;
right: 0;
/* padding-left: 1%; */
background: white;
/* margin-left: -23px; */
padding: 0.5%;
}
<ngx-spinner bdColor = "rgba(0, 0, 0, 0.4)" size = "medium" color = "#fff" type = "ball-scale-multiple" ><p style="color: white" > Loading... </p></ngx-spinner>
<div class="login-navbar-header">
<a class="login-navbar-brand" href="#"><img src="../../assets/images/osdu_logo.png" width="160" alt="Logo"></a>
</div>
<div class="container-login">
<div class="row contmargin">
<div class="cont_box">
<div class="txt_header">
<!-- <h2 class="text-center">Sign In</h2> -->
<!-- <p class="otpPara">Please check your mail and enter the otp below</p> -->
</div>
<p>Sign in with your username and password</p>
<div class="panel panel-default" style="padding: 3%;">
<div class="panel-body" >
<div class="text-center">
<div class="panel-body">
<form #myForm id="myForm" role="form" autocomplete="off" class="form" method="post" style="border:0px solid;">
<div class="form-group">
<div class="input-group" style=" box-shadow: 0 1px 4px 0px rgba(0, 0, 0, 0.12);">
<span style="width: 14%;" class="input-group-addon"><i class="fa fa-envelope-o" aria-hidden="true"></i></span>
<input [(ngModel)]="selectEmail" id="email" name="email" placeholder="Username" class="form-control" type="text" >
</div>
<div class="input-group" style="margin-top: 5%; box-shadow: 0 1px 4px 0px rgba(0, 0, 0, 0.12);">
<span style="width: 14%;" class="input-group-addon"><i class="fa fa-lock" aria-hidden="true"></i></span>
<input [(ngModel)]="selectpassword" id="Password" name="Password" placeholder="Password" class="form-control" type="password">
</div>
<!-- <div *ngIf="isSuccess" class="success-msg">
Password has been changed successfully.Please <a routerLink="/" class="orangeColor">Click here</a> to login.
</div> -->
</div>
<div style="text-align: center;">
<p *ngIf="isError" style="color:red"> {{errorMessge}}</p>
</div>
<div class="form-group">
<!-- <button type="submit" name="recover-submit" (click)=onSubmit() class="btn btn-lg btn-primary btn-block" [disabled]="!validityCheck">Reset Password</button> -->
<input name="recover-submit" (click)="onSubmit()" style="cursor: pointer;" class="submitButton-customizable" value="Sign In" type="button">
</div>
<input type="hidden" class="hide" name="token" id="token" value="">
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { LoginComponent } from './login.component';
describe('LoginComponent', () => {
let component: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ LoginComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(LoginComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
import { Component, ElementRef, OnInit, ViewChild, ViewEncapsulation } from '@angular/core';
import { FormGroup, FormControl, FormBuilder, Validators,FormsModule,ReactiveFormsModule } from '@angular/forms';
import { Router } from '@angular/router';
import { NgxSpinnerService } from 'ngx-spinner';
import { AuthService } from '../common/Authentication/auth.service';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css'],
encapsulation: ViewEncapsulation.None
})
export class LoginComponent implements OnInit {
// passwordForm: FormGroup;
selectEmail:string='';
selectpassword:string='';
errorMessge:string='';
validityCheck:boolean=false;
isError:boolean=false;
@ViewChild('myForm', { static: true }) myForm : ElementRef;
error_messages = {
'email': [
{ type: 'required', message: 'email is required.' },
],
'Password': [
{ type: 'required', message: 'password is required.' },
],
}
constructor(private auth:AuthService,public router: Router, private spinner: NgxSpinnerService,) {
}
ngOnInit(): void {
}
onSubmit(){
this.spinner.show();
var email=this.myForm["nativeElement"].email.value;
var pass=this.myForm["nativeElement"].Password.value;
if(email !="" && pass !=""){
this.auth.getAuthToken_ibm(email,pass).subscribe((result) => {
this.auth.setTokenSession(result.access_token);
this.auth.setRefreshToken(result.refresh_token);
this.auth.setIdToken(result.id_token);
this.auth.refreshTokenTimer(result.id_token);
this.auth.setrefreshTimer(result.id_token);
//let refreshtimer=this.checkGreaterThanOneDay(result.id_token);
// this.router.navigate(['/dashboard']);
this.spinner.hide();
this.router.navigate(['/dashboard'])
.then(() => {
window.location.reload();
});
}, (err) => {
this.spinner.hide();
this.isError=true;
this.errorMessge=err;
console.log(err);
});
}
else{
this.spinner.hide();
this.isError=true;
this.errorMessge="Please fill all the fields."
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment