Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@pebble-finances/api

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@pebble-finances/api - npm Package Compare versions

Comparing version 0.0.5 to 0.1.0

src/http/account/account.js

10

package.json
{
"name": "@pebble-finances/api",
"version": "0.0.5",
"version": "0.1.0",
"description": "",

@@ -12,8 +12,10 @@ "main": "src/index.js",

"dependencies": {
"@pebble-finances/db": "^0.0.5",
"@pebble-finances/db": "^0.1.0",
"body-parser": "^1.18.3",
"cors": "^2.8.5",
"express": "^4.16.4",
"google-auth-library": "^3.1.0"
"google-auth-library": "^3.1.0",
"googleapis": "^38.0.0"
},
"gitHead": "b926a0edf5cbf3342915725e4ef3b1e0b5a25000"
"gitHead": "122fdd9fcfdb76b11e5de154e3efc378f8fc17d6"
}

32

src/http/auth/auth.js

@@ -0,1 +1,2 @@

const { google } = require('googleapis');
const { OAuth2Client } = require('google-auth-library');

@@ -24,12 +25,30 @@

const getAuthEmail = async (token) => {
const revokeToken = async (token) => {
const client = getClient();
try {
await client.revokeToken(token.access_token);
}
catch (error) {
// If token is already invalid, do nothing
console.log(error.data);
if (!error.data || error.data.error !== 'invalid_token') {
throw error;
}
}
}
const getAuthData = async (token) => {
const client = getClient();
client.setCredentials(token);
const accessToken = await client.getAccessToken();
const tokenInfo = await client.getTokenInfo(accessToken.token);
const { email } = tokenInfo;
var oauth2 = google.oauth2({
auth: client,
version: 'v2'
});
return email;
const response = await oauth2.userinfo.v2.me.get();
return response.data;
};

@@ -55,5 +74,6 @@

module.exports = {
getAuthEmail,
getAuthData,
getToken,
revokeToken,
validateToken
};

@@ -1,3 +0,5 @@

const { validateToken } = require('./auth');
const { transaction } = require('../utils/transaction');
const { getAuthData, validateToken } = require('./auth');
const AuthException = require('../exceptions/AuthException');
const express = require('express');
const HttpException = require('../exceptions/HttpException');

@@ -15,6 +17,18 @@

const token = JSON.parse(tokenString);
let token;
try {
token = JSON.parse(tokenString);
}
catch (error) {
throw new AuthException('Invalid token');
}
await validateToken(token);
const authData = await getAuthData(token);
req.user = authData;
req.token = token;
next();

@@ -25,2 +39,6 @@ }

module.exports = middleware;
const authRouter = express.Router();
authRouter.use(middleware);
module.exports = authRouter;

@@ -0,8 +1,10 @@

const { getAuthData, getToken, revokeToken } = require('./auth');
const { transaction } = require('../utils/transaction');
const { User, NoSuchUserException } = require('@pebble-finances/db');
const AuthException = require('../exceptions/AuthException');
const HttpException = require('../exceptions/HttpException');
const { getAuthEmail, getToken } = require('./auth');
const authMiddleware = require('./middleware');
module.exports = (client, app) => {
app.post('/auth/email', (req, res) => {
app.post('/auth/signin', (req, res) => {
const { code } = req.body;

@@ -18,10 +20,24 @@

try {
const token = await getAuthEmail(code);
const token = await getToken(code);
const { email, name } = await getAuthData(token);
const user = { email, name };
// Sign in automatically creates a new user
try {
await User.get(client, email);
}
catch (error) {
if (error instanceof NoSuchUserException) {
await User.add(client, user);
}
else {
throw error;
}
}
res.send(token);
}
catch (error) {
const { data } = error.response;
throw new AuthException(data);
console.log(error);
throw new AuthException(error);
}

@@ -32,4 +48,4 @@ }

app.post('/auth/signin', (req, res) => {
const { code } = req.body;
app.post('/auth/signout', authMiddleware, (req, res) => {
const { token } = req;

@@ -39,16 +55,16 @@ transaction(

async () => {
if (!code) {
throw new HttpException('[code] is required.');
}
await revokeToken(token);
try {
const token = await getToken(code);
res.sendStatus(200);
}
);
});
res.send(token);
}
catch (error) {
const { data } = error.response;
throw new AuthException(data);
}
app.post('/auth/validate', authMiddleware, (req, res) => {
transaction(
res,
async () => {
res.send({
ok: true
});
}

@@ -55,0 +71,0 @@ );

@@ -5,2 +5,5 @@ const bodyParser = require('body-parser');

const userRoutes = require('./user/user');
const accountRoutes = require('./account/account');
const accountTypesRoutes = require('./accountTypes/accountTypes');
const cors = require('cors');

@@ -11,2 +14,3 @@ module.exports = (client) => {

app.use(cors());
app.use(bodyParser.json());

@@ -19,2 +23,4 @@ app.use(bodyParser.urlencoded({ extended: true }));

accountRoutes(client, app);
accountTypesRoutes(client, app);
authRoutes(client, app);

@@ -21,0 +27,0 @@ userRoutes(client, app);

const { transaction } = require('../utils/transaction');
const HttpException = require('../exceptions/HttpException');
const { User } = require('@pebble-finances/db');
const express = require('express');
const authMiddleware = require('../auth/middleware');
const authRouter = express.Router();
const HttpException = require('../exceptions/HttpException');
authRouter.use(authMiddleware);
module.exports = (client, app) => {
app.post('/user/add', authRouter, (req, res) => {
app.post('/user/add', authMiddleware, (req, res) => {
const user = { ...req.body };

@@ -24,4 +20,4 @@

app.get('/user/get/:email', authRouter, (req, res) => {
const { email } = req.params;
app.get('/user/get', authMiddleware, (req, res) => {
const { email } = req.user;

@@ -37,3 +33,6 @@ transaction(

res.send(result);
res.send({
...req.user,
...result
});
}

@@ -43,3 +42,3 @@ );

app.get('/user/list', authRouter, (req, res) => {
app.get('/user/list', authMiddleware, (req, res) => {
transaction(

@@ -46,0 +45,0 @@ res,

@@ -0,3 +1,4 @@

const { DBException } = require('@pebble-finances/db');
const AuthException = require('../exceptions/AuthException');
const Exception = require('../../exceptions/Exception');
const { DBException } = require('@pebble-finances/db');

@@ -9,13 +10,24 @@ const transaction = async (res, executor) => {

catch (error) {
if (
if (error instanceof AuthException) {
res.status(401).send({
error: {
message: 'Unauthorized'
}
});
}
else if (
error instanceof DBException ||
error instanceof Exception
) {
res.send({
message: error.message
res.status(500).send({
error: {
message: error.message
}
});
}
else {
res.send({
message: 'Internal Error'
res.status(500).send({
error: {
message: error.message
}
});

@@ -22,0 +34,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc