Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Commit

Permalink
feat: set up function
Browse files Browse the repository at this point in the history
  • Loading branch information
hetd54 committed Sep 17, 2024
1 parent fcef739 commit 5916f64
Show file tree
Hide file tree
Showing 10 changed files with 8,264 additions and 14 deletions.
18 changes: 17 additions & 1 deletion firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,21 @@
"auth": {
"port": 9099
}
}
},
"functions": [
{
"source": "functions",
"codebase": "default",
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log",
"*.local"
],
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
]
}
]
}
28 changes: 28 additions & 0 deletions functions/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = {
env: {
es6: true,
node: true,
},
parserOptions: {
"ecmaVersion": 2018,
},
extends: [
"eslint:recommended",
"google",
],
rules: {
"no-restricted-globals": ["error", "name", "length"],
"prefer-arrow-callback": "error",
"quotes": ["error", "double", {"allowTemplateLiterals": true}],
},
overrides: [
{
files: ["**/*.spec.*"],
env: {
mocha: true,
},
rules: {},
},
],
globals: {},
};
87 changes: 87 additions & 0 deletions functions/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"parserOptions": {
// Required for certain syntax usages
"ecmaVersion": 6
},
"plugins": [
"promise"
],
"extends": "eslint:recommended",
"rules": {
// Removed rule "disallow the use of console" from recommended eslint rules
"no-console": "off",
// Removed rule "disallow multiple spaces in regular expressions" from recommended eslint rules
"no-regex-spaces": "off",
// Removed rule "disallow the use of debugger" from recommended eslint rules
"no-debugger": "off",
// Removed rule "disallow unused variables" from recommended eslint rules
"no-unused-vars": "off",
// Removed rule "disallow mixed spaces and tabs for indentation" from recommended eslint rules
"no-mixed-spaces-and-tabs": "off",
// Removed rule "disallow the use of undeclared variables unless mentioned in /*global */ comments" from recommended eslint rules
"no-undef": "off",
// Warn against template literal placeholder syntax in regular strings
"no-template-curly-in-string": 1,
// Warn if return statements do not either always or never specify values
"consistent-return": 1,
// Warn if no return statements in callbacks of array methods
"array-callback-return": 1,
// Require the use of === and !==
"eqeqeq": 2,
// Disallow the use of alert, confirm, and prompt
"no-alert": 2,
// Disallow the use of arguments.caller or arguments.callee
"no-caller": 2,
// Disallow null comparisons without type-checking operators
"no-eq-null": 2,
// Disallow the use of eval()
"no-eval": 2,
// Warn against extending native types
"no-extend-native": 1,
// Warn against unnecessary calls to .bind()
"no-extra-bind": 1,
// Warn against unnecessary labels
"no-extra-label": 1,
// Disallow leading or trailing decimal points in numeric literals
"no-floating-decimal": 2,
// Warn against shorthand type conversions
"no-implicit-coercion": 1,
// Warn against function declarations and expressions inside loop statements
"no-loop-func": 1,
// Disallow new operators with the Function object
"no-new-func": 2,
// Warn against new operators with the String, Number, and Boolean objects
"no-new-wrappers": 1,
// Disallow throwing literals as exceptions
"no-throw-literal": 2,
// Require using Error objects as Promise rejection reasons
"prefer-promise-reject-errors": 2,
// Enforce “for” loop update clause moving the counter in the right direction
"for-direction": 2,
// Enforce return statements in getters
"getter-return": 2,
// Disallow await inside of loops
"no-await-in-loop": 2,
// Disallow comparing against -0
"no-compare-neg-zero": 2,
// Warn against catch clause parameters from shadowing variables in the outer scope
"no-catch-shadow": 1,
// Disallow identifiers from shadowing restricted names
"no-shadow-restricted-names": 2,
// Enforce return statements in callbacks of array methods
"callback-return": 2,
// Require error handling in callbacks
"handle-callback-err": 2,
// Warn against string concatenation with __dirname and __filename
"no-path-concat": 1,
// Prefer using arrow functions for callbacks
"prefer-arrow-callback": 1,
// Return inside each then() to create readable and reusable Promise chains.
// Forces developers to return console logs and http calls in promises.
"promise/always-return": 2,
//Enforces the use of catch() on un-returned promises
"promise/catch-or-return": 2,
// Warn against nested then() or catch() statements
"promise/no-nesting": 1
}
}
2 changes: 2 additions & 0 deletions functions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
*.local
97 changes: 97 additions & 0 deletions functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
Based on implementation from https://github.com/Herohtar/netlify-cms-oauth-firebase/tree/master
*/

// eslint-disable-next-line @typescript-eslint/no-var-requires
const functions = require("firebase-functions");
// eslint-disable-next-line @typescript-eslint/no-var-requires
const express = require("express");
// eslint-disable-next-line @typescript-eslint/no-var-requires
const simpleOauth = require("simple-oauth2");
// eslint-disable-next-line @typescript-eslint/no-var-requires
const randomstring = require("randomstring");

const oauth = functions.config().oauth;
const oauthProvider = oauth.provider || "github";

// eslint-disable-next-line require-jsdoc
function getScript(mess, content) {
return `<!doctype html><html><body><script>
(function() {
function receiveMessage(e) {
console.log("receiveMessage %o", e)
window.opener.postMessage(
'authorization:github:${mess}:${JSON.stringify(content)}',
e.origin
)
window.removeEventListener("message",receiveMessage,false);
}
window.addEventListener("message", receiveMessage, false)
console.log("Sending message: %o", "github")
window.opener.postMessage("authorizing:github", "*")
})()
</script></body></html>`;
}

const oauth2 = simpleOauth.create({
client: {
id: oauth.client_id,
secret: oauth.client_secret,
},
auth: {
tokenHost: oauth.git_hostname || "https://github.com",
tokenPath: oauth.token_path || "/login/oauth/access_token",
authorizePath: oauth.authorize_path || "/login/oauth/authorize",
},
});

const oauthApp = express();

oauthApp.get("/auth", (req, res) => {
const authorizationUri = oauth2.authorizationCode.authorizeURL({
redirect_uri: oauth.redirect_url,
scope: oauth.scopes || "repo,user",
state: randomstring.generate(32),
});

res.redirect(authorizationUri);
});

// eslint-disable-next-line consistent-return
oauthApp.get("/callback", async (req, res) => {
const options = {
code: req.query.code,
};

if (oauthProvider === "gitlab") {
options.client_id = oauth.client_id;
options.client_secret = oauth.client_secret;
options.grant_type = "authorization_code";
options.redirect_uri = oauth.redirect_url;
}

try {
const result = await oauth2.authorizationCode.getToken(options);
const token = oauth2.accessToken.create(result);

return res.send(
getScript("success", {
token: token.token.access_token,
provider: oauthProvider,
}),
);
} catch (error) {
console.error("Access Token Error", error.message);
res.send(getScript("error", error));
}
});

oauthApp.get("/success", (req, res) => {
res.send("");
});

oauthApp.get("/", (req, res) => {
res.redirect(301, `/oauth/auth`);
});

exports.oauth = functions.https.onRequest(oauthApp);
Loading

0 comments on commit 5916f64

Please sign in to comment.