Skip to content

Commit

Permalink
KNOX-2929 - Logged in user is shown on Knox UIs (#819)
Browse files Browse the repository at this point in the history
  • Loading branch information
smolnar82 authored Nov 16, 2023
1 parent c4f77c9 commit e888ec0
Show file tree
Hide file tree
Showing 15 changed files with 352 additions and 7 deletions.
7 changes: 5 additions & 2 deletions gateway-admin-ui/admin-ui/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {ResourceDetailComponent} from './resource-detail/resource-detail.compone
import {ProviderConfigSelectorComponent} from './provider-config-selector/provider-config-selector.component';
import {NewDescWizardComponent} from './new-desc-wizard/new-desc-wizard.component';
import {ProviderConfigWizardComponent} from './provider-config-wizard/provider-config-wizard.component';
import {SessionInformationComponent} from './sessionInformation/session.information.component';

@NgModule({
imports: [BrowserModule,
Expand Down Expand Up @@ -74,7 +75,8 @@ import {ProviderConfigWizardComponent} from './provider-config-wizard/provider-c
ResourceDetailComponent,
ProviderConfigSelectorComponent,
NewDescWizardComponent,
ProviderConfigWizardComponent
ProviderConfigWizardComponent,
SessionInformationComponent
],
providers: [TopologyService,
ServiceDefinitionService,
Expand All @@ -85,7 +87,8 @@ import {ProviderConfigWizardComponent} from './provider-config-wizard/provider-c
{provide: APP_BASE_HREF, useValue: '/'}
],
bootstrap: [AppComponent,
GatewayVersionComponent
GatewayVersionComponent,
SessionInformationComponent
]
})
export class AppModule {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<div style="text-align: right; color: rgb(130, 180, 93);">Logged in as {{ getUser() }}</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Component, OnInit} from '@angular/core';
import {SessionInformationService} from './session.information.service';
import {SessionInformation} from './session.information';

@Component({
selector: 'app-session-information',
templateUrl: './session.information.component.html',
providers: [SessionInformationService]
})

export class SessionInformationComponent implements OnInit {

sessionInformation: SessionInformation;
logoutSupported = true;

constructor(private sessionInformationService: SessionInformationService) {
this['showSessionInformation'] = true;
}

getUser() {
if (this.sessionInformation) {
return this.sessionInformation.user;
} else {
console.debug('SessionInformationComponent --> getUser() --> dr.who');
return 'dr.who';
}
}

ngOnInit(): void {
console.debug('SessionInformationComponent --> ngOnInit() --> ');
this.sessionInformationService.getSessionInformation()
.then(sessionInformation => this.setSessonInformation(sessionInformation));
}

setSessonInformation(sessionInformation: SessionInformation) {
this.sessionInformation = sessionInformation;
console.debug('SessionInformationComponent --> setSessonInformation() --> ' + this.sessionInformation.user);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Injectable} from '@angular/core';
import {HttpClient, HttpErrorResponse, HttpHeaders} from '@angular/common/http';
import Swal from 'sweetalert2';

import 'rxjs/add/operator/toPromise';
import {SessionInformation} from './session.information';

@Injectable()
export class SessionInformationService {
pathParts = window.location.pathname.split('/');
topologyContext = '/' + this.pathParts[1] + '/' + this.pathParts[2] + '/';
sessionUrl = this.topologyContext + 'session/api/v1/sessioninfo';

constructor(private http: HttpClient) {}

getSessionInformation(): Promise<SessionInformation> {
let headers = new HttpHeaders();
headers = this.addJsonHeaders(headers);
return this.http.get(this.sessionUrl, { headers: headers})
.toPromise()
.then(response => response['sessioninfo'] as SessionInformation)
.catch((err: HttpErrorResponse) => {
console.debug('HomepageService --> getSessionInformation() --> ' + this.sessionUrl + '\n error: ' + err.message);
if (err.status === 401) {
window.location.assign(document.location.pathname);
} else {
return this.handleError(err);
}
});
}

addJsonHeaders(headers: HttpHeaders): HttpHeaders {
return this.addCsrfHeaders(headers.append('Accept', 'application/json').append('Content-Type', 'application/json'));
}

addCsrfHeaders(headers: HttpHeaders): HttpHeaders {
return this.addXHRHeaders(headers.append('X-XSRF-Header', 'homepage'));
}

addXHRHeaders(headers: HttpHeaders): HttpHeaders {
return headers.append('X-Requested-With', 'XMLHttpRequest');
}

private handleError(error: HttpErrorResponse): Promise<any> {
Swal.fire({
icon: 'error',
title: 'Oops!',
text: 'Something went wrong!\n' + (error.error ? error.error : error.statusText),
confirmButtonColor: '#7cd1f9'
});
return Promise.reject(error.message || error);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export class SessionInformation {
user: string;
logoutUrl: string;
logoutPageUrl: string;
globalLgoutPageUrl: string;
}
1 change: 1 addition & 0 deletions gateway-admin-ui/admin-ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
style="max-width:200px; margin-top: -9px;"
src="assets/knox-logo-transparent.gif" alt="Apache Knox Manager"> </a>
</div>
<app-session-information></app-session-information>
</div>
</nav>

Expand Down
5 changes: 3 additions & 2 deletions knox-token-generation-ui/token-generation/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ import { HttpClientModule } from '@angular/common/http';
import { TokenGenerationComponent } from './token-generation.component';
import { ReactiveFormsModule } from '@angular/forms';
import { TokenGenService } from './token-generation.service';
import { SessionInformationComponent } from './session.information.component';

@NgModule({
imports: [BrowserModule,
HttpClientModule,
ReactiveFormsModule
],
declarations: [TokenGenerationComponent],
declarations: [TokenGenerationComponent, SessionInformationComponent],
providers: [TokenGenService],
bootstrap: [TokenGenerationComponent]
bootstrap: [TokenGenerationComponent, SessionInformationComponent]
})
export class AppModule {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- The text color is the same as the Cloudera logo's color -->
<div style="text-align: right; color: rgb(130, 180, 93);">Logged in as {{ getUser() }}</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Component, OnInit} from '@angular/core';
import {TokenGenService} from './token-generation.service';
import {SessionInformation} from './token-generation.models';

@Component({
selector: 'app-session-information',
templateUrl: './session.information.component.html',
providers: [TokenGenService]
})

export class SessionInformationComponent implements OnInit {

sessionInformation: SessionInformation;
logoutSupported = true;

constructor(private tokenGenerationService: TokenGenService) {
this['showSessionInformation'] = true;
}

getUser() {
if (this.sessionInformation) {
return this.sessionInformation.user;
} else {
console.debug('SessionInformationComponent --> getUser() --> dr.who');
return 'dr.who';
}
}

ngOnInit(): void {
console.debug('SessionInformationComponent --> ngOnInit() --> ');
this.tokenGenerationService.getSessionInformation()
.then(sessionInformation => this.setSessionInformation(sessionInformation));
}

private setSessionInformation(sessionInformation: SessionInformation) {
this.sessionInformation = sessionInformation;
console.debug('SessionInformationComponent --> setSessionInformation() --> ' + this.sessionInformation.user);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,12 @@ export interface TokenData {
lifespanHours: number;
lifespanMins: number;
}

export class SessionInformation {
user: string;
logoutUrl: string;
logoutPageUrl: string;
globalLgoutPageUrl: string;
canSeeAllTokens: boolean;
currentKnoxSsoCookieTokenId: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse, HttpHeaders, HttpParams } from '@angular/common/http';
import Swal from 'sweetalert2';
import { TokenData, TokenRequestParams, TokenResultData, TssStatusData } from './token-generation.models';
import { TokenData, TokenRequestParams, TokenResultData, TssStatusData, SessionInformation } from './token-generation.models';

@Injectable()
export class TokenGenService {
readonly baseURL: string;
readonly tokenURL: string;
readonly tssStatusRequestURL: string;
readonly sessionUrl: string;

constructor(private http: HttpClient) {
const knoxtokenURL = 'knoxtoken/api/v2/token';
Expand All @@ -35,6 +36,7 @@ export class TokenGenService {
this.baseURL = temporaryURL.substring(0, temporaryURL.lastIndexOf('/') + 1);
this.tokenURL = topologyContext + knoxtokenURL;
this.tssStatusRequestURL = topologyContext + tssStatusURL;
this.sessionUrl = topologyContext + 'session/api/v1/sessioninfo';
}

getTokenStateServiceStatus(): Promise<TssStatusData> {
Expand Down Expand Up @@ -70,6 +72,22 @@ export class TokenGenService {
});
}

getSessionInformation(): Promise<SessionInformation> {
let headers = new HttpHeaders();
headers = this.addHeaders(headers);
return this.http.get(this.sessionUrl, { headers: headers})
.toPromise()
.then(response => response['sessioninfo'] as SessionInformation)
.catch((err: HttpErrorResponse) => {
console.debug('TokenManagementService --> getSessionInformation() --> ' + this.sessionUrl + '\n error: ' + err.message);
if (err.status === 401) {
window.location.assign(document.location.pathname);
} else {
return this.handleError(err);
}
});
}

getGeneratedTokenData(params: TokenRequestParams): Promise<TokenResultData> {
let headers = new HttpHeaders();
headers = this.addHeaders(headers);
Expand Down
6 changes: 6 additions & 0 deletions knox-token-generation-ui/token-generation/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
</script>
</head>
<body class="login">
<div style="background: #222; padding-right: 15px; padding-left: 15px; margin-right: 1.75%; margin-left: 1.75%; position: relative; min-height: 110px; font-size:14px;">
<app-session-information></app-session-information>
<a class="navbar-brand" href="#"> <img style="max-width:200px;" src="assets/knox-logo-transparent.gif" alt="Apache Knox Home"></a>
</div>

<app-token-generation></app-token-generation>
</div>
</body>
</html>
5 changes: 3 additions & 2 deletions knox-token-management-ui/token-management/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {FormsModule, ReactiveFormsModule} from '@angular/forms';

import {TokenManagementComponent} from './token.management.component';
import {TokenManagementService} from './token.management.service';
import {SessionInformationComponent} from './session.information.component';

@NgModule({
imports: [BrowserModule,
Expand All @@ -52,9 +53,9 @@ import {TokenManagementService} from './token.management.service';
MatSlideToggleModule,
MatCheckboxModule
],
declarations: [TokenManagementComponent],
declarations: [TokenManagementComponent, SessionInformationComponent],
providers: [TokenManagementService],
bootstrap: [TokenManagementComponent]
bootstrap: [TokenManagementComponent, SessionInformationComponent]
})
export class AppModule {
}
Loading

0 comments on commit e888ec0

Please sign in to comment.