stable
This commit is contained in:
24
database.js
24
database.js
@@ -60,3 +60,27 @@ export async function GetKeypairFromEmail(email){
|
|||||||
const [rows] = await pool.query("SELECT Users.pub_key,Users.email, PrivateKeys.private_key FROM Users JOIN PrivateKeys ON Users.pub_key = PrivateKeys.pub_key WHERE email=?", [email]);
|
const [rows] = await pool.query("SELECT Users.pub_key,Users.email, PrivateKeys.private_key FROM Users JOIN PrivateKeys ON Users.pub_key = PrivateKeys.pub_key WHERE email=?", [email]);
|
||||||
return rows[0];
|
return rows[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function CreateNewRequest(){
|
||||||
|
const id = uuidv4();
|
||||||
|
|
||||||
|
await pool.query(`INSERT INTO Requests (id) VALUES(?)`, [id]);
|
||||||
|
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function SetRequestResult(id,result){
|
||||||
|
const [rows] = await pool.query("UPDATE Requests SET result=?, status=1 WHERE id=?", [result,id]);
|
||||||
|
|
||||||
|
return rows.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function uuidv4() {
|
||||||
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
|
||||||
|
.replace(/[xy]/g, function (c) {
|
||||||
|
const r = Math.random() * 16 | 0,
|
||||||
|
v = c == 'x' ? r : (r & 0x3 | 0x8);
|
||||||
|
return v.toString(16);
|
||||||
|
});
|
||||||
|
}
|
||||||
16
firebase.js
Normal file
16
firebase.js
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import admin from "firebase-admin";
|
||||||
|
import { applicationDefault } from 'firebase-admin/app';
|
||||||
|
import serviceAccountKey from './keys/serviceAccount.json' assert { type: "json" };
|
||||||
|
|
||||||
|
export const app = admin.initializeApp({
|
||||||
|
credential: admin.credential.cert(serviceAccountKey)
|
||||||
|
});
|
||||||
|
|
||||||
|
export const verifyFirebaseToken = async (token) => {
|
||||||
|
try {
|
||||||
|
const decodedToken = await app.auth().verifyIdToken(token);
|
||||||
|
return decodedToken; // Includes uid, email, etc.
|
||||||
|
} catch (error) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
115
index.js
115
index.js
@@ -1,21 +1,21 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import dotenv from 'dotenv';
|
import dotenv from 'dotenv';
|
||||||
|
|
||||||
import {RegisterNewUser, GetUserFromEmail, GetUserFromPubkey, GetKeypairFromEmail, Authneticate} from './database.js';
|
import { RegisterNewUser, GetUserFromEmail, GetUserFromPubkey, GetKeypairFromEmail, Authneticate, CreateNewRequest, SetRequestResult } from './database.js';
|
||||||
|
import { verifyFirebaseToken } from './firebase.js';
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
import cors from 'cors';
|
||||||
|
app.use(cors());
|
||||||
app.use((err,req,res,next)=>{
|
app.use((err,req,res,next)=>{
|
||||||
console.error(err.stack);
|
console.error(err.stack);
|
||||||
res.status(500).send("Something went wrong!");
|
res.status(500).send("Something went wrong!");
|
||||||
})
|
})
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||||
|
app.get("/registerv1", async (req,res)=>{
|
||||||
|
|
||||||
app.get("/register", async (req,res)=>{
|
|
||||||
const {email, password} = req.query;
|
const {email, password} = req.query;
|
||||||
|
|
||||||
if(!email || !password){
|
if(!email || !password){
|
||||||
@@ -31,13 +31,43 @@ app.get("/register", async (req,res)=>{
|
|||||||
res.status(201).json(newUser);
|
res.status(201).json(newUser);
|
||||||
})
|
})
|
||||||
|
|
||||||
app.get("/login", async(req,res)=>{
|
app.get("/register", async (req,res)=>{
|
||||||
const {email, pubkey} = req.query;
|
const {tokenId} = req.query;
|
||||||
if(!email && !pubkey){
|
|
||||||
res.status(403).json({error:"Neither email nor pubkey was sent"})
|
if(!tokenId){
|
||||||
|
res.status(403).json({error: "Missing tokenId"});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const user = await verifyFirebaseToken(tokenId);
|
||||||
|
if(!user){
|
||||||
|
res.status(403).json({error:"Invalid tokenId. Auth failed"});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const newUser = await RegisterNewUser(user.uid,user.uid);
|
||||||
|
|
||||||
|
if(!newUser){
|
||||||
|
res.status(403).json({error: "User exists"});
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(201).json(newUser);
|
||||||
|
})
|
||||||
|
|
||||||
|
app.get("/login", async(req,res)=>{
|
||||||
|
const {tokenId} = req.query;
|
||||||
|
if(!tokenId){
|
||||||
|
res.status(403).json({error:"Missing tokenId"})
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const fuser = await verifyFirebaseToken(tokenId);
|
||||||
|
if(!fuser){
|
||||||
|
res.status(403).json({error:"Invalid tokenId. Auth failed"});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const email = fuser.uid;
|
||||||
|
|
||||||
if(email){
|
if(email){
|
||||||
const user = await GetUserFromEmail(email);
|
const user = await GetUserFromEmail(email);
|
||||||
if(user){
|
if(user){
|
||||||
@@ -45,13 +75,6 @@ app.get("/login", async(req,res)=>{
|
|||||||
}else{
|
}else{
|
||||||
res.status(404).json({error:"User couldnt be found"})
|
res.status(404).json({error:"User couldnt be found"})
|
||||||
}
|
}
|
||||||
}else if(pubkey){
|
|
||||||
const user = await GetUserFromPubkey(pubkey);
|
|
||||||
if(user){
|
|
||||||
res.status(200).json(user);
|
|
||||||
}else{
|
|
||||||
res.status(404).json({error:"User couldnt be found"})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -70,20 +93,21 @@ app.get("/authenticate", async(req,res)=>{
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
app.get("/getKeypairWithPassword", async(req,res)=>{
|
app.get("/getKeypairWithToken", async(req,res)=>{
|
||||||
const {email, password} = req.query;
|
const {tokenId} = req.query;
|
||||||
if(!email || !password){
|
if(!tokenId){
|
||||||
res.status(403).json({error:"Credentials missing"})
|
res.status(403).json({error:"tokenId missing"})
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const authResult = await Authneticate(email,password);
|
const fuser = await verifyFirebaseToken(tokenId);
|
||||||
if(authResult){
|
if(!fuser){
|
||||||
const user = await GetKeypairFromEmail(email);
|
res.status(403).json({error:"Invalid tokenId. Auth failed"});
|
||||||
res.status(200).json(user)
|
return;
|
||||||
}else{
|
|
||||||
res.status(403).json({authenticated: false})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const user = await GetKeypairFromEmail(fuser.uid);
|
||||||
|
res.status(200).json(user);
|
||||||
})
|
})
|
||||||
|
|
||||||
app.get("/getPubkey", async(req,res)=>{
|
app.get("/getPubkey", async(req,res)=>{
|
||||||
@@ -116,6 +140,45 @@ app.get("/getKeypair", async(req,res)=>{
|
|||||||
res.status(200).json(user);
|
res.status(200).json(user);
|
||||||
})
|
})
|
||||||
|
|
||||||
|
app.get("/firebaseAuth", async (req,res)=>{
|
||||||
|
const {tokenId} = req.query;
|
||||||
|
|
||||||
|
if(!tokenId){
|
||||||
|
res.status(403).json({error: "Invalid tokenID"});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = await verifyFirebaseToken(tokenId);
|
||||||
|
if(user){
|
||||||
|
res.status(200).json({status:"Success", user: user});
|
||||||
|
}else{
|
||||||
|
res.status(403).json({status:"Failed", user:null});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
app.get("/createNewRequest", async(req,res)=>{
|
||||||
|
const id = await CreateNewRequest();
|
||||||
|
|
||||||
|
res.status(200).json({id:id});
|
||||||
|
})
|
||||||
|
|
||||||
|
app.get("/completeRequest", async(req,res)=>{
|
||||||
|
const {id,result}=req.query;
|
||||||
|
|
||||||
|
if(!id || !result){
|
||||||
|
res.status(403).json({error:"Missing parameters, need id and result"});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await SetRequestResult(id,result);
|
||||||
|
|
||||||
|
res.status(200).json({status:"Success"});
|
||||||
|
})
|
||||||
|
|
||||||
|
app.get("/status", async(req,res)=>{
|
||||||
|
res.status(200).json({status:"Running"});
|
||||||
|
})
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
14
keys/serviceAccount.json
Normal file
14
keys/serviceAccount.json
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"type": "service_account",
|
||||||
|
"project_id": "sologin-55bbd",
|
||||||
|
"private_key_id": "d8003aa087c48e44a963fa0d80813e2ca4625506",
|
||||||
|
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDrWwRaTiuwoN8v\nOkkOTlhgwpSYcZK1Ys0rI22MprfuFiyGiygQGYGDzP89aq6Y7Qxe/E7ZU6jEIB0t\nNc2gBTPepFh9bBZ8Cdn3vhqxGwLwZA+K3Q+JBPgeF/rQyPJvH1gsZvSHnDy6MqOL\nA3iTyOowpZARTlxCeNZKuG6jr06RMZhPScrAOlJH8Uw2XFhpD+pzxKErmFszulCh\n1HQbZYXAm6yiqKApLdm8QfPUbdevrhAEKDNLpcgtml4xDCXYLd6/XMat/aAvgUCT\n63+eR1VjSIJfIOMAQXI8+NkRMxUqvptfYU42uvgH5BQoD8VPOfLk1umEnTgJ1OYY\nWhPoEKb1AgMBAAECggEAbTCj21Wq9V4dTWboH+f/Hp0HnZYLb3WwJBRirb9nU+Eb\ntSv4YUfEC6bev9qvuCk59RNvbWpfwOX5Q1FT3aqsPt3WZBHyeIeFhWfAMGyMt4Rk\npZMbswEIL8YGSjkAKMfM+4SkbAnzLptxvnvUxBORexJEvZKyQ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gEXdnlc2YIQ4P9jhA=\n-----END PRIVATE KEY-----\n",
|
||||||
|
"client_email": "firebase-adminsdk-zxug8@sologin-55bbd.iam.gserviceaccount.com",
|
||||||
|
"client_id": "115460243259470432140",
|
||||||
|
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
||||||
|
"token_uri": "https://oauth2.googleapis.com/token",
|
||||||
|
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
||||||
|
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-zxug8%40sologin-55bbd.iam.gserviceaccount.com",
|
||||||
|
"universe_domain": "googleapis.com"
|
||||||
|
}
|
||||||
|
|
||||||
21
package-lock.json
generated
21
package-lock.json
generated
@@ -13,6 +13,7 @@
|
|||||||
"@solana/spl-token": "^0.4.9",
|
"@solana/spl-token": "^0.4.9",
|
||||||
"@solana/web3.js": "^1.95.4",
|
"@solana/web3.js": "^1.95.4",
|
||||||
"bs58": "^6.0.0",
|
"bs58": "^6.0.0",
|
||||||
|
"cors": "^2.8.5",
|
||||||
"dotenv": "^16.4.5",
|
"dotenv": "^16.4.5",
|
||||||
"express": "^4.21.1",
|
"express": "^4.21.1",
|
||||||
"firebase-admin": "^13.0.0",
|
"firebase-admin": "^13.0.0",
|
||||||
@@ -1231,6 +1232,18 @@
|
|||||||
"resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
|
"resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
|
||||||
"integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ=="
|
"integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ=="
|
||||||
},
|
},
|
||||||
|
"node_modules/cors": {
|
||||||
|
"version": "2.8.5",
|
||||||
|
"resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz",
|
||||||
|
"integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==",
|
||||||
|
"dependencies": {
|
||||||
|
"object-assign": "^4",
|
||||||
|
"vary": "^1"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.10"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/cross-fetch": {
|
"node_modules/cross-fetch": {
|
||||||
"version": "3.1.8",
|
"version": "3.1.8",
|
||||||
"resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.8.tgz",
|
"resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.8.tgz",
|
||||||
@@ -2508,6 +2521,14 @@
|
|||||||
"node-gyp-build-test": "build-test.js"
|
"node-gyp-build-test": "build-test.js"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/object-assign": {
|
||||||
|
"version": "4.1.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
|
||||||
|
"integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.10.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/object-hash": {
|
"node_modules/object-hash": {
|
||||||
"version": "3.0.0",
|
"version": "3.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/object-hash/-/object-hash-3.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/object-hash/-/object-hash-3.0.0.tgz",
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
"@solana/spl-token": "^0.4.9",
|
"@solana/spl-token": "^0.4.9",
|
||||||
"@solana/web3.js": "^1.95.4",
|
"@solana/web3.js": "^1.95.4",
|
||||||
"bs58": "^6.0.0",
|
"bs58": "^6.0.0",
|
||||||
|
"cors": "^2.8.5",
|
||||||
"dotenv": "^16.4.5",
|
"dotenv": "^16.4.5",
|
||||||
"express": "^4.21.1",
|
"express": "^4.21.1",
|
||||||
"firebase-admin": "^13.0.0",
|
"firebase-admin": "^13.0.0",
|
||||||
|
|||||||
Reference in New Issue
Block a user