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: expose OAuth capability #144

Merged
merged 1 commit into from
Jan 14, 2024
Merged
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
6,009 changes: 1,642 additions & 4,367 deletions client/functions/package-lock.json

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions client/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@
"scripts": {
"lint": "eslint \"src/**/*\"",
"build": "tsc",
"dev": "tsc -w",
"serve": "npm run build && firebase emulators:start --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "16"
"node": "18"
},
"main": "lib/index.js",
"main": "lib/functions/src/index.js",
"dependencies": {
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"cors": "^2.8.5",
"express": "^4.17.2",
"express-jwt": "^6.1.0",
"firebase-admin": "^10.0.2",
"firebase-functions": "^3.16.0",
"express-unless": "^2.1.3",
"firebase-admin": "^11.5.0",
"firebase-functions": "^4.2.0",
"jwks-rsa": "^1.12.3",
"lodash": "^4.17.21"
},
Expand All @@ -30,7 +32,7 @@
"eslint": "^6.8.0",
"eslint-plugin-import": "^2.25.3",
"firebase-functions-test": "^0.2.0",
"typescript": "^3.9.10"
"typescript": "^5.1.6"
},
"private": true
}
30 changes: 27 additions & 3 deletions client/functions/src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import {RequestHandler} from "express";
import {Auth} from "firebase-admin/lib/auth";
import {RequestHandler, Request, Response} from "express";
import {Auth} from "firebase-admin/auth";
import axios from 'axios';

const {createHash} = require('crypto');

const AUTHORIZATION_URL = 'https://gainstrack.jp.auth0.com/authorize'
const TOKEN_URL = 'https://gainstrack.jp.auth0.com/oauth/token'

function javaHash(input: string) {
const md5Bytes = createHash('md5').update(input).digest();
md5Bytes[6] &= 0x0f; /* clear version */
Expand All @@ -14,7 +18,6 @@ function javaHash(input: string) {
return uuid;
}


export const firebaseHandler: ((fbauth:Auth) => RequestHandler)= (fbauth) => async (req, res) => {
// Create UID from authenticated Auth0 user
// @ts-ignore
Expand All @@ -34,3 +37,24 @@ export const firebaseHandler: ((fbauth:Auth) => RequestHandler)= (fbauth) => as
})
}
};

/// Middleware to redirect to the Auth0 authorization endpoint
export const authHandler: RequestHandler = (req, res) => {
// Construct the full URL for the Auth0 authorize endpoint with the query parameters
const auth0AuthorizeUrl = `${AUTHORIZATION_URL}?${req.url.split('?')[1]}`;

// Redirect the client to the constructed URL
res.redirect(auth0AuthorizeUrl);
};

// Middleware to proxy the token request to Auth0
export const tokenHandler: RequestHandler = async (req, res) => {
try {
const form_data = req.body;
const auth0Response = await axios.post(TOKEN_URL, form_data);

res.json(auth0Response.data);
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
}
};
11 changes: 7 additions & 4 deletions client/functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as functions from 'firebase-functions';
import {firebaseHandler} from "./auth";
import {authHandler, firebaseHandler, tokenHandler} from './auth';
import {quoteSourcesHandler, quoteSourcesTableHandler} from "./queries";
import * as cors from 'cors';
import {Request} from "firebase-functions/lib/providers/https";
import cors from 'cors';
import {Request} from "firebase-functions/lib/v1/providers/https";
import {quoteSourceHistoryCreateHandler} from "./writes";

const admin = require('firebase-admin');
Expand Down Expand Up @@ -112,7 +112,10 @@ const app = express();

app.post('/firebase', jwtCheck, firebaseHandler(admin.auth()));
app.post('/functions/auth/firebase', jwtCheck, firebaseHandler(admin.auth()));

app.get('/authorize', authHandler);
app.post('/token', tokenHandler);
app.post('/functions/auth/token', tokenHandler);
app.get('/functions/auth/authorize', authHandler);

export const auth = functions
// .region('asia-northeast1')
Expand Down
2 changes: 1 addition & 1 deletion client/functions/src/queries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Request} from "firebase-functions/lib/v1/providers/https";
import {Response} from "express";
import {Request} from "firebase-functions/lib/providers/https";

import {quoteSourceFieldProperties} from '../../src/lib/assetdb/AssetSchema';
import {get} from 'lodash';
Expand Down
4 changes: 2 additions & 2 deletions client/functions/src/writes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {QueryDocumentSnapshot} from "firebase-functions/lib/providers/firestore";
import {EventContext} from "firebase-functions";
import {QueryDocumentSnapshot} from 'firebase-admin/firestore';

export let quoteSourceHistoryCreateHandler = (firestore: FirebaseFirestore.Firestore) =>async (snap: QueryDocumentSnapshot, context: EventContext) => {
// Get an object representing the document
Expand Down Expand Up @@ -37,7 +37,7 @@ export let quoteSourceHistoryCreateHandler = (firestore: FirebaseFirestore.Fires

await firestore.collection('quoteSources').doc(id).set(toSave);
}
} catch (e) {
} catch (e:any) {
console.error(e);
snapData.error = e.toString()
await firestore.collection('quoteSourceErrors').add(snapData);
Expand Down
7 changes: 5 additions & 2 deletions client/functions/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
"compilerOptions": {
"module": "commonjs",
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedLocals": false,
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "es2017"
"target": "es2021",
"moduleResolution": "node",
"esModuleInterop": true,
"skipLibCheck": true
},
"compileOnSave": true,
"include": [
Expand Down
Loading