Skip to content

Commit

Permalink
feat: use api-key for symphony if provided (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
birme authored Jun 10, 2024
1 parent 7641d69 commit 1cfb980
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 17 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Intercom solution powered by Symphony Media Bridge. This is the Intercom manager
| --------------------------- | --------------------------------------------------------------------------------- |
| `PORT` | Intercom-Manager API port |
| `SMB_ADDRESS` | The address:port of the Symphony Media Bridge instance |
| `SMB_APIKEY` | When set, provide this API key for the Symphony Media Bridge (optional) |
| `MONGODB_CONNECTION_STRING` | MongoDB connection string (default: `mongodb://localhost:27017/intercom-manager`) |

## Installation / Usage
Expand Down
4 changes: 3 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface ApiOptions {
title: string;
smbServerBaseUrl: string;
endpointIdleTimeout: string;
smbServerApiKey?: string;
}

export default async (opts: ApiOptions) => {
Expand Down Expand Up @@ -70,7 +71,8 @@ export default async (opts: ApiOptions) => {
api.register(await getApiProductions(), {
prefix: 'api/v1',
smbServerBaseUrl: opts.smbServerBaseUrl,
endpointIdleTimeout: opts.endpointIdleTimeout
endpointIdleTimeout: opts.endpointIdleTimeout,
smbServerApiKey: opts.smbServerApiKey
});

return api;
Expand Down
5 changes: 5 additions & 0 deletions src/api_productions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function checkUserStatus() {
export interface ApiProductionsOptions {
smbServerBaseUrl: string;
endpointIdleTimeout: string;
smbServerApiKey?: string;
}

const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
Expand All @@ -43,6 +44,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
opts.smbServerBaseUrl
).toString();
const smb = new SmbProtocol();
const smbServerApiKey = opts.smbServerApiKey || '';

fastify.post<{
Body: NewProduction;
Expand Down Expand Up @@ -236,6 +238,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
const smbConferenceId = await coreFunctions.createConferenceForLine(
smb,
smbServerUrl,
smbServerApiKey,
productionId,
lineId
);
Expand All @@ -244,6 +247,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
const endpoint = await coreFunctions.createEndpoint(
smb,
smbServerUrl,
smbServerApiKey,
smbConferenceId,
endpointId,
true,
Expand Down Expand Up @@ -330,6 +334,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
await coreFunctions.handleAnswerRequest(
smb,
smbServerUrl,
smbServerApiKey,
line.smbConferenceId,
endpointId,
connectionEndpointDescription,
Expand Down
28 changes: 23 additions & 5 deletions src/api_productions_core_functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export class CoreFunctions {
async createEndpoint(
smb: SmbProtocol,
smbServerUrl: string,
smbServerApiKey: string,
lineId: string,
endpointId: string,
audio: boolean,
Expand All @@ -88,14 +89,16 @@ export class CoreFunctions {
endpointId,
audio,
data,
endpointIdleTimeout
endpointIdleTimeout,
smbServerApiKey
);
return endpoint;
}

async handleAnswerRequest(
smb: SmbProtocol,
smbServerUrl: string,
smbServerApiKey: string,
lineId: string,
endpointId: string,
endpointDescription: SmbEndpointDescription,
Expand Down Expand Up @@ -184,7 +187,8 @@ export class CoreFunctions {
smbServerUrl,
lineId,
endpointId,
endpointDescription
endpointDescription,
smbServerApiKey
);
}

Expand All @@ -198,10 +202,14 @@ export class CoreFunctions {
private async createConference(
smb: SmbProtocol,
smbServerUrl: string,
smbServerApiKey: string,
productionId: string,
lineId: string
): Promise<string> {
const activeLines: string[] = await smb.getConferences(smbServerUrl);
const activeLines: string[] = await smb.getConferences(
smbServerUrl,
smbServerApiKey
);

const production = await this.productionManager.requireProduction(
parseInt(productionId, 10)
Expand All @@ -213,7 +221,10 @@ export class CoreFunctions {
return line.smbConferenceId;
}

const newConferenceId = await smb.allocateConference(smbServerUrl);
const newConferenceId = await smb.allocateConference(
smbServerUrl,
smbServerApiKey
);

if (
!(await this.productionManager.setLineId(
Expand All @@ -233,11 +244,18 @@ export class CoreFunctions {
async createConferenceForLine(
smb: SmbProtocol,
smbServerUrl: string,
smbServerApiKey: string,
productionId: string,
lineId: string
): Promise<string> {
const createConf = () =>
this.createConference(smb, smbServerUrl, productionId, lineId);
this.createConference(
smb,
smbServerUrl,
smbServerApiKey,
productionId,
lineId
);

return this.connectionQueue.queueAsync(createConf);
}
Expand Down
3 changes: 2 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const PORT = process.env.PORT ? Number(process.env.PORT) : 8000;
const server = await api({
title: 'intercom-manager',
smbServerBaseUrl: SMB_ADDRESS,
endpointIdleTimeout: ENDPOINT_IDLE_TIMEOUT_S
endpointIdleTimeout: ENDPOINT_IDLE_TIMEOUT_S,
smbServerApiKey: process.env.SMB_APIKEY
});

server.listen({ port: PORT, host: '0.0.0.0' }, (err, address) => {
Expand Down
32 changes: 22 additions & 10 deletions src/smb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ interface BaseAllocationRequest {
}

export class SmbProtocol {
async allocateConference(smbUrl: string): Promise<string> {
async allocateConference(smbUrl: string, smbKey: string): Promise<string> {
const allocateResponse = await fetch(smbUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
'Content-Type': 'application/json',
...(smbKey !== '' && { Authorization: `Bearer ${smbKey}` })
},
body: '{}'
});
Expand All @@ -46,7 +47,8 @@ export class SmbProtocol {
endpointId: string,
audio: boolean,
data: boolean,
idleTimeout: number
idleTimeout: number,
smbKey: string
): Promise<SmbEndpointDescription> {
const request: BaseAllocationRequest = {
action: 'allocate',
Expand Down Expand Up @@ -74,7 +76,8 @@ export class SmbProtocol {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
'Content-Type': 'application/json',
...(smbKey !== '' && { Authorization: `Bearer ${smbKey}` })
},
body: JSON.stringify(request)
});
Expand All @@ -96,15 +99,17 @@ export class SmbProtocol {
smbUrl: string,
conferenceId: string,
endpointId: string,
endpointDescription: SmbEndpointDescription
endpointDescription: SmbEndpointDescription,
smbKey: string
): Promise<void> {
const request = JSON.parse(JSON.stringify(endpointDescription));
request['action'] = 'configure';
const url = smbUrl + conferenceId + '/' + endpointId;
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
'Content-Type': 'application/json',
...(smbKey !== '' && { Authorization: `Bearer ${smbKey}` })
},
body: JSON.stringify(request)
});
Expand All @@ -130,9 +135,12 @@ export class SmbProtocol {
}
}

async getConferences(smbUrl: string): Promise<string[]> {
async getConferences(smbUrl: string, smbKey: string): Promise<string[]> {
const response = await fetch(smbUrl, {
method: 'GET'
method: 'GET',
headers: {
...(smbKey !== '' && { Authorization: `Bearer ${smbKey}` })
}
});

if (!response.ok) {
Expand All @@ -145,11 +153,15 @@ export class SmbProtocol {

async getConference(
smbUrl: string,
conferenceId: string
conferenceId: string,
smbKey: string
): Promise<DetailedConference[]> {
const url = smbUrl + conferenceId;
const response = await fetch(url, {
method: 'GET'
method: 'GET',
headers: {
...(smbKey !== '' && { Authorization: `Bearer ${smbKey}` })
}
});

if (!response.ok) {
Expand Down

0 comments on commit 1cfb980

Please sign in to comment.