From f29f2e7af31cf770cdaaac86a5138c211e2b77b7 Mon Sep 17 00:00:00 2001 From: Nikolaos Laskaris Date: Tue, 17 Oct 2017 11:17:24 +0300 Subject: [PATCH] minor changes --- dmp-admin/src/app/app.module.ts | 1 - .../datasets-viewer.component.html | 6 +- .../services/login/native-login.service.ts | 2 +- .../src/app/services/login/token.service.ts | 19 ++++- dmp-admin/src/app/services/rest-base.ts | 10 ++- dmp-admin/src/index.html | 1 + dmp-frontend/package-lock.json | 5 ++ dmp-frontend/package.json | 1 + dmp-frontend/src/app/app.module.ts | 3 +- dmp-frontend/src/app/login/login-page.ts | 27 ++++++- .../src/app/services/server.service.ts | 2 + .../src/app/services/token.service.ts | 77 +++++++++++++++++++ 12 files changed, 137 insertions(+), 17 deletions(-) create mode 100644 dmp-frontend/src/app/services/token.service.ts diff --git a/dmp-admin/src/app/app.module.ts b/dmp-admin/src/app/app.module.ts index c05b1c55e..2dc57045a 100644 --- a/dmp-admin/src/app/app.module.ts +++ b/dmp-admin/src/app/app.module.ts @@ -11,7 +11,6 @@ import { ReactiveFormsModule } from '@angular/forms'; import { ContextMenuModule } from 'ngx-contextmenu'; import { Ng2Webstorage } from 'ngx-webstorage'; -import { AuthInterceptor } from './services/interceptors/auth-interceptor'; import { RestBase } from './services/rest-base'; import { TokenService } from './services/login/token.service'; import { NativeLoginService } from './services/login/native-login.service'; diff --git a/dmp-admin/src/app/datasets-viewer/datasets-viewer.component.html b/dmp-admin/src/app/datasets-viewer/datasets-viewer.component.html index 6f8b452e9..4859817e1 100644 --- a/dmp-admin/src/app/datasets-viewer/datasets-viewer.component.html +++ b/dmp-admin/src/app/datasets-viewer/datasets-viewer.component.html @@ -41,15 +41,15 @@ - {{dataset.id}} + {{dataset?.id}} diff --git a/dmp-admin/src/app/services/login/native-login.service.ts b/dmp-admin/src/app/services/login/native-login.service.ts index ca5fb25d5..34f7cf6a3 100644 --- a/dmp-admin/src/app/services/login/native-login.service.ts +++ b/dmp-admin/src/app/services/login/native-login.service.ts @@ -18,7 +18,7 @@ export class NativeLoginService implements OnInit { login(username, password){ var credentials = {"username": username, "password":password}; - return this.restBase.post("project/create", credentials); + return this.restBase.post("nativeLogin", credentials); } diff --git a/dmp-admin/src/app/services/login/token.service.ts b/dmp-admin/src/app/services/login/token.service.ts index 8259095b5..5b2bd7897 100644 --- a/dmp-admin/src/app/services/login/token.service.ts +++ b/dmp-admin/src/app/services/login/token.service.ts @@ -1,16 +1,29 @@ -import { Injectable, } from '@angular/core'; +import { Injectable, Inject} from '@angular/core'; import { Observable } from 'rxjs'; import {LocalStorageService} from 'ngx-webstorage'; import 'rxjs/add/operator/map'; +import { DOCUMENT } from '@angular/platform-browser'; +declare var jQuery : any; @Injectable() export class TokenService { - constructor (private storage : LocalStorageService) { - + constructor (private storage : LocalStorageService, @Inject(DOCUMENT) private document) { + var csrfToken : string = jQuery(document).find('meta[name="csrf-token"]').attr('content'); + this.setCSRFToken(csrfToken); } + + + getCSRFToken() : string{ + return this.storage.retrieve('csrf-token'); + } + + setCSRFToken(csrfToken : string){ + this.storage.store('csrf-token',csrfToken); + } + getProvider() : TokenProvider { return this.storage.retrieve('provider'); diff --git a/dmp-admin/src/app/services/rest-base.ts b/dmp-admin/src/app/services/rest-base.ts index 28ba71483..3d24a03ec 100644 --- a/dmp-admin/src/app/services/rest-base.ts +++ b/dmp-admin/src/app/services/rest-base.ts @@ -41,12 +41,14 @@ export class RestBase { private createOptions(){ var token = this.tokenService.getToken(); var provider: TokenProvider = this.tokenService.getProvider(); + var csrfToken : string = this.tokenService.getCSRFToken(); + const params = new HttpParams(); - var headers; + var headers; if(provider == TokenProvider.google) - headers = new HttpHeaders().set('Content-Type', 'application/json').set("google-token", token); - if(provider == TokenProvider.google) - headers = new HttpHeaders().set('Content-Type', 'application/json').set("google-token", token); + headers = new HttpHeaders().set('Content-Type', 'application/json').set('X-CSRF-Token', csrfToken).set("google-token", token); + if(provider == TokenProvider.native) + headers = new HttpHeaders().set('Content-Type', 'application/json').set('X-CSRF-Token', csrfToken).set("native-token", token); let options = { params: params, headers: headers }; return options; } diff --git a/dmp-admin/src/index.html b/dmp-admin/src/index.html index 791666a35..7f54dfa42 100644 --- a/dmp-admin/src/index.html +++ b/dmp-admin/src/index.html @@ -4,6 +4,7 @@ DmpAdmin + diff --git a/dmp-frontend/package-lock.json b/dmp-frontend/package-lock.json index c050a5ace..69989fb2b 100644 --- a/dmp-frontend/package-lock.json +++ b/dmp-frontend/package-lock.json @@ -5366,6 +5366,11 @@ "integrity": "sha1-KzJxhOiZIQEXeyhWP7XnECrNDKk=", "dev": true }, + "ngx-webstorage": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/ngx-webstorage/-/ngx-webstorage-1.8.0.tgz", + "integrity": "sha1-zQhbmPvHpyzI8qqkDGyASzjUVt4=" + }, "no-case": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/no-case/-/no-case-2.3.1.tgz", diff --git a/dmp-frontend/package.json b/dmp-frontend/package.json index a2eb501cf..68d9e8075 100644 --- a/dmp-frontend/package.json +++ b/dmp-frontend/package.json @@ -25,6 +25,7 @@ "bootstrap": "^3.3.7", "core-js": "^2.4.1", "jquery": "^3.2.1", + "ngx-webstorage": "^1.8.0", "rxjs": "^5.4.2", "zone.js": "^0.8.17" }, diff --git a/dmp-frontend/src/app/app.module.ts b/dmp-frontend/src/app/app.module.ts index 1c0deaccf..d8db18a98 100644 --- a/dmp-frontend/src/app/app.module.ts +++ b/dmp-frontend/src/app/app.module.ts @@ -9,6 +9,7 @@ import { AppComponent } from './app.component'; import { DynamicFormComponent } from './form/dynamic-form.component'; import { DynamicFormFieldComponent } from './form/fields/dynamic-form-field.component'; import { ServerService } from './services/server.service'; +import { TokenService, TokenProvider } from './services/token.service'; import { dataModelBuilder } from './services/dataModelBuilder.service'; import { DynamicFormGroupComponent } from './form/dynamic-form-group/dynamic-form-group.component'; import { LoginComponent } from './login/login-page'; @@ -41,7 +42,7 @@ import { PaginationService } from './services/pagination.service'; AppRoutingModule ], - providers: [ServerService, dataModelBuilder, AuthGuard, PaginationService], + providers: [ServerService, dataModelBuilder, AuthGuard, PaginationService, TokenService], bootstrap: [AppComponent] }) export class AppModule { diff --git a/dmp-frontend/src/app/login/login-page.ts b/dmp-frontend/src/app/login/login-page.ts index a72782b5e..5b65ae980 100644 --- a/dmp-frontend/src/app/login/login-page.ts +++ b/dmp-frontend/src/app/login/login-page.ts @@ -1,6 +1,7 @@ import { Component, OnInit, NgZone } from '@angular/core'; import {GoogleSignInSuccess} from 'angular-google-signin'; import { Router, ActivatedRoute } from '@angular/router'; +import { TokenService, TokenProvider } from '../services/token.service'; @Component({ selector: 'login-page', @@ -9,12 +10,14 @@ import { Router, ActivatedRoute } from '@angular/router'; }) export class LoginComponent implements OnInit{ returnUrl: string; - public static token : string; + constructor( private route: ActivatedRoute, private router: Router, - private ngZone: NgZone){ + private ngZone: NgZone, + private tokenService : TokenService + ){ } @@ -31,13 +34,29 @@ export class LoginComponent implements OnInit{ let googleUser: gapi.auth2.GoogleUser = event.googleUser; let id: string = googleUser.getId(); let profile: gapi.auth2.BasicProfile = googleUser.getBasicProfile(); - LoginComponent.token = googleUser.getAuthResponse().id_token; + this.tokenService.setToken(googleUser.getAuthResponse().id_token); + console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead. console.log('Name: ' + profile.getName()); - console.log('token: ' + LoginComponent.token); + console.log('token: ' + this.tokenService.getToken()); + + this.tokenService.setProvider(TokenProvider.google); + this.tokenService.setLoggedIn(true); + this.tokenService.setEmail(profile.getEmail()); + this.tokenService.setUsername(); //GEMISE AYTO... + + /* AYTA FEYGOUN, xanontai me to refresh tis selidas + localStorage.setItem('currentUser', JSON.stringify(googleUser)); var currentUser = JSON.parse(localStorage.getItem('currentUser')); console.log('current user local storage:' +currentUser) + */ + + // STO LOGOUT, KATHARIZEIS TO tokenService (apo opoudipote... e;inai apo pantou prosvasimo, kai den xanei tis times toy oso exeis to browser anoixto.) + + + + //this.router.navigateByUrl('dynamic-form'); this.ngZone.run(() => this.router.navigateByUrl('projects')); //this.router.navigate(['/projects']); diff --git a/dmp-frontend/src/app/services/server.service.ts b/dmp-frontend/src/app/services/server.service.ts index d515e6888..ce862cce4 100644 --- a/dmp-frontend/src/app/services/server.service.ts +++ b/dmp-frontend/src/app/services/server.service.ts @@ -46,6 +46,8 @@ export class ServerService { //let headers = new HttpHeaders(); let headers = new HttpHeaders().set("google-token", LoginComponent.token); +console.log("google-token: "+ LoginComponent.token); + return this.http.get(this.fetchURL) .map( (response: Response) => { diff --git a/dmp-frontend/src/app/services/token.service.ts b/dmp-frontend/src/app/services/token.service.ts new file mode 100644 index 000000000..5b2bd7897 --- /dev/null +++ b/dmp-frontend/src/app/services/token.service.ts @@ -0,0 +1,77 @@ +import { Injectable, Inject} from '@angular/core'; +import { Observable } from 'rxjs'; +import {LocalStorageService} from 'ngx-webstorage'; +import 'rxjs/add/operator/map'; +import { DOCUMENT } from '@angular/platform-browser'; + +declare var jQuery : any; + +@Injectable() +export class TokenService { + + + constructor (private storage : LocalStorageService, @Inject(DOCUMENT) private document) { + var csrfToken : string = jQuery(document).find('meta[name="csrf-token"]').attr('content'); + this.setCSRFToken(csrfToken); + } + + + getCSRFToken() : string{ + return this.storage.retrieve('csrf-token'); + } + + setCSRFToken(csrfToken : string){ + this.storage.store('csrf-token',csrfToken); + } + + + getProvider() : TokenProvider { + return this.storage.retrieve('provider'); + } + + setProvider(provider : TokenProvider){ + this.storage.store('provider',provider); + } + + getToken() : string{ + return this.storage.retrieve('token'); + } + + setToken(token : string){ + this.storage.store('token',token); + } + + isLoggedIn(){ + return this.storage.retrieve('loggedIn'); + } + + setLoggedIn(boolStatus){ + this.storage.store('loggedIn',boolStatus); + } + + getEmail(){ + return this.storage.retrieve('email'); + } + + setEmail(email){ + this.storage.store('email',email); + } + + getUsername(){ + return this.storage.retrieve('username'); + } + + setUsername(username){ + this.storage.store('username',username); + } + + +} + +export enum TokenProvider { + native, + google +} + + +