diff --git a/package-lock.json b/package-lock.json index 6e2dc822448361831eaa4025ca9b04c12f5343ff..239cbbc2db7119dd57a1941057f846471f810e38 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12189,9 +12189,9 @@ } }, "rxjs": { - "version": "6.5.5", - "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.5.5.tgz", - "integrity": "sha512-WfQI+1gohdf0Dai/Bbmk5L5ItH5tYqm3ki2c5GdWhKjalzjg93N3avFjVStyZZz+A2Em+ZxKH5bNghw9UeylGQ==", + "version": "6.6.3", + "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.6.3.tgz", + "integrity": "sha512-trsQc+xYYXZ3urjOiJOuCOa5N3jAZ3eiSpQB5hIT8zGlL2QfnHLJ2r7GMkBGuIausdJN1OneaI6gQlsqNHHmZQ==", "requires": { "tslib": "^1.9.0" } diff --git a/package.json b/package.json index 930d394301cc7c1b8c30de7b071eaba62e65297b..9c7fbc243b8f6a8ac10c29fb563aa5da0868d834 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "file-saver": "^2.0.2", "ng2-cookies": "^1.0.12", "ngx-filesaver": "^9.0.0", - "rxjs": "^6.5.5", + "rxjs": "^6.6.3", "stream": "0.0.2", "swagger-editor-dist": "^3.11.5", "timers": "^0.1.1", diff --git a/src/app/authentication/authentication.component.ts b/src/app/authentication/authentication.component.ts index 3fc7e5c52381e0d9267bec51902a62f231002af5..c8359c80163f2400ddfc8686556a3a43c40ee8f3 100644 --- a/src/app/authentication/authentication.component.ts +++ b/src/app/authentication/authentication.component.ts @@ -23,7 +23,6 @@ export class AuthenticationComponent implements OnInit { constructor(private tokenStorageService: TokenStorageService) { } ngOnInit(): void { this.isLoggedIn = !!this.tokenStorageService.getToken(); - if (this.isLoggedIn) { const user = this.tokenStorageService.getUser(); this.username = user.username; diff --git a/src/app/authentication/services/auth.service.ts b/src/app/authentication/services/auth.service.ts index a3e69484522d2c212f0318004833f737619d0b50..06806264032727bb129f5039e736c179f5408567 100644 --- a/src/app/authentication/services/auth.service.ts +++ b/src/app/authentication/services/auth.service.ts @@ -1,11 +1,12 @@ import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; -import { SmartHarvesterUser } from 'src/app/user/model/user'; +import { TokenStorageService } from './token-storage.service'; +import { map } from "rxjs/operators"; const AUTH_API = "http://localhost:8080/harvester/auth"; - +const F2DSAPI = "http://51.178.69.147" const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) }; @@ -15,7 +16,7 @@ const httpOptions = { }) export class AuthService { - constructor(private http: HttpClient) { } + constructor(private http: HttpClient, private tokenService: TokenStorageService) { } login(credentials): Observable<any> { let data: any @@ -35,12 +36,18 @@ export class AuthService { }, httpOptions); } - public isLoggedIn(){ - return localStorage.getItem('ACCESS_TOKEN') !== null; + getF2DSAuthToken():Observable<any> { + return this.http.post(F2DSAPI+'/tokens', { + email: "admin@cines-lab.fr", + password: "adminadmin" + },httpOptions); + } + public isLoggedIn() { + return this.tokenService.getToken() !== null; } - public logout(){ - localStorage.removeItem('ACCESS_TOKEN'); + public logout() { + this.tokenService.removeToken(); } } diff --git a/src/app/authentication/services/token-storage.service.ts b/src/app/authentication/services/token-storage.service.ts index 6c7852add1f28c8972d6899e259bb1f36df0f887..09ab345153fb1b4436fa06c29adbe2637d97a25d 100644 --- a/src/app/authentication/services/token-storage.service.ts +++ b/src/app/authentication/services/token-storage.service.ts @@ -1,7 +1,9 @@ import { Injectable } from '@angular/core'; +import { AuthService } from './auth.service'; const TOKEN_KEY = 'auth-token'; const USER_KEY = 'auth-user'; +const F2DS_TOKEN_KEY = 'f2ds-token'; @Injectable({ providedIn: 'root' @@ -13,13 +15,14 @@ export class TokenStorageService { window.sessionStorage.clear(); } - public saveToken(token: string): void { + public saveToken(token: string,f2dsToken:string): void { window.sessionStorage.removeItem(TOKEN_KEY); window.sessionStorage.setItem(TOKEN_KEY, token); + window.sessionStorage.setItem(F2DS_TOKEN_KEY, f2dsToken); } public getToken(): string { - return sessionStorage.getItem(TOKEN_KEY); + return window.sessionStorage.getItem(TOKEN_KEY); } public saveUser(user): void { @@ -30,4 +33,10 @@ export class TokenStorageService { public getUser(): any { return JSON.parse(sessionStorage.getItem(USER_KEY)); } + public removeToken(){ + window.localStorage.removeItem(TOKEN_KEY); + } + + + } diff --git a/src/app/authentication/signin/signin.component.ts b/src/app/authentication/signin/signin.component.ts index 3feb5fbc682e96a19b55795d4a3d3c66cdef30d4..a244fe32f4f53aa939bf00e422a4e3e3dbd5856c 100644 --- a/src/app/authentication/signin/signin.component.ts +++ b/src/app/authentication/signin/signin.component.ts @@ -5,6 +5,9 @@ import { SmartHarvesterUser } from 'src/app/user/model/user'; import { AuthService } from '../services/auth.service'; import { TokenStorageService } from '../services/token-storage.service'; +export interface F2DSResponse{ + token:string; +} @Component({ selector: 'app-signin', templateUrl: './signin.component.html', @@ -12,8 +15,8 @@ import { TokenStorageService } from '../services/token-storage.service'; }) export class SigninComponent implements OnInit { - - isSubmitted = false; + f2dsToken: string; + isSubmitted = false; isLoggedIn = false; isLoginFailed = false; errorMessage = ''; @@ -28,18 +31,24 @@ export class SigninComponent implements OnInit { ngOnInit(): void { if (this.tokenStorage.getToken()) { - // Uncomment line to avoid re-authentication if token is still valid - // this.isLoggedIn = true; + // Uncomment line to avoid re-authentication if token is still valid + //this.isLoggedIn = true; } + this.authService.getF2DSAuthToken().subscribe((response:F2DSResponse) => { + if (response) this.f2dsToken = response.token; + console.log("F2ds token is : ", this.f2dsToken) + }) } - get formControls() { return this.formGroup.controls; } + get formControls() { return this.formGroup.controls; } onSubmit(): void { - + + + this.authService.login(this.formGroup).subscribe( data => { - this.tokenStorage.saveToken(data.accessToken); + this.tokenStorage.saveToken(data.accessToken, this.f2dsToken); this.tokenStorage.saveUser(data); this.isLoginFailed = false; @@ -50,6 +59,7 @@ export class SigninComponent implements OnInit { }, err => { this.errorMessage = err.error.message; + console.error("Error : ", this.errorMessage) this.isLoginFailed = true; } ); @@ -57,7 +67,7 @@ export class SigninComponent implements OnInit { reloadPage(): void { window.location.reload(); - } + } getInputType() { diff --git a/src/app/repository/repository.component.ts b/src/app/repository/repository.component.ts index 39fd639e435b29a59c1af8db0d34a76354f9b032..bb17611ece096c8a499afba5812d4f134a314917 100644 --- a/src/app/repository/repository.component.ts +++ b/src/app/repository/repository.component.ts @@ -2,10 +2,12 @@ import { Component, OnInit } from '@angular/core'; import { Validators } from '@angular/forms'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { FileSaverService } from 'ngx-filesaver'; -import { FormControl} from '@angular/forms'; -import { FormGroup} from '@angular/forms'; +import { FormControl } from '@angular/forms'; +import { FormGroup } from '@angular/forms'; import { AppConfiguration } from '../AppConfiguration'; import { environment } from 'src/environments/environment.prod'; +import { AuthService } from '../authentication/services/auth.service'; +import { PublishRepositoryService } from './services/publish-repository.service'; @Component({ @@ -20,118 +22,117 @@ export class RepositoryComponent implements OnInit { public importFile: File; public resultat: any; - Form = new FormGroup({ - repotype: new FormControl(), - reponame: new FormControl('', Validators.required), - repodescription: new FormControl(), - repourl: new FormControl(), - repoversion: new FormControl(), - repolicence: new FormControl(), - repolanguage: new FormControl(), - filetofill: new FormControl(), + Form = new FormGroup({ + repotype: new FormControl(), + reponame: new FormControl('', Validators.required), + repodescription: new FormControl(), + repourl: new FormControl(), + repoversion: new FormControl(), + repolicence: new FormControl(), + repolanguage: new FormControl(), + filetofill: new FormControl(), }); - constructor( - private appConfig: AppConfiguration, - private http: HttpClient, - private _FileSaverService: FileSaverService, - ) { } + constructor(private appConfig: AppConfiguration, private http: HttpClient, private authService:AuthService, + private _FileSaverService: FileSaverService,private publishService: PublishRepositoryService) { } ngOnInit() { + this.authService.getF2DSAuthToken() } repositorytoyaml(buttonType) { -if (buttonType == 'submit') { -let data: string; + if (buttonType == 'submit') { + let data: string; -data ='\ + data = '\ repository:\n\ - type: '+ this.Form.value.repotype +'\n\ - title: '+ this.Form.value.reponame +'\n\ - description: '+ this.Form.value.repodescription +'\n\ - url: '+ this.Form.value.repourl +'\n\ - version: '+ this.Form.value.repoversion +' \n\ - licence: '+ this.Form.value.repolicence +'\n\ - language: '+ this.Form.value.repolanguage +'\n\ + type: '+ this.Form.value.repotype + '\n\ + title: '+ this.Form.value.reponame + '\n\ + description: '+ this.Form.value.repodescription + '\n\ + url: '+ this.Form.value.repourl + '\n\ + version: '+ this.Form.value.repoversion + ' \n\ + licence: '+ this.Form.value.repolicence + '\n\ + language: '+ this.Form.value.repolanguage + '\n\ ' - const fileName = `repository.yaml`; - const fileType = this._FileSaverService.genType(fileName); - const txtBlob = new Blob([data], { type: fileType }); - - this._FileSaverService.save(txtBlob, fileName); - } - - if (buttonType == 'fill') { - const files = (event.target as HTMLInputElement).files; - this.importFile = files[0]; - - let text = ""; - let lines = []; - let line = ""; - let map = new Map(); - let fileReader = new FileReader(); + const fileName = `repository.yaml`; + const fileType = this._FileSaverService.genType(fileName); + const txtBlob = new Blob([data], { type: fileType }); + this._FileSaverService.save(txtBlob, fileName); + } - fileReader.onload = (event) => { - text = fileReader.result as string; + if (buttonType == 'fill') { + const files = (event.target as HTMLInputElement).files; + this.importFile = files[0]; + + let text = ""; + let lines = []; + let line = ""; + let map = new Map(); + let fileReader = new FileReader(); + + + fileReader.onload = (event) => { + text = fileReader.result as string; + + lines = text.split("\n"); + for (let i = 0; i < lines.length; i++) { + line = lines[i].split(": "); + map.set(line[0].trim(), line[1]); + } + + this.Form.setValue({ + repotype: map.get('type'), + reponame: map.get('title'), + repodescription: map.get('description'), + repourl: map.get('url'), + repoversion: map.get('version'), + repolicence: map.get('licence'), + repolanguage: map.get('language'), + filetofill: '' + }); - lines = text.split("\n"); - for ( let i = 0; i < lines.length; i++) { - line = lines[i].split(": "); - map.set(line[0].trim(),line[1]); } - - this.Form.setValue({ - repotype: map.get('type') , - reponame: map.get('title') , - repodescription: map.get('description'), - repourl: map.get('url'), - repoversion: map.get('version'), - repolicence: map.get('licence'), - repolanguage: map.get('language'), - filetofill: '' - }); - + fileReader.readAsText(this.importFile); } - fileReader.readAsText(this.importFile); - } - if (buttonType == 'publish') { + if (buttonType == 'publish') { - let data: string; + let data: string; - data ='\ + data = '\ @prefix dcat: <http://www.w3.org/ns/dcat#>.\n\ @prefix dct: <http://purl.org/dc/terms/>.\n\ -@prefix fdp: <'+this.appConfig.fdpurl+'/>.\n\nfdp:new \n\ +@prefix fdp: <'+ this.appConfig.fdpurl + '/>.\n\nfdp:new \n\ a dcat:Catalog, dcat:Resource;\n\ dct:description "'+ this.Form.value.repodescription + '";\n\ dct:hasVersion "'+ this.Form.value.repoversion + '";\n\ - dct:isPartOf <'+this.appConfig.fdpurl+'>;\n\ - dcat:keyword "'+this.Form.value.repotype+'";\n\ + dct:isPartOf <'+ this.appConfig.fdpurl + '>;\n\ + dcat:keyword "'+ this.Form.value.repotype + '";\n\ dct:title "'+ this.Form.value.reponame + '".\n' - const httpOptions = { - headers: new HttpHeaders({ - 'Accept': 'text/turtle', - 'Content-Type': 'text/turtle', - 'Authorization': 'Bearer '+ environment.token - }) - }; + return this.publishService.publishRepository(data); + /* const httpOptions = { + headers: new HttpHeaders({ + 'Accept': 'text/turtle', + 'Content-Type': 'text/turtle', + 'Authorization': 'Bearer ' + environment.token + }) + }; - this.resultat = this.http - .post(this.appConfig.fdpurl+"/catalog", data, httpOptions ) - .subscribe( (r)=>{console.log('reponse: ', r)}) ; + this.resultat = this.http + .post(this.appConfig.fdpurl + "/catalog", data, httpOptions) + .subscribe((r) => { console.log('reponse: ', r) }); console.log("resultat: " + JSON.stringify(this.resultat)); - return JSON.stringify(this.resultat); + return JSON.stringify(this.resultat); */ - } + } } diff --git a/src/app/repository/services/publish-repository.service.spec.ts b/src/app/repository/services/publish-repository.service.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..30108e85fb0fb0b98bddc891fc8e7ce830d25192 --- /dev/null +++ b/src/app/repository/services/publish-repository.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { PublishRepositoryService } from './publish-repository.service'; + +describe('PublishRepositoryService', () => { + let service: PublishRepositoryService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(PublishRepositoryService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/repository/services/publish-repository.service.ts b/src/app/repository/services/publish-repository.service.ts new file mode 100644 index 0000000000000000000000000000000000000000..7a592fda1f3381cefd6210b471e89b95affeea90 --- /dev/null +++ b/src/app/repository/services/publish-repository.service.ts @@ -0,0 +1,41 @@ +import { HttpClient, HttpHeaders } from '@angular/common/http'; +import { Injectable } from '@angular/core'; +import { AppConfiguration } from 'src/app/AppConfiguration'; +import { AuthService } from 'src/app/authentication/services/auth.service'; +import { environment } from 'src/environments/environment.prod'; + + + + +@Injectable({ + providedIn: 'root' +}) +export class PublishRepositoryService { + + fds2Token: string + constructor(private http: HttpClient,private appConfig: AppConfiguration, private authService: AuthService) { } + + publishRepository(data:string){ + + this.authService.getF2DSAuthToken().subscribe(data=>{ + this.fds2Token = data.token + }) + if (this.fds2Token) { + const httpOptions = { + headers: new HttpHeaders({ + 'Accept': 'text/turtle', + 'Content-Type': 'text/turtle', + 'Authorization': 'Bearer '+ this.fds2Token + }) + }; + + let resultat = this.http.post(this.appConfig.fdpurl+"/catalog", data, httpOptions ).subscribe( (r)=>{console.log('reponse: ', r)}) ; + if (resultat){ + console.log("resultat: " + JSON.stringify(resultat)); + return JSON.stringify(resultat); + } + return "The repository has not been published" + } + } + +}