Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it compatible JWT Server and application REST requests #157

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Make it compatible JWT Server and application REST requests
  • Loading branch information
SelimEmre committed Jan 21, 2022
commit 0b0935b28e3b0f15fae3cd3f205f91e4a061efa5
3 changes: 2 additions & 1 deletion src/app/app.page/app.definitions.ts
Original file line number Diff line number Diff line change
@@ -166,7 +166,8 @@ export class ServerSettings {
constructor(public serverName: string,
public licenceKey: string,
public buildForMarket: boolean,
public logLevel: string
public logLevel: string,
public jwtServerControlEnabled: boolean
) {}
}

5 changes: 3 additions & 2 deletions src/app/app.page/app.page.component.ts
Original file line number Diff line number Diff line change
@@ -408,6 +408,7 @@ export class AppPageComponent implements OnInit, OnDestroy, AfterViewInit {
}

getInitParams() {
let currentServerJwtStatus = localStorage.getItem('serverJWTControlEnabled');

this.sub = this.route.params.subscribe(params => {
//this method is called whenever app changes
@@ -442,7 +443,8 @@ export class AppPageComponent implements OnInit, OnDestroy, AfterViewInit {

this.restService.isAdmin().subscribe(data => {
console.log(data);
if (data["success"] == true) {
// If JWT Server token is enable then no need to check admin status
if(data["success"] == true || currentServerJwtStatus){
this.admin_check = true;
}
else {
@@ -676,7 +678,6 @@ export class AppPageComponent implements OnInit, OnDestroy, AfterViewInit {
}

this.dataSource = new MatTableDataSource(this.broadcastTableData.dataRows);
console.log(this.dataSource)
this.cdr.detectChanges();

});
23 changes: 18 additions & 5 deletions src/app/pages/login/login.component.html
Original file line number Diff line number Diff line change
@@ -43,23 +43,36 @@ <h3 class="card-title" i18n="@@loginTitle">Login</h3>
You can now login with your username and password
</div>

<div class="form-group">
<div *ngIf="serverSettings.jwtServerControlEnabled" class="form-group">
<label i18n="@@loginFormEmail">JWT Token</label>
<input (keydown)="JWTTokenChanged()" type="text" name="jwtToken" placeholder="JWT Token" class="form-control input-no-border" [(ngModel)]="serverJWTToken">
</div>

<div *ngIf="!serverSettings.jwtServerControlEnabled" class="form-group">
<label i18n="@@loginFormEmail">Username</label>
<input (keydown)="credentialsChanged()" type="email" name="email" i18n-placeholder="@@email_place_holder" placeholder="Username" class="form-control input-no-border" [(ngModel)]="email">

</div>
<div class="form-group">
<div *ngIf="!serverSettings.jwtServerControlEnabled" class="form-group">
<label i18n="@@loginFormPassword">Password</label>
<input (keydown)="credentialsChanged()" type="password" name="password" i18n-placeholder="@@password_place_holder" placeholder="Password" class="form-control input-no-border" [(ngModel)]="password">
</div>
<div class="form-group text-danger text-center" [hidden]="!showIncorrectCredentials"
i18n="@@loginFormIncorrectCredentials" >
<div *ngIf="!serverSettings.jwtServerControlEnabled" class="form-group text-danger text-center" [hidden]="!showIncorrectCredentials"
i18n="@@loginFormIncorrectCredentials" >
Username or password is incorrect
</div>
<div *ngIf="serverSettings.jwtServerControlEnabled" class="form-group text-danger text-center" [hidden]="!showIncorrectJWTToken">
JWT Token is incorrect
</div>
<div class="form-group text-danger text-center" *ngIf="blockLoginAttempt"
>
You have 3 invalid login attempts. Please try again to login after 5 minutes
</div>
<div class="checkbox">
<input [(ngModel)]="serverSettings.jwtServerControlEnabled" name="jwtTokenEnabled" id="jwtTokenEnabled" type="checkbox">
<label for="jwtTokenEnabled">
Login with JWT Server Token
</label>
</div>
</div>
<div class="card-footer text-center">
<button type="submit" i18n="@@loginFormSubmit" class="btn btn-fill btn-wd ">Let's go</button>
66 changes: 49 additions & 17 deletions src/app/pages/login/login.component.ts
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ import {Router} from '@angular/router';
import {AuthService} from '../../rest/auth.service';
import {User} from '../../rest/rest.service';
import {RestService} from '../../rest/rest.service';
import {ServerSettings} from "../../app.page/app.definitions";

declare var $:any;

@@ -20,13 +21,16 @@ export class LoginComponent implements OnInit{
public email = "";
public password = "";
public showIncorrectCredentials = false;
public showIncorrectJWTToken = false;
public blockLoginAttempt = false;
public firstLogin = false;
public firstUser: User;
public temp_model_password:string;
public firstUserIsCreating:boolean;
public showYouCanLogin:boolean;
public showFailedToCreateUserAccount:boolean;
public serverSettings: ServerSettings;
public serverJWTToken: string;

constructor(private element : ElementRef, private auth: AuthService, private router: Router,private restService: RestService) {
this.nativeElement = element.nativeElement;
@@ -53,6 +57,8 @@ export class LoginComponent implements OnInit{
}
});

this.serverSettings = new ServerSettings(null,null, false, "INFO",false);

this.auth.licenceWarningDisplay = true;

this.checkFullPageBackgroundImage();
@@ -94,24 +100,46 @@ export class LoginComponent implements OnInit{

loginUser() {

this.auth.login(this.email, this.password).subscribe(data =>{

if (data["success"] == true) {
this.auth.isAuthenticated = data["success"];
localStorage.setItem("authenticated", "true");
localStorage.setItem(LOCAL_STORAGE_EMAIL_KEY, this.email);
this.router.navigateByUrl("/dashboard");
}
else {
this.showIncorrectCredentials = true;
}
if(!this.serverSettings.jwtServerControlEnabled){
localStorage.clear();
}

});

this.restService.getBlockedStatus(this.email).subscribe(data => {
this.blockLoginAttempt = data["success"];
});

if(this.serverSettings.jwtServerControlEnabled) {
//We need to define this value in this line
//server JWT tokens needs to be define before rest request
localStorage.setItem('serverJWTControlEnabled', "true");
localStorage.setItem('serverJWTToken', this.serverJWTToken);
this.restService.getApplications().subscribe(data =>{
if ( data['applications'].length > 0) {
localStorage.setItem("authenticated", "true");
this.auth.isAuthenticated = true;
this.router.navigateByUrl("/dashboard");
}
else{
this.showIncorrectJWTToken = true;
}
},
error =>{
this.showIncorrectJWTToken = true;
});
}
else{
this.auth.login(this.email, this.password).subscribe(data =>{
if (data["success"] == true) {
this.auth.isAuthenticated = data["success"];
localStorage.setItem("authenticated", "true");
localStorage.setItem(LOCAL_STORAGE_EMAIL_KEY, this.email);
this.router.navigateByUrl("/dashboard");
}
else {
this.showIncorrectCredentials = true;
}
});

this.restService.getBlockedStatus(this.email).subscribe(data => {
this.blockLoginAttempt = data["success"];
});
}
}


@@ -139,6 +167,10 @@ export class LoginComponent implements OnInit{
this.showIncorrectCredentials = false;
}

JWTTokenChanged():void {
this.showIncorrectJWTToken = false;
}

}

export const LOCAL_STORAGE_EMAIL_KEY = "email";
23 changes: 18 additions & 5 deletions src/app/rest/auth.service.ts
Original file line number Diff line number Diff line change
@@ -37,6 +37,8 @@ export class AuthService implements CanActivate {

constructor(private restService: RestService, private router: Router, private datePipe: DatePipe) {

this.serverSettings = new ServerSettings(null,null, false, "INFO",true);

setInterval(() => {
this.checkServerIsAuthenticated();

@@ -95,12 +97,11 @@ export class AuthService implements CanActivate {

checkServerIsAuthenticated(): void {

if (localStorage.getItem('authenticated')) {
if (localStorage.getItem('authenticated') && !this.serverSettings.jwtServerControlEnabled) {
this.restService.isAuthenticated().subscribe(data => {

this.isAuthenticated = data["success"];

if (!this.isAuthenticated) {
if (!this.isAuthenticated ) {
console.debug("Not authenticated navigating to login ");
this.router.navigateByUrl('/pages/login');
}
@@ -113,6 +114,13 @@ export class AuthService implements CanActivate {
this.router.navigateByUrl('/pages/login');
});
}
else if(localStorage.getItem('authenticated') && this.isAuthenticated && this.serverSettings.jwtServerControlEnabled ){
this.isAuthenticated = true;

if(this.router.url=="/pages/login"){
this.router.navigateByUrl('/dashboard/overview');
}
}
else{
this.isAuthenticated = false;
}
@@ -122,7 +130,7 @@ export class AuthService implements CanActivate {
console.debug("AuthService: is authenticated: " + this.isAuthenticated
+ " local storage: " + localStorage.getItem('authenticated'));

if (localStorage.getItem('authenticated') && this.isAuthenticated) {
if (localStorage.getItem('authenticated') && this.isAuthenticated && !this.serverSettings.jwtServerControlEnabled) {

this.restService.isAuthenticated().subscribe(data => {

@@ -141,7 +149,12 @@ export class AuthService implements CanActivate {
});
return true;
}
else {
else if(localStorage.getItem('authenticated') && this.isAuthenticated && this.serverSettings.jwtServerControlEnabled ){

this.isAuthenticated = true;
return true;
}
else{
console.debug("AuthService navigating login")
this.router.navigateByUrl('/pages/login');
this.isAuthenticated = false;
12 changes: 10 additions & 2 deletions src/app/rest/rest.service.ts
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@ import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/toPromise';
import {HttpClient, HttpEvent, HttpHandler, HttpHeaders, HttpInterceptor, HttpRequest} from '@angular/common/http';
import {Endpoint,PlaylistItem} from "../app.page/app.definitions";
import { filter } from 'rxjs-compat/operator/filter';
import {SidebarComponent} from "../sidebar/sidebar.component";

declare function require(name: string);
@@ -78,13 +77,22 @@ export class AuthInterceptor implements HttpInterceptor{
let currentAppJwtToken = localStorage.getItem(appName+'jwtToken');
let currentAppJwtStatus = localStorage.getItem(appName+'jwtControlEnabled');

let currentServerJwtToken = localStorage.getItem('serverJWTToken');
let currentServerJwtStatus = localStorage.getItem('serverJWTControlEnabled');

// Check AppName, JWT Token status and JWT Token not null
if(appName != null && currentAppJwtToken != null && currentAppJwtStatus != "false"){
if(appName != null && currentAppJwtToken != null && currentAppJwtStatus == "true"){
req = req.clone({
withCredentials: true,
headers: req.headers.append('Authorization', currentAppJwtToken)
});
}
else if(currentServerJwtToken != null || currentServerJwtStatus == "true"){
req = req.clone({
withCredentials: true,
headers: req.headers.append('Authorization', currentServerJwtToken)
});
}
else {
req = req.clone({
withCredentials: true
7 changes: 4 additions & 3 deletions src/app/server.settings/server.settings.component.ts
Original file line number Diff line number Diff line change
@@ -14,7 +14,6 @@ import {RestService, User} from "../rest/rest.service";
import {ActivatedRoute, Router} from "@angular/router";
import {HttpClient} from "@angular/common/http";
import {DataService} from "../rest/data.service";
import {MatPaginator, MatPaginatorIntl, PageEvent} from "@angular/material/paginator"
import {MatTableDataSource} from "@angular/material/table"
import {MatSort} from "@angular/material/sort"
import {UserEditComponent} from './dialog/user.edit.dialog.component';
@@ -113,7 +112,7 @@ export class ServerSettingsComponent implements OnDestroy, OnInit, AfterViewIni
}

ngOnInit(){
this.serverSettings = new ServerSettings(null,null, false, this.logLevelInfo);
this.serverSettings = new ServerSettings(null,null, false, this.logLevelInfo,false);

this.callTimer();

@@ -124,13 +123,15 @@ export class ServerSettingsComponent implements OnDestroy, OnInit, AfterViewIni
}

ngAfterViewInit() {
let currentServerJwtStatus = localStorage.getItem('serverJWTControlEnabled');
this.restService.isEnterpriseEdition().subscribe(data => {
this.isEnterpriseEdition = data["success"];
this.getServerSettings();
});
this.restService.isAdmin().subscribe(data => {
console.log(data);
if(data["success"] == true){
// If JWT Server token is enable then no need to check admin status
if(data["success"] == true || currentServerJwtStatus){
this.admin_check = true;
}
else{