Skip to content
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

feat(cli): add @coveo/search-token-server package #33

Merged
merged 13 commits into from
Feb 17, 2021
8 changes: 8 additions & 0 deletions packages/search-token-server/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Coveo related configuration
COVEO_PLATFORM_HOSTNAME=<PLATFORM_HOSTNAME dev,qa,prod,hippa>
y-lakhdar marked this conversation as resolved.
Show resolved Hide resolved
# for example https://platform.cloud.coveo.com
Copy link
Member

@olamothe olamothe Feb 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just me, but I would put the comment above the line it explains ;)

And a comment that says to look at README.

In general, don't be afraid to put quite a bit of explanation about what everything is with a lot of detail.

Think from the point of view from a total newbie that just started to play with Coveo interacting through the CLI.

They need explanation about what are the valid platform endpoint, what is an API key/where to see them in your organization, the privileges required, what is a search hub etc..

All of these are well explained in our documentation, so try to always link to an article that would help a total newbie to roughly understand what's going on.

Without that, it is all very cryptic. This module/package can be seen as a good way to teach.

y-lakhdar marked this conversation as resolved.
Show resolved Hide resolved
y-lakhdar marked this conversation as resolved.
Show resolved Hide resolved
COVEO_API_KEY=<YOUR_COVEO_API_KEY_HERE>
y-lakhdar marked this conversation as resolved.
Show resolved Hide resolved
USER_EMAIL=<YOUR_EMAIL>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:
The name of the security identity to impersonate.
Example: "[email protected]"
See https://docs.coveo.com/en/56/#name-string-required.

(If I understand correctly)


# Optional variables
# SEARCH_HUB=<YOUR_SEARCH_HUB>
y-lakhdar marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions packages/search-token-server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
y-lakhdar marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 7 additions & 0 deletions packages/search-token-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Simple search token generation server

## Setup environment

Create the `.env` file at the root of this project using `.env.example` as starting point and make sure to replace all emptied variables `<...>` by the proper information of your organization.
y-lakhdar marked this conversation as resolved.
Show resolved Hide resolved

For more info, https://docs.coveo.com/en/56/build-a-search-ui/search-token-authentication
y-lakhdar marked this conversation as resolved.
Show resolved Hide resolved
23 changes: 23 additions & 0 deletions packages/search-token-server/middlewares/searchToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {NextFunction} from 'express';
import {getSearchToken} from '../utils/cloudPlatformAPI';

export function ensureTokenGenerated(req: any, res: any, next: NextFunction) {
const userIds = [process.env.USER_EMAIL!].map((user) => ({
y-lakhdar marked this conversation as resolved.
Show resolved Hide resolved
name: user,
provider: 'Email Security Provider',
}));

getSearchToken({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use an async await to 'flatten' the code personnaly

hostname: process.env.COVEO_PLATFORM_HOSTNAME!,
apiKey: process.env.COVEO_API_KEY!,
searchHub: process.env.SEARCH_HUB,
userIds: userIds,
})
.then((data: any) => {
req.token = data.token;
next();
})
.catch((err: any) => {
next(err);
});
}
21 changes: 21 additions & 0 deletions packages/search-token-server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@coveo/search-token-server",
"author": "Coveo",
"version": "0.0.0",
"main": "server.ts",
"license": "Apache-2.0",
"dependencies": {
"dotenv": "^8.2.0",
"express": "^4.17.1",
"node-fetch": "^2.6.1",
"ts-node": "^9.1.1",
"typescript": "^4.1.5"
},
"scripts": {
"serve": "ts-node server.ts"
},
"devDependencies": {
"@types/express": "^4.17.11",
"@types/node-fetch": "^2.5.8"
}
}
26 changes: 26 additions & 0 deletions packages/search-token-server/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* eslint-disable @typescript-eslint/no-namespace */
import * as express from 'express';
import {ensureTokenGenerated} from './middlewares/searchToken';
import {config} from 'dotenv';
config();

const app = express();

declare global {
namespace Express {
interface Request {
token: string;
}
}
}

app.use(express.json());

app.get('/token', ensureTokenGenerated, (req, res) => {
res.send({token: req.token});
});

// start our server on port 3000
app.listen(3000, '127.0.0.1', () => {
console.log('Server now listening on 3000');
});
14 changes: 14 additions & 0 deletions packages/search-token-server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
y-lakhdar marked this conversation as resolved.
Show resolved Hide resolved
"compilerOptions": {
"experimentalDecorators": true,
"declaration": true,
"importHelpers": true,
"module": "commonjs",
"outDir": "lib",
"rootDir": "src",
"strict": true,
"target": "es2017",
"noEmitOnError": false
},
"include": ["src/**/*"]
}
24 changes: 24 additions & 0 deletions packages/search-token-server/utils/cloudPlatformAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import fetch, {Response} from 'node-fetch';

export function getSearchToken(options: {
y-lakhdar marked this conversation as resolved.
Show resolved Hide resolved
userIds: {name: string; provider: string}[];
hostname: string;
apiKey: string;
searchHub?: string;
}) {
const body: {[id: string]: any} = {
userIds: options.userIds,
};
if (options.searchHub) {
body.searchHub = options.searchHub;
}

return fetch(`${options.hostname}/rest/search/token`, {
method: 'post',
body: JSON.stringify(body),
headers: {
Authorization: `Bearer ${options.apiKey}`,
'Content-Type': 'application/json',
},
}).then((res: Response) => res.json());
}