Skip to content

Commit

Permalink
DEVEXP-607: Mailgun authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
asein-sinch committed Oct 11, 2024
1 parent c2e8f01 commit 431e3db
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
14 changes: 14 additions & 0 deletions packages/sdk-client/src/api/api-client-options-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { SinchClientParameters } from '../domain';
import { ApiClientOptions } from './api-client-options';
import {
ApiTokenRequest,
BasicAuthenticationRequest,
Oauth2TokenRequest,
SigningRequest,
XTimestampRequest,
Expand All @@ -20,6 +21,19 @@ export const buildOAuth2ApiClientOptions = (params: SinchClientParameters, apiNa
return apiClientOptions;
};

export const buildMailgunApiClientOptions = (params: SinchClientParameters): ApiClientOptions => {
if (!params.mailgunApiKey) {
throw new Error('Invalid configuration for the Mailgun API: the "mailgunApiKey" must be provided');
}
const apiClientOptions: ApiClientOptions = {
requestPlugins: [
new BasicAuthenticationRequest('api', params.mailgunApiKey),
],
};
addPlugins(apiClientOptions, params);
return apiClientOptions;
};

export const buildApplicationSignedApiClientOptions = (
params: SinchClientParameters, apiName: string,
): ApiClientOptions => {
Expand Down
1 change: 1 addition & 0 deletions packages/sdk-client/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export {
} from './api-client-options';
export {
buildOAuth2ApiClientOptions,
buildMailgunApiClientOptions,
buildApplicationSignedApiClientOptions,
buildFlexibleOAuth2OrApiTokenApiClientOptions,
} from './api-client-options-helper';
Expand Down
3 changes: 2 additions & 1 deletion packages/sdk-client/src/domain/domain-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ export const CONVERSATION_HOSTNAME = `https://${REGION_PATTERN}conversation.api.
export const CONVERSATION_TEMPLATES_HOSTNAME = `https://${REGION_PATTERN}template.api.sinch.com`;
export const ELASTIC_SIP_TRUNKING_HOSTNAME = 'https://elastic-trunking.api.sinch.com';
export const FAX_HOSTNAME = `https://${REGION_PATTERN}fax.api.sinch.com`;
export const MAILGUN_HOSTNAME = `https://api.${REGION_PATTERN}mailgun.net`;
export const NUMBERS_HOSTNAME = 'https://numbers.api.sinch.com';
export const SMS_HOSTNAME = `https://${REGION_PATTERN}sms.api.sinch.com`;;
export const SMS_HOSTNAME = `https://${REGION_PATTERN}sms.api.sinch.com`;
export const VERIFICATION_HOSTNAME = 'https://verification.api.sinch.com';
export const VOICE_HOSTNAME = `https://calling${REGION_PATTERN}.api.sinch.com`;
export const VOICE_APPLICATION_MANAGEMENT_HOSTNAME = 'https://callingapi.sinch.com';
Expand Down
35 changes: 29 additions & 6 deletions packages/sdk-client/src/domain/domain-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import { ResponsePlugin } from '../plugins/core/response-plugin';
* - API Token: SMS on all regions
* - Application Signed: Verification and Voice
*/
export interface SinchClientParameters extends
Partial<UnifiedCredentials>,
Partial<ServicePlanIdCredentials>,
Partial<ApplicationCredentials>,
ApiHostname,
ApiPlugins {}
export type SinchClientParameters = Partial<
UnifiedCredentials
& MailgunCredentials
& ServicePlanIdCredentials
& ApplicationCredentials
& ApiHostname
& ApiPlugins>;

export interface UnifiedCredentials {
/** The project ID associated with the API Client. You can find this on your [Dashboard](https://dashboard.sinch.com/account/access-keys). */
Expand All @@ -32,6 +33,13 @@ export interface UnifiedCredentials {
conversationRegion?: ConversationRegion;
}

export interface MailgunCredentials {
/** Your API Key created from the [Mailgun Dashboard](https://app.mailgun.com/settings/api_security) */
mailgunApiKey: string;
/** The region for the Mailgun API. Default region is empty */
mailgunRegion?: string;
}

export interface ServicePlanIdCredentials {
/** Your service plan ID. You can find this on your [Dashboard](https://dashboard.sinch.com/sms/api/rest). */
servicePlanId: string;
Expand Down Expand Up @@ -63,6 +71,8 @@ export interface ApiHostname {
elasticSipTrunkingHostname?: string;
/** Override the hostname for the Fax API - Note the regions become ineffective */
faxHostname?: string;
/** Override the hostname for the Mailgun API - Note the regions become ineffective */
mailgunHostname?: string;
/** Override the hostname for the Numbers API */
numbersHostname?: string;
/** Override the hostname for the SMS API - Note the regions become ineffective */
Expand Down Expand Up @@ -158,3 +168,16 @@ export type ConversationRegion = SupportedConversationRegion | string;
export const ConversationRegion = {
...SupportedConversationRegion,
};

// ////////////////////
// Mailgun regions
export enum SupportedMailgunRegion {
DEFAULT = '',
EUROPE = 'eu',
}

export type MailgunRegion = SupportedMailgunRegion | string;

export const MailgunRegion = {
...SupportedMailgunRegion,
};
4 changes: 4 additions & 0 deletions packages/sdk-client/tests/domain/domain-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
VERIFICATION_HOSTNAME,
VOICE_APPLICATION_MANAGEMENT_HOSTNAME,
VOICE_HOSTNAME,
MAILGUN_HOSTNAME,
} from '../../src';

describe('Domain Helper', () => {
Expand All @@ -29,6 +30,9 @@ describe('Domain Helper', () => {

const formattedVoiceHostname = formatRegionalizedHostname(VOICE_HOSTNAME, '-bzh');
expect(formattedVoiceHostname).toBe('https://calling-bzh.api.sinch.com');

const formattedMailgunHostname = formatRegionalizedHostname(MAILGUN_HOSTNAME, 'bzh.');
expect(formattedMailgunHostname).toBe('https://api.bzh.mailgun.net');
});

it('should leave the hostname untouched', () => {
Expand Down

0 comments on commit 431e3db

Please sign in to comment.