-
Notifications
You must be signed in to change notification settings - Fork 3
[_]: feature/mail EPs #380
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,148 @@ | ||
| import { ApiSecurity, ApiUrl, AppDetails } from '../shared'; | ||
| import { headersWithToken } from '../shared/headers'; | ||
| import { HttpClient } from '../shared/http/client'; | ||
| import { | ||
| EncryptedKeystore, | ||
| KeystoreType, | ||
| HybridEncryptedEmail, | ||
| PwdProtectedEmail, | ||
| RecipientWithPublicKey, | ||
| base64ToUint8Array, | ||
| EmailPublicParameters, | ||
| } from 'internxt-crypto'; | ||
| import { | ||
| MailboxResponse, | ||
| EmailListResponse, | ||
| EmailResponse, | ||
| EmailCreatedResponse, | ||
| SendEmailRequest, | ||
| DraftEmailRequest, | ||
| UpdateEmailRequest, | ||
| ListEmailsQuery, | ||
| } from './types'; | ||
|
|
||
| export class MailApi { | ||
| private readonly client: HttpClient; | ||
| private readonly appDetails: AppDetails; | ||
| private readonly apiSecurity: ApiSecurity; | ||
| private readonly apiUrl: ApiUrl; | ||
|
|
||
| public static client(apiUrl: ApiUrl, appDetails: AppDetails, apiSecurity: ApiSecurity) { | ||
| return new MailApi(apiUrl, appDetails, apiSecurity); | ||
| } | ||
|
|
||
| private constructor(apiUrl: ApiUrl, appDetails: AppDetails, apiSecurity: ApiSecurity) { | ||
| this.client = HttpClient.create(apiUrl, apiSecurity.unauthorizedCallback); | ||
| this.appDetails = appDetails; | ||
| this.apiSecurity = apiSecurity; | ||
| this.apiUrl = apiUrl; | ||
| } | ||
|
|
||
| /** | ||
| * Uploads encrypted keystore to the server | ||
| * | ||
| * @param keystore - The encrypted keystore | ||
| * @returns Server response | ||
| */ | ||
| async uploadKeystore(keystore: EncryptedKeystore): Promise<void> { | ||
| return this.client.post(`${this.apiUrl}/keystore`, { encryptedKeystore: keystore }, this.headers()); | ||
| } | ||
|
|
||
| /** | ||
| * Requests encrypted keystore from the server | ||
| * | ||
| * @param userEmail - The email of the user | ||
| * @param keystoreType - The type of the keystore | ||
| * @returns The encrypted keystore | ||
| */ | ||
| async downloadKeystore(userEmail: string, keystoreType: KeystoreType): Promise<EncryptedKeystore> { | ||
| return this.client.getWithParams(`${this.apiUrl}/user/keystore`, { userEmail, keystoreType }, this.headers()); | ||
| } | ||
|
|
||
| /** | ||
| * Requests users with corresponding public keys from the server | ||
| * | ||
| * @param emails - The emails of the users | ||
| * @returns Users with corresponding public keys | ||
| */ | ||
| async getUsersWithPublicKeys(emails: string[]): Promise<RecipientWithPublicKey[]> { | ||
| const response = await this.client.post<{ publicKey: string; email: string }[]>( | ||
| `${this.apiUrl}/users/public-keys`, | ||
| { emails }, | ||
| this.headers(), | ||
| ); | ||
|
|
||
| const result = await Promise.all( | ||
| response.map(async (item) => { | ||
| const publicHybridKey = base64ToUint8Array(item.publicKey); | ||
| return { email: item.email, publicHybridKey }; | ||
| }), | ||
| ); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Sends the encrypted emails to the server | ||
| * | ||
| * @param emails - The encrypted emails | ||
| * @param params - The public parameters of the email | ||
| * @returns Server response | ||
| */ | ||
| async sendE2EEmails(emails: HybridEncryptedEmail[], params: EmailPublicParameters): Promise<void> { | ||
| return this.client.post(`${this.apiUrl}/emails`, { emails, params }, this.headers()); | ||
| } | ||
|
|
||
| /** | ||
| * Sends the password-protected email to the server | ||
| * | ||
| * @param email - The password-protected email | ||
| * @param params - The public parameters of the email | ||
| * @returns Server response | ||
| */ | ||
| async sendE2EPasswordProtectedEmail(email: PwdProtectedEmail, params: EmailPublicParameters): Promise<void> { | ||
| return this.client.post(`${this.apiUrl}/emails`, { email, params }, this.headers()); | ||
| } | ||
|
|
||
| async getMailboxes(): Promise<MailboxResponse[]> { | ||
| return this.client.get(`${this.apiUrl}/email/mailboxes`, this.headers()); | ||
| } | ||
|
|
||
| async listEmails(query?: ListEmailsQuery): Promise<EmailListResponse> { | ||
| return this.client.getWithParams(`${this.apiUrl}/email`, query ?? {}, this.headers()); | ||
| } | ||
|
|
||
| async getEmail(id: string): Promise<EmailResponse> { | ||
| return this.client.get(`${this.apiUrl}/email/${id}`, this.headers()); | ||
| } | ||
|
|
||
| async deleteEmail(id: string): Promise<void> { | ||
| return this.client.delete(`${this.apiUrl}/email/${id}`, this.headers()); | ||
| } | ||
|
|
||
| async updateEmail(id: string, body: UpdateEmailRequest): Promise<void> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What will this do?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This EP will be used to mark an email as read/flagged. |
||
| return this.client.patch(`${this.apiUrl}/email/${id}`, body, this.headers()); | ||
| } | ||
|
|
||
| async sendEmail(body: SendEmailRequest): Promise<EmailCreatedResponse> { | ||
| return this.client.post(`${this.apiUrl}/email/send`, body, this.headers()); | ||
| } | ||
|
|
||
| async saveDraft(body: DraftEmailRequest): Promise<EmailCreatedResponse> { | ||
| return this.client.post(`${this.apiUrl}/email/drafts`, body, this.headers()); | ||
xabg2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Returns the needed headers for the module requests | ||
| * @private | ||
| */ | ||
| private headers() { | ||
| return headersWithToken({ | ||
| clientName: this.appDetails.clientName, | ||
| clientVersion: this.appDetails.clientVersion, | ||
| token: this.apiSecurity.token, | ||
| desktopToken: this.appDetails.desktopHeader, | ||
| customHeaders: this.appDetails.customHeaders, | ||
| }); | ||
| } | ||
| } | ||
File renamed without changes.
This file contains hidden or 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 |
|---|---|---|
| @@ -1,200 +1,2 @@ | ||
| import { ApiSecurity, ApiUrl, AppDetails } from '../shared'; | ||
| import { headersWithToken } from '../shared/headers'; | ||
| import { HttpClient } from '../shared/http/client'; | ||
| import { | ||
| EncryptedKeystore, | ||
| KeystoreType, | ||
| HybridEncryptedEmail, | ||
| PwdProtectedEmail, | ||
| HybridKeyPair, | ||
| Email, | ||
| RecipientWithPublicKey, | ||
| base64ToUint8Array, | ||
| EmailPublicParameters, | ||
| } from 'internxt-crypto'; | ||
|
|
||
| import { createKeystores, encryptEmail, passwordProtectAndSendEmail, openKeystore, recoverKeys } from './create'; | ||
|
|
||
| export class Mail { | ||
| private readonly client: HttpClient; | ||
| private readonly appDetails: AppDetails; | ||
| private readonly apiSecurity: ApiSecurity; | ||
| private readonly apiUrl: ApiUrl; | ||
|
|
||
| public static client(apiUrl: ApiUrl, appDetails: AppDetails, apiSecurity: ApiSecurity) { | ||
| return new Mail(apiUrl, appDetails, apiSecurity); | ||
| } | ||
|
|
||
| private constructor(apiUrl: ApiUrl, appDetails: AppDetails, apiSecurity: ApiSecurity) { | ||
| this.client = HttpClient.create(apiUrl, apiSecurity.unauthorizedCallback); | ||
| this.appDetails = appDetails; | ||
| this.apiSecurity = apiSecurity; | ||
| this.apiUrl = apiUrl; | ||
| } | ||
|
|
||
| /** | ||
| * Uploads encrypted keystore to the server | ||
| * | ||
| * @param encryptedKeystore - The encrypted keystore | ||
| * @returns Server response | ||
| */ | ||
| async uploadKeystoreToServer(encryptedKeystore: EncryptedKeystore): Promise<void> { | ||
| return this.client.post(`${this.apiUrl}/keystore`, { encryptedKeystore }, this.headers()); | ||
| } | ||
|
|
||
| /** | ||
| * Creates recovery and encryption keystores and uploads them to the server | ||
| * | ||
| * @param userEmail - The email of the user | ||
| * @param baseKey - The secret key of the user | ||
| * @returns The recovery codes and keys of the user | ||
| */ | ||
| async createAndUploadKeystores( | ||
| userEmail: string, | ||
| baseKey: Uint8Array, | ||
| ): Promise<{ recoveryCodes: string; keys: HybridKeyPair }> { | ||
| const { encryptionKeystore, recoveryKeystore, recoveryCodes, keys } = await createKeystores(userEmail, baseKey); | ||
| await Promise.all([this.uploadKeystoreToServer(encryptionKeystore), this.uploadKeystoreToServer(recoveryKeystore)]); | ||
| return { recoveryCodes, keys }; | ||
| } | ||
|
|
||
| /** | ||
| * Requests encrypted keystore from the server | ||
| * | ||
| * @param userEmail - The email of the user | ||
| * @param keystoreType - The type of the keystore | ||
| * @returns The encrypted keystore | ||
| */ | ||
| async downloadKeystoreFromServer(userEmail: string, keystoreType: KeystoreType): Promise<EncryptedKeystore> { | ||
| return this.client.getWithParams(`${this.apiUrl}/user/keystore`, { userEmail, keystoreType }, this.headers()); | ||
| } | ||
|
|
||
| /** | ||
| * Requests encrypted keystore from the server and opens it | ||
| * | ||
| * @param userEmail - The email of the user | ||
| * @param baseKey - The secret key of the user | ||
| * @returns The hybrid keys of the user | ||
| */ | ||
| async getUserEmailKeys(userEmail: string, baseKey: Uint8Array): Promise<HybridKeyPair> { | ||
| const keystore = await this.downloadKeystoreFromServer(userEmail, KeystoreType.ENCRYPTION); | ||
| return openKeystore(keystore, baseKey); | ||
| } | ||
|
|
||
| /** | ||
| * Requests recovery keystore from the server and opens it | ||
| * | ||
| * @param userEmail - The email of the user | ||
| * @param recoveryCodes - The recovery codes of the user | ||
| * @returns The hybrid keys of the user | ||
| */ | ||
| async recoverUserEmailKeys(userEmail: string, recoveryCodes: string): Promise<HybridKeyPair> { | ||
| const keystore = await this.downloadKeystoreFromServer(userEmail, KeystoreType.RECOVERY); | ||
| return recoverKeys(keystore, recoveryCodes); | ||
| } | ||
|
|
||
| /** | ||
| * Request user with corresponding public keys from the server | ||
| * | ||
| * @param userEmail - The email of the user | ||
| * @returns User with corresponding public keys | ||
| */ | ||
| async getUserWithPublicKeys(userEmail: string): Promise<RecipientWithPublicKey> { | ||
| const response = await this.client.post<{ publicKey: string; email: string }[]>( | ||
| `${this.apiUrl}/users/public-keys`, | ||
| { emails: [userEmail] }, | ||
| this.headers(), | ||
| ); | ||
| if (!response[0]) throw new Error(`No public keys found for ${userEmail}`); | ||
| const singleResponse = response[0]; | ||
| const publicHybridKey = base64ToUint8Array(singleResponse.publicKey); | ||
| const result = { email: singleResponse.email, publicHybridKey }; | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Request users with corresponding public keys from the server | ||
| * | ||
| * @param emails - The emails of the users | ||
| * @returns Users with corresponding public keys | ||
| */ | ||
| async getSeveralUsersWithPublicKeys(emails: string[]): Promise<RecipientWithPublicKey[]> { | ||
| const response = await this.client.post<{ publicKey: string; email: string }[]>( | ||
| `${this.apiUrl}/users/public-keys`, | ||
| { emails }, | ||
| this.headers(), | ||
| ); | ||
|
|
||
| const result = await Promise.all( | ||
| response.map(async (item) => { | ||
| const publicHybridKey = base64ToUint8Array(item.publicKey); | ||
| return { email: item.email, publicHybridKey }; | ||
| }), | ||
| ); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Sends the encrypted emails to the server | ||
| * | ||
| * @param emails - The encrypted emails | ||
| * @param params - The public parameters of the email (sender, recipients, CCs, BCCs, etc.) | ||
| * @returns Server response | ||
| */ | ||
| async sendEncryptedEmail(emails: HybridEncryptedEmail[], params: EmailPublicParameters): Promise<void> { | ||
| return this.client.post(`${this.apiUrl}/emails`, { emails, params }, this.headers()); | ||
| } | ||
|
|
||
| /** | ||
| * Encrypts and sends email(s) to the server | ||
| * | ||
| * @param email - The message to encrypt | ||
| * @param aux - The optional auxilary data to encrypt together with the email (e.g. email sender) | ||
| * @returns Server response | ||
| */ | ||
| async encryptAndSendEmail(email: Email, aux?: string): Promise<void> { | ||
| const recipientEmails = email.params.recipients.map((user) => user.email); | ||
| const recipients = await this.getSeveralUsersWithPublicKeys(recipientEmails); | ||
|
|
||
| const encEmails = await encryptEmail(email, recipients, aux); | ||
| return this.sendEncryptedEmail(encEmails, email.params); | ||
| } | ||
|
|
||
| /** | ||
| * Sends the password-protected email to the server | ||
| * | ||
| * @param email - The password-protected email | ||
| * @returns Server response | ||
| */ | ||
| async sendPasswordProtectedEmail(email: PwdProtectedEmail, params: EmailPublicParameters): Promise<void> { | ||
| return this.client.post(`${this.apiUrl}/emails`, { email, params }, this.headers()); | ||
| } | ||
|
|
||
| /** | ||
| * Creates the password-protected email and sends it to the server | ||
| * | ||
| * @param email - The email | ||
| * @param pwd - The password | ||
| * @param aux - The optional auxilary data to encrypt together with the email (e.g. email sender) | ||
| * @returns Server response | ||
| */ | ||
| async passwordProtectAndSendEmail(email: Email, pwd: string, aux?: string): Promise<void> { | ||
| const encEmail = await passwordProtectAndSendEmail(email, pwd, aux); | ||
| return this.sendPasswordProtectedEmail(encEmail, email.params); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the needed headers for the module requests | ||
| * @private | ||
| */ | ||
| private headers() { | ||
| return headersWithToken({ | ||
| clientName: this.appDetails.clientName, | ||
| clientVersion: this.appDetails.clientVersion, | ||
| token: this.apiSecurity.token, | ||
| desktopToken: this.appDetails.desktopHeader, | ||
| customHeaders: this.appDetails.customHeaders, | ||
| }); | ||
| } | ||
| } | ||
| export * from './types'; | ||
| export * from './mail'; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xabg2 It should be just sendEmail, no? Then we can add other functions that will send encrypted or password-protected emails
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the name to differentiate it from the other EPs that do not have the crypto layer )and they are not used rn in mail web). But we can change it in this PR if you want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's ok, I've just thought that the next PR will add all crypto in the email, including new send methods. But at the end it will be the same