From 7d624a2daa346a075d6a9514134f615508baa02a Mon Sep 17 00:00:00 2001 From: Aldo Mihasi Date: Thu, 27 Oct 2022 09:32:26 +0300 Subject: [PATCH] [WIP] system deposit authentication, using default repo token --- .../eudat/controllers/DepositController.java | 10 +++++---- .../managers/DataManagementPlanManager.java | 4 ++-- .../eu/eudat/models/data/doi/DepositCode.java | 21 +++++++++++++++++++ .../interfaces/ZenodoDeposit.java | 7 ++++++- .../app/core/model/deposit/deposit-request.ts | 5 +++++ .../deposit-repositories.service.ts | 11 ++++++---- .../dmp-deposit-dialog.component.ts | 21 ++++++++----------- .../ui/dmp/overview/dmp-overview.component.ts | 1 + 8 files changed, 57 insertions(+), 23 deletions(-) create mode 100644 dmp-backend/web/src/main/java/eu/eudat/models/data/doi/DepositCode.java diff --git a/dmp-backend/web/src/main/java/eu/eudat/controllers/DepositController.java b/dmp-backend/web/src/main/java/eu/eudat/controllers/DepositController.java index 1687f560f..c0d603dba 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/controllers/DepositController.java +++ b/dmp-backend/web/src/main/java/eu/eudat/controllers/DepositController.java @@ -4,6 +4,7 @@ import eu.eudat.depositinterface.repository.RepositoryDepositConfiguration; import eu.eudat.logic.managers.DepositManager; import eu.eudat.logic.security.claims.ClaimedAuthorities; import eu.eudat.logic.services.ApiContext; +import eu.eudat.models.data.doi.DepositCode; import eu.eudat.models.data.doi.DepositRequest; import eu.eudat.models.data.helpers.responses.ResponseItem; import eu.eudat.models.data.security.Principal; @@ -39,15 +40,16 @@ public class DepositController extends BaseController { return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem>().status(ApiMessageCode.NO_MESSAGE).payload(ids)); } - @RequestMapping(method = RequestMethod.GET, value = {"/getAccessToken"}) + @RequestMapping(method = RequestMethod.POST, value = {"/getAccessToken"}) public @ResponseBody - ResponseEntity> getAccessToken(@RequestParam("repository") String repository, @RequestParam("code") String code, @ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal principal) throws Exception { - String accessToken = this.depositManager.authenticate(repository, code); + ResponseEntity> getAccessToken(@RequestBody DepositCode depositCode, @ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal principal) throws Exception { + String accessToken = this.depositManager.authenticate(depositCode.getRepositoryId(), depositCode.getCode()); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().status(ApiMessageCode.NO_MESSAGE).payload(accessToken)); } @RequestMapping(method = RequestMethod.POST, value = {"/createDoi"}) - public ResponseEntity> createDoi(@RequestBody DepositRequest depositRequest, @ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal principal) { + public @ResponseBody + ResponseEntity> createDoi(@RequestBody DepositRequest depositRequest, @ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal principal) { try { String doi = this.depositManager.deposit(depositRequest, principal); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().status(ApiMessageCode.SUCCESS_MESSAGE).message("Successfully created DOI for Data Datamanagement Plan in question.").payload(doi)); diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DataManagementPlanManager.java b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DataManagementPlanManager.java index b6d734eb4..fff2b168a 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DataManagementPlanManager.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DataManagementPlanManager.java @@ -2151,8 +2151,8 @@ public class DataManagementPlanManager { } }).orElse(null); if (finalDoi != null) { - //dmp.setDoi(finalDoi); - //apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().createOrUpdate(dmp); + dmp.setDoi(finalDoi); + apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().createOrUpdate(dmp); } Files.deleteIfExists(file.getFile().toPath()); diff --git a/dmp-backend/web/src/main/java/eu/eudat/models/data/doi/DepositCode.java b/dmp-backend/web/src/main/java/eu/eudat/models/data/doi/DepositCode.java new file mode 100644 index 000000000..a567dc9da --- /dev/null +++ b/dmp-backend/web/src/main/java/eu/eudat/models/data/doi/DepositCode.java @@ -0,0 +1,21 @@ +package eu.eudat.models.data.doi; + +public class DepositCode { + + private String repositoryId; + private String code; + + public String getRepositoryId() { + return repositoryId; + } + public void setRepositoryId(String repositoryId) { + this.repositoryId = repositoryId; + } + + public String getCode() { + return code; + } + public void setCode(String code) { + this.code = code; + } +} diff --git a/dmp-backend/zenodoRepository/src/main/java/eu/eudat/depositinterface/zenodorepository/interfaces/ZenodoDeposit.java b/dmp-backend/zenodoRepository/src/main/java/eu/eudat/depositinterface/zenodorepository/interfaces/ZenodoDeposit.java index c3bdef1ad..b8696885f 100644 --- a/dmp-backend/zenodoRepository/src/main/java/eu/eudat/depositinterface/zenodorepository/interfaces/ZenodoDeposit.java +++ b/dmp-backend/zenodoRepository/src/main/java/eu/eudat/depositinterface/zenodorepository/interfaces/ZenodoDeposit.java @@ -27,7 +27,7 @@ import java.io.IOException; import java.nio.file.Files; import java.util.*; -import static eu.eudat.depositinterface.repository.RepositoryDepositConfiguration.DepositAccountStatus.BothWaysDeposit; +import static eu.eudat.depositinterface.repository.RepositoryDepositConfiguration.DepositAccountStatus.*; @Component public class ZenodoDeposit implements RepositoryDeposit { @@ -45,6 +45,11 @@ public class ZenodoDeposit implements RepositoryDeposit { public String deposit(DMPDepositModel dmpDepositModel, boolean update, String zenodoToken) throws Exception { RepositoryDepositConfiguration conf = this.getConfiguration(); + + if(zenodoToken == null){ + zenodoToken = conf.getAccessToken(); + } + String zenodoUrl = conf.getRepositoryUrl(); // First step, post call to Zenodo, to create the entry. diff --git a/dmp-frontend/src/app/core/model/deposit/deposit-request.ts b/dmp-frontend/src/app/core/model/deposit/deposit-request.ts index daf62d32c..10ee13980 100644 --- a/dmp-frontend/src/app/core/model/deposit/deposit-request.ts +++ b/dmp-frontend/src/app/core/model/deposit/deposit-request.ts @@ -2,4 +2,9 @@ export class DepositRequest { repositoryId: string; dmpId: string; accessToken: string; +} + +export class DepositCode { + repositoryId: string; + code: string; } \ No newline at end of file diff --git a/dmp-frontend/src/app/core/services/deposit-repositories/deposit-repositories.service.ts b/dmp-frontend/src/app/core/services/deposit-repositories/deposit-repositories.service.ts index 2c9becc6c..d4cee9ada 100644 --- a/dmp-frontend/src/app/core/services/deposit-repositories/deposit-repositories.service.ts +++ b/dmp-frontend/src/app/core/services/deposit-repositories/deposit-repositories.service.ts @@ -1,7 +1,7 @@ import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { DepositConfigurationModel } from '@app/core/model/deposit/deposit-configuration'; -import { DepositRequest } from '@app/core/model/deposit/deposit-request'; +import { DepositCode, DepositRequest } from '@app/core/model/deposit/deposit-request'; import { Observable } from 'rxjs'; import { ConfigurationService } from '../configuration/configuration.service'; import { BaseHttpService } from '../http/base-http.service'; @@ -20,11 +20,14 @@ export class DepositRepositoriesService { return this.http.get(this.actionUrl + 'repos', { headers: this.headers }); } - getAccessToken(repository: string, code: string): Observable { - return this.http.get(this.actionUrl + 'getAccessToken?repository=' + repository + '&code=' + code, { headers: this.headers }); + getAccessToken(repositoryId: string, code: string): Observable { + const depositCode = new DepositCode(); + depositCode.repositoryId = repositoryId; + depositCode.code = code; + return this.http.post(this.actionUrl + 'getAccessToken', depositCode, { headers: this.headers }); } - createDoi(repositoryId: string, dmpId: string, accessToken: string): Observable { + createDoi(repositoryId: string, dmpId: string, accessToken: string | null): Observable { const depositRequest = new DepositRequest(); depositRequest.repositoryId = repositoryId; depositRequest.dmpId = dmpId; diff --git a/dmp-frontend/src/app/ui/dmp/editor/dmp-deposit-dialog/dmp-deposit-dialog.component.ts b/dmp-frontend/src/app/ui/dmp/editor/dmp-deposit-dialog/dmp-deposit-dialog.component.ts index 2fb0eac2d..acd7dec11 100644 --- a/dmp-frontend/src/app/ui/dmp/editor/dmp-deposit-dialog/dmp-deposit-dialog.component.ts +++ b/dmp-frontend/src/app/ui/dmp/editor/dmp-deposit-dialog/dmp-deposit-dialog.component.ts @@ -71,15 +71,12 @@ export class DmpDepositDialogComponent extends BaseComponent implements OnInit { + 'http://localhost:4200/login/external/zenodo', repo, this.dmp); break; case 1: - // this.dmpService.getDoi(dmp.id) - // .pipe(takeUntil(this._destroyed)) - // .subscribe( - // complete => { - // this.onDOICallbackSuccess(); - // this.dmp.doi = complete; - // }, - // error => this.onDeleteCallbackError(error) - // ); + this.depositRepositoriesService.createDoi(repo.repositoryId, this.dmp.id, null) + .pipe(takeUntil(this._destroyed)) + .subscribe(doi =>{ + this.onDOICallbackSuccess(); + this.outputRepos.push(doi); + }, error => this.onDeleteCallbackError(error)); break; } }); @@ -104,12 +101,12 @@ export class DmpDepositDialogComponent extends BaseComponent implements OnInit { this.depositRepositoriesService.getAccessToken(repo.repositoryId, result.oauthCode) .pipe(takeUntil(this._destroyed)) .subscribe(token => { - console.log(token); this.depositRepositoriesService.createDoi(repo.repositoryId, dmp.id, token) .pipe(takeUntil(this._destroyed)) .subscribe(doi =>{ - console.log(doi); - }); + this.onDOICallbackSuccess(); + this.outputRepos.push(doi); + }, error => this.onDeleteCallbackError(error)); }); // this.userService.registerDOIToken(result.oauthCode, this.configurationService.app + 'oauth2') // .pipe(takeUntil(this._destroyed)) diff --git a/dmp-frontend/src/app/ui/dmp/overview/dmp-overview.component.ts b/dmp-frontend/src/app/ui/dmp/overview/dmp-overview.component.ts index bf561abbc..eb89f320d 100644 --- a/dmp-frontend/src/app/ui/dmp/overview/dmp-overview.component.ts +++ b/dmp-frontend/src/app/ui/dmp/overview/dmp-overview.component.ts @@ -604,6 +604,7 @@ export class DmpOverviewComponent extends BaseComponent implements OnInit { dialogRef.afterClosed().pipe(takeUntil(this._destroyed)).subscribe((result: string[]) => { if (result) { this.dmp.doi = result[0]; + this.hasDOIToken = true; } }); }