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: updates import resolution #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions functions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
"moment": "^2.24.0"
},
"devDependencies": {
"tslint": "^5.12.0",
"typescript": "^3.2.2",
"firebase-functions-test": "^0.1.6",
"mocha": "^7.0.2",
"@types/chai": "^4.2.0",
"@types/mocha": "^7.0.2",
"chai": "^4.2.0",
"@types/chai": "^4.2.0",
"ts-node": "^8.8.2"
"firebase-functions-test": "^0.1.6",
"mocha": "^7.0.2",
"ts-node": "^8.8.2",
"tslib": "^1.11.1",
"tslint": "^5.12.0",
"typescript": "^3.2.2"
},
"private": true
}
10 changes: 4 additions & 6 deletions functions/src/config.example.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import FunctionConfig from "./opentrace/types/FunctionConfig";
import Authenticator from "./opentrace/utils/Authenticator";
import PinGenerator from "./opentrace/utils/PinGenerator";
import {FunctionConfig} from "./opentrace/types/FunctionConfig";
import {Authenticator} from "./opentrace/utils/Authenticator";
import {PinGenerator} from "./opentrace/utils/PinGenerator";

const config: FunctionConfig = {
export const config: FunctionConfig = {
projectId: "",
regions: [],
utcOffset: 0,
Expand All @@ -26,5 +26,3 @@ const config: FunctionConfig = {
bucketForArchive: "archive-bucket",
},
};

export default config;
4 changes: 2 additions & 2 deletions functions/src/firebaseFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as functions from "firebase-functions";
import functions from "firebase-functions";

import config from "./config";
import {config} from "./config";

/**
* Wrapper around functions to handle authentication
Expand Down
12 changes: 6 additions & 6 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as admin from "firebase-admin";
import admin from "firebase-admin";
admin.initializeApp();

import * as firebaseFunctions from "./firebaseFunctions";
import config from "./config";
import {config} from "./config";

import getHandshakePin from "./opentrace/getHandshakePin";
import getTempIDs from "./opentrace/getTempIDs";
import getUploadToken from "./opentrace/getUploadToken";
import processUploadedData from "./opentrace/processUploadedData";
import {getHandshakePin} from "./opentrace/getHandshakePin";
import {getTempIDs} from "./opentrace/getTempIDs";
import {getUploadToken} from "./opentrace/getUploadToken";
import {processUploadedData} from "./opentrace/processUploadedData";

exports.getHandshakePin = firebaseFunctions.https(getHandshakePin);
exports.getTempIDs = firebaseFunctions.https(getTempIDs);
Expand Down
9 changes: 3 additions & 6 deletions functions/src/opentrace/getHandshakePin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import * as functions from "firebase-functions";

import config from "../config";
import functions from "firebase-functions";
import {config} from "../config";

/**
* Get the handshake pin for a user.
*/
const getHandshakePin = async (uid: string) => {
export const getHandshakePin = async (uid: string) => {
console.log('getHandshakePin:', 'uid', uid);

try {
Expand All @@ -20,5 +19,3 @@ const getHandshakePin = async (uid: string) => {
};

export const getUserHandshakePin = config.upload.pinGenerator.generatePin;

export default getHandshakePin;
12 changes: 5 additions & 7 deletions functions/src/opentrace/getTempIDs.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as moment from "moment";
import moment from "moment";

import config from "../config";
import CustomEncrypter from "./utils/CustomEncrypter";
import getEncryptionKey from "./utils/getEncryptionKey";
import {config} from "../config";
import {CustomEncrypter} from "./utils/CustomEncrypter";
import {getEncryptionKey} from "./utils/getEncryptionKey";

const UID_SIZE = 21;
const TIME_SIZE = 4;
Expand All @@ -11,7 +11,7 @@ const TEMPID_SIZE = UID_SIZE + TIME_SIZE * 2;
const IV_SIZE = 16;
const AUTHTAG_SIZE = 16;

const getTempIDs = async (uid: string) => {
export const getTempIDs = async (uid: string) => {
console.log('getTempIDs:', 'uid', uid);

const encryptionKey = await getEncryptionKey();
Expand Down Expand Up @@ -76,5 +76,3 @@ export function decryptTempID(tempID: string, encryptionKey: Buffer): { uid: str
expiryTime: expiryTime
};
}

export default getTempIDs;
16 changes: 7 additions & 9 deletions functions/src/opentrace/getUploadToken.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import functions from "firebase-functions";
import admin from "firebase-admin";

import config from "../config";
import getEncryptionKey from "./utils/getEncryptionKey";
import CustomEncrypter from "./utils/CustomEncrypter";
import formatTimestamp from "./utils/formatTimestamp";
import {config} from "../config";
import {getEncryptionKey} from "./utils/getEncryptionKey";
import {CustomEncrypter} from "./utils/CustomEncrypter";
import {formatTimestamp} from "./utils/formatTimestamp";

/**
* Get upload token by passing in a secret string as `data`
*/
const getUploadToken = async (uid: string, data: any, context: functions.https.CallableContext) => {
export const getUploadToken = async (uid: string, data: any, context: functions.https.CallableContext) => {
console.log('getUploadToken:', 'uid', uid, 'data', data, 'ip', context.rawRequest.ip);

let valid = false;
Expand Down Expand Up @@ -109,5 +109,3 @@ export function validateToken(token: string, encryptionKey: Buffer, validateToke

return {uid: uid, uploadCode: upload};
}

export default getUploadToken;
6 changes: 2 additions & 4 deletions functions/src/opentrace/processUploadedData.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as functions from "firebase-functions";
import functions from "firebase-functions";
import {ObjectMetadata} from "firebase-functions/lib/providers/storage";

const processUploadedData = async (object: ObjectMetadata) => {
export const processUploadedData = async (object: ObjectMetadata) => {
throw new functions.https.HttpsError('unimplemented', 'Not implemented yet');
};

export default processUploadedData;
8 changes: 3 additions & 5 deletions functions/src/opentrace/types/FunctionConfig.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Authenticator from "../utils/Authenticator";
import PinGenerator from "../utils/PinGenerator";
import {Authenticator} from "../utils/Authenticator";
import {PinGenerator} from "../utils/PinGenerator";

// SUPPORTED_REGIONS from function-configuration.d.ts
declare type SUPPORTED_REGIONS = "us-central1" | "us-east1" | "us-east4" | "europe-west1" | "europe-west2" | "asia-east2" | "asia-northeast1";

interface FunctionConfig {
export interface FunctionConfig {
projectId: string // Firebase Project ID
regions: SUPPORTED_REGIONS[]
utcOffset: number | string
Expand All @@ -28,5 +28,3 @@ interface FunctionConfig {
bucketForArchive: string
}
}

export default FunctionConfig;
4 changes: 1 addition & 3 deletions functions/src/opentrace/types/HeartBeatEvent.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
interface HeartBeatEvent {
export interface HeartBeatEvent {
timestamp: number,
msg?: string,
// enhanced fields:
timestampString?: string,
}

export default HeartBeatEvent;
4 changes: 1 addition & 3 deletions functions/src/opentrace/types/StreetPassRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Transmission Power txPower
* Organization org
*/
interface StreetPassRecord {
export interface StreetPassRecord {
timestamp: number,
msg?: string,
modelC: string,
Expand All @@ -24,5 +24,3 @@ interface StreetPassRecord {
contactIdValidFrom?: number,
contactIdValidTo?: number,
}

export default StreetPassRecord;
4 changes: 2 additions & 2 deletions functions/src/opentrace/utils/Authenticator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as functions from "firebase-functions";
import functions from "firebase-functions";
import {CallableContext} from "firebase-functions/lib/providers/https";

export default class Authenticator {
export class Authenticator {
async authenticate(data: any, context: CallableContext): Promise<string> {
if (!context.auth) {
throw new functions.https.HttpsError('unauthenticated', 'The function must be called while authenticated.');
Expand Down
8 changes: 3 additions & 5 deletions functions/src/opentrace/utils/CustomEncrypter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as crypto from "crypto";
import config from "../../config";
import crypto from "crypto";
import {config} from "../../config";

class CustomEncrypter {
export class CustomEncrypter {

algorithm: string;
key: Buffer;
Expand Down Expand Up @@ -71,5 +71,3 @@ class CustomEncrypter {
return this.decrypt(decodedCipherTextB64, decodedIvB64, decodedAuthTagB64);
}
}

export default CustomEncrypter;
2 changes: 1 addition & 1 deletion functions/src/opentrace/utils/PinGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* This class uses a plain substring to generate a pin from user uid.
* It should be subclassed with a secure implementation.
*/
export default class PinGenerator {
export class PinGenerator {
async generatePin(uid: string): Promise<string> {
return uid.substring(0, 6).toUpperCase();
}
Expand Down
8 changes: 3 additions & 5 deletions functions/src/opentrace/utils/formatTimestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
* Convert timestamp (expressed in seconds since the Epoch) to "DD-MMM-YYYY HH:mm:ss Z" format
* @param timestamp
*/
import * as moment from "moment";
import moment from "moment";

import config from "../../config";
import {config} from "../../config";

const TIMESTAMP_FORMAT = "DD-MMM-YYYY HH:mm:ss Z";

function formatTimestamp(timestamp: number) {
export function formatTimestamp(timestamp: number) {
return moment.unix(timestamp).utcOffset(config.utcOffset).format(TIMESTAMP_FORMAT);
}

export default formatTimestamp;
8 changes: 3 additions & 5 deletions functions/src/opentrace/utils/getEncryptionKey.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {SecretManagerServiceClient} from "@google-cloud/secret-manager";

import config from "../../config";
import {config} from "../../config";

const SECRET_KEY = `projects/${config.projectId}/secrets/${config.encryption.keyPath}`;
const SECRET_KEY_DEFAULT_VERSION = `${SECRET_KEY}/versions/${config.encryption.defaultVersion}`;

const getEncryptionKey = async (): Promise<Buffer> => getEncryptionSecret(SECRET_KEY_DEFAULT_VERSION);
export const getEncryptionKey = async (): Promise<Buffer> => getEncryptionSecret(SECRET_KEY_DEFAULT_VERSION);

export const getAllEncryptionKeys = async (): Promise<Buffer[]> => {
const secretManagerClient = new SecretManagerServiceClient();
Expand All @@ -25,7 +25,7 @@ export const getAllEncryptionKeys = async (): Promise<Buffer[]> => {

const isDefaultKey = (_: string) => SECRET_KEY_DEFAULT_VERSION.substring(SECRET_KEY_DEFAULT_VERSION.length - 2) === _.substring(_.length - 2);

async function getEncryptionSecret(keyPathIncludingVersion: string): Promise<Buffer> {
export async function getEncryptionSecret(keyPathIncludingVersion: string): Promise<Buffer> {
const secretManagerClient = new SecretManagerServiceClient();

console.log("getEncryptionSecret:", `Getting encryption key: ${keyPathIncludingVersion}`);
Expand All @@ -36,5 +36,3 @@ async function getEncryptionSecret(keyPathIncludingVersion: string): Promise<Buf
// @ts-ignore
return Buffer.from(secret.payload.data.toString(), 'base64');
}

export default getEncryptionKey;
4 changes: 2 additions & 2 deletions functions/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as admin from "firebase-admin";
import config from "../src/config";
import admin from "firebase-admin";
import {config} from "../src/config";

if (!process.env.GOOGLE_APPLICATION_CREDENTIALS) {
throw new Error("Environment variable GOOGLE_APPLICATION_CREDENTIALS is required to access Firebase. Refer to: https://cloud.google.com/docs/authentication/production ");
Expand Down
16 changes: 8 additions & 8 deletions functions/test/opentrace/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as admin from "firebase-admin";
import * as functions from "firebase-functions";
import * as chai from "chai";
import * as crypto from "crypto";
import admin from "firebase-admin";
import functions from "firebase-functions";
import chai from "chai";
import crypto from "crypto";

import config from "../../src/config";
import {config} from "../../src/config";
import {FunctionsTestWrapper} from "../index.test";
import getEncryptionKey from "../../src/opentrace/utils/getEncryptionKey";
import {getEncryptionKey} from "../../src/opentrace/utils/getEncryptionKey";

describe('config.ts', function () {
describe('#encryption', function () {
Expand Down Expand Up @@ -48,10 +48,10 @@ describe('config.ts', function () {
const uid = crypto.randomBytes(28).toString("utf-8");

return config.upload.pinGenerator.generatePin(uid)
.catch(error => {
.catch((error: any) => {
chai.assert(false, `generatePin throws error: ${error.message}`);
})
.then(async pin => {
.then(async (pin: any) => {
chai.assert(pin == await config.upload.pinGenerator.generatePin(uid),
"Pin is not consistent between different calls");
});
Expand Down
8 changes: 4 additions & 4 deletions functions/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"target": "es2017"
"target": "es2017",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"importHelpers": true
},
"compileOnSave": true,
"include": [
"src"
]
}