-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #306 from logion-network/feature/secret-recovery
Secret Recovery Request
- Loading branch information
Showing
16 changed files
with
580 additions
and
4 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
resources/mail/secret-recovery-requested-legal-officer.pug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
| logion notification - Secret Recovery request to be reviewed | ||
| Dear #{legalOfficer.userIdentity.firstName} #{legalOfficer.userIdentity.lastName}, | ||
| | ||
| Please note, you can't answer this email. The logion network will never ask you to provide any kind of information or access to a web address through its email notifications. | ||
| | ||
| The following user has requested your help to recover a lost secret, please go to your logion application and start the review and approval process: | ||
| __________________________________________________________ | ||
| | ||
| Name: | ||
| ******* | ||
| #{walletUser.firstName} #{walletUser.lastName} | ||
| | ||
| Email: | ||
| ******* | ||
| #{walletUser.email} | ||
| | ||
| Telephone: | ||
| ************ | ||
| #{walletUser.phoneNumber} | ||
| | ||
| Address: | ||
| ********* | ||
| #{walletUserPostalAddress.line1} | ||
| #{walletUserPostalAddress.line2} | ||
| #{walletUserPostalAddress.postalCode} #{walletUserPostalAddress.city} | ||
| #{walletUserPostalAddress.country} | ||
| __________________________________________________________ | ||
| | ||
include /footer.pug |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
| logion notification - Recoverable secret added | ||
| Dear #{walletUser.firstName} #{walletUser.lastName}, | ||
| | ||
| You receive this message because you just requested to recover the secret with name #{secretName} linked to your Identity LOC #{loc.id}. | ||
| | ||
| Please note, you can't answer this email. The logion network will never ask you to provide any kind of information or access to a web address through its email notifications. | ||
| | ||
| As a reminder, find below all details of the Legal Officer you'll have to contact in order to recover the secret's value: | ||
| | ||
include /legal-officer-details.pug | ||
| | ||
include /footer.pug |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import { OpenAPIV3 } from "express-oas-generator"; | ||
import { | ||
addTag, | ||
setControllerTag, | ||
requireDefined, | ||
badRequest, | ||
getRequestBody, | ||
getDefaultResponses, | ||
Log | ||
} from "@logion/rest-api-core"; | ||
import { injectable } from "inversify"; | ||
import { Controller, HttpPost, ApiController, Async, SendsResponse } from "dinoloop"; | ||
import { components } from "./components.js"; | ||
import { SecretRecoveryRequestFactory, SecretRecoveryRequestDescription } from "../model/secret_recovery.model.js"; | ||
import { SecretRecoveryRequestService } from "../services/secret_recovery.service.js"; | ||
import { LocRequestRepository } from "../model/locrequest.model.js"; | ||
import moment from "moment"; | ||
import { NotificationRecipient, Template, NotificationService } from "../services/notification.service.js"; | ||
import { UserPrivateData } from "./adapters/locrequestadapter.js"; | ||
import { LocalsObject } from "pug"; | ||
import { DirectoryService } from "../services/directory.service.js"; | ||
import { ValidAccountId } from "@logion/node-api"; | ||
|
||
type CreateSecretRecoveryRequestView = components["schemas"]["CreateSecretRecoveryRequestView"]; | ||
|
||
const { logger } = Log; | ||
|
||
export function fillInSpec(spec: OpenAPIV3.Document): void { | ||
const tagName = 'SecretRecovery'; | ||
addTag(spec, { | ||
name: tagName, | ||
description: "Handling of Secret Recovery Requests" | ||
}); | ||
setControllerTag(spec, /^\/api\/secret-recovery.*/, tagName); | ||
|
||
SecretRecoveryController.createSecretRecoveryRequest(spec); | ||
} | ||
|
||
@injectable() | ||
@Controller('/secret-recovery') | ||
export class SecretRecoveryController extends ApiController { | ||
|
||
constructor( | ||
private secretRecoveryRequestFactory: SecretRecoveryRequestFactory, | ||
private secretRecoveryRequestService: SecretRecoveryRequestService, | ||
private locRequestRepository: LocRequestRepository, | ||
private directoryService: DirectoryService, | ||
private notificationService: NotificationService, | ||
) { | ||
super(); | ||
} | ||
|
||
static createSecretRecoveryRequest(spec: OpenAPIV3.Document) { | ||
const operationObject = spec.paths["/api/secret-recovery"].post!; | ||
operationObject.summary = "Creates a new Secret recovery request"; | ||
operationObject.description = "This is publicly available"; | ||
operationObject.requestBody = getRequestBody({ | ||
description: "Secret recovery request creation data", | ||
view: "secret-recovery", | ||
}); | ||
operationObject.responses = getDefaultResponses("ProtectionRequestView"); | ||
} | ||
|
||
@Async() | ||
@HttpPost('') | ||
@SendsResponse() | ||
async createSecretRecoveryRequest(body: CreateSecretRecoveryRequestView) { | ||
const { requesterIdentityLocId, challenge, secretName } = body; | ||
const requesterIdentityLoc = requireDefined( | ||
await this.locRequestRepository.findById(requesterIdentityLocId), | ||
() => badRequest("Identity LOC not found") | ||
); | ||
requireDefined( | ||
requesterIdentityLoc.secrets?.find(secret => secret.name === secretName), | ||
() => badRequest("Secret not found") | ||
) | ||
const userIdentity = { | ||
firstName: body.userIdentity.firstName || "", | ||
lastName: body.userIdentity.lastName || "", | ||
email: body.userIdentity.email || "", | ||
phoneNumber: body.userIdentity.phoneNumber || "", | ||
}; | ||
const userPostalAddress = { | ||
line1: body.userPostalAddress.line1 || "", | ||
line2: body.userPostalAddress.line2 || "", | ||
postalCode: body.userPostalAddress.postalCode || "", | ||
city: body.userPostalAddress.city || "", | ||
country: body.userPostalAddress.country || "", | ||
}; | ||
const recoveryRequest = this.secretRecoveryRequestFactory.newSecretRecoveryRequest({ | ||
requesterIdentityLocId, | ||
legalOfficerAddress: requesterIdentityLoc.getOwner(), | ||
challenge, | ||
secretName, | ||
userIdentity, | ||
userPostalAddress, | ||
createdOn: moment() | ||
}) | ||
await this.secretRecoveryRequestService.add(recoveryRequest); | ||
const userPrivateData: UserPrivateData = { | ||
identityLocId: requesterIdentityLocId, | ||
userIdentity, | ||
userPostalAddress, | ||
} | ||
this.notify("WalletUser", "secret-recovery-requested-user", recoveryRequest.getDescription(), requesterIdentityLoc.getOwner(), userPrivateData); | ||
this.notify("LegalOfficer", "secret-recovery-requested-legal-officer", recoveryRequest.getDescription(), requesterIdentityLoc.getOwner(), userPrivateData); | ||
this.response.sendStatus(204); | ||
} | ||
|
||
private notify(recipient: NotificationRecipient, templateId: Template, secretRecoveryRequest: SecretRecoveryRequestDescription, legalOfficerAccount: ValidAccountId, userPrivateData: UserPrivateData): void { | ||
this.getNotificationInfo(secretRecoveryRequest, legalOfficerAccount, userPrivateData) | ||
.then(info => { | ||
const to = recipient === "WalletUser" ? info.userEmail : info.legalOfficerEMail | ||
return this.notificationService.notify(to, templateId, info.data) | ||
.catch(reason => logger.warn("Failed to send email '%s' to %s : %s", templateId, to, reason)) | ||
}) | ||
.catch(reason => | ||
logger.warn("Failed to retrieve notification info from directory: %s. Mail '%' not sent.", reason, templateId) | ||
) | ||
} | ||
|
||
private async getNotificationInfo(secretRecoveryRequest: SecretRecoveryRequestDescription, legalOfficerAccount: ValidAccountId, userPrivateData: UserPrivateData): | ||
Promise<{ legalOfficerEMail: string, userEmail: string | undefined, data: LocalsObject }> { | ||
|
||
const legalOfficer = await this.directoryService.get(legalOfficerAccount) | ||
const { userIdentity, userPostalAddress } = userPrivateData; | ||
return { | ||
legalOfficerEMail: legalOfficer.userIdentity.email, | ||
userEmail: userIdentity?.email, | ||
data: { | ||
secretName: secretRecoveryRequest.secretName, | ||
legalOfficer, | ||
walletUser: userIdentity, | ||
walletUserPostalAddress: userPostalAddress | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
|
||
export class AddSecretRecovery1715933228153 implements MigrationInterface { | ||
name = 'AddSecretRecovery1715933228153' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`CREATE TABLE "secret_recovery_request" ("id" uuid NOT NULL, "requester_identity_loc_id" uuid NOT NULL, "legal_officer_address" character varying(255) NOT NULL, "secret_name" character varying(255) NOT NULL, "challenge" character varying(255) NOT NULL, "created_on" TIMESTAMP NOT NULL, "first_name" character varying(255), "last_name" character varying(255), "email" character varying(255), "phone_number" character varying(255), "line1" character varying(255), "line2" character varying(255), "postal_code" character varying(255), "city" character varying(255), "country" character varying(255), CONSTRAINT "PK_secret_recovery_request" PRIMARY KEY ("id"))`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`DROP TABLE "secret_recovery_request"`); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.