Skip to content

Commit

Permalink
added the validation functionality so now the existing sessiontoken i…
Browse files Browse the repository at this point in the history
…s used if it is valid (#50)
  • Loading branch information
shariqanwar20 committed Aug 28, 2021
1 parent 2248f7f commit e62c6e1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@yellicode/typescript": "^1.13.3",
"ajv": "^8.6.2",
"await-exec": "^0.1.2",
"axios": "^0.21.1",
"chalk": "^4.1.1",
"chokidar": "^2.0.4",
"configstore": "3.1.5",
Expand Down
8 changes: 6 additions & 2 deletions src/commands/login.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { Command, flags } from "@oclif/command";
import { isSessionTokenActive } from "../lib/configStore";
const express = require("express");
const open = require("open");
const ConfigStore = require("configstore");

export default class Login extends Command {
async run() {

// console.log(await isSessionTokenActive());
const app = express();
const port = 4000;
const scope = encodeURIComponent("from:cli");
const redirect = encodeURIComponent("http://localhost:4000/auth");
const config = new ConfigStore("panacloud", { sessionToken: "" });
const config = new ConfigStore("panacloud", { entityId: "", sessionToken: "" });

app.use(express.json());

Expand All @@ -23,7 +26,8 @@ export default class Login extends Command {
const server = await app.listen(port);

app.post("/auth", (req: any, res: any) => {
config.set("sessionToken", JSON.parse(req.body.body).address);
config.set("entityId", JSON.parse(req.body.body).entityId);
config.set("sessionToken", JSON.parse(req.body.body).sessionToken);
res.send(`Hello World!`);
server.close();
});
Expand Down
55 changes: 44 additions & 11 deletions src/lib/configStore.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,49 @@
const Configstore = require("configstore")
const fs = require("fs")
const Configstore = require("configstore");
const fs = require("fs");
import axios from "axios";

export const isSessionTokenActive = (sessionToken: string) => {
const config = new Configstore("panacloud");
export const isSessionTokenActive = async (): Promise<boolean> => {
const config = new Configstore("panacloud");

try {
if (fs.existsSync(config.path)) {
/* config file exists, confirm with API that is this token valid */
console.log(config.path);

const tokenFile = JSON.parse(fs.readFileSync(config.path))
console.log(tokenFile.sessionToken);
return true;
/* config file exists, confirm with API that is this token valid */
console.log(config.path);

const tokenFile = JSON.parse(fs.readFileSync(config.path));

const { data } = await axios.post(
"https://kvnqyzqjgjf2bkgdubxgsz7dle.appsync-api.us-east-1.amazonaws.com/graphql",
{
query: `
query EntityProfile {
getEntityProfile(input: {requestedEntityType: USER, requesterEntityId: "${tokenFile.entityId}", requesterEntityType: USER, requestedEntityId: "${tokenFile.entityId}"}) {
id
... on userFullProfileInfo {
id
publicAddress
firstName
lastName
}
}
}
`,
},
{
headers: {
Authorization: `Bearer ${tokenFile.sessionToken}`,
},
}
);
console.log(data);

if (data.data.getEntityProfile.publicAddress) return true;
else return false;
}
else {
return false;
}
} catch (error) {
return false;
}
}
};
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,13 @@ await-exec@^0.1.2:
resolved "https://registry.yarnpkg.com/await-exec/-/await-exec-0.1.2.tgz#c4dc58b415f9daf34fc9d17549f40fbc4839fd6b"
integrity sha512-BQUiyBLScS0+YPnnCZZGjb78mZ8sQ8aKgxarDPNw05rpbaCS7VIQSLy2tgjZKct9Dn1xLbKMXOpA98OWei90zA==

axios@^0.21.1:
version "0.21.1"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.1.tgz#22563481962f4d6bde9a76d516ef0e5d3c09b2b8"
integrity sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==
dependencies:
follow-redirects "^1.10.0"

balanced-match@^1.0.0:
version "1.0.2"
resolved "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz"
Expand Down Expand Up @@ -1792,6 +1799,11 @@ flatted@^2.0.0:
resolved "https://registry.npmjs.org/flatted/-/flatted-2.0.2.tgz"
integrity sha512-r5wGx7YeOwNWNlCA0wQ86zKyDLMQr+/RB8xy74M4hTphfmjlijTSSXGuH8rnvKZnfT9i+75zmd8jcKdMR4O6jA==

follow-redirects@^1.10.0:
version "1.14.2"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.2.tgz#cecb825047c00f5e66b142f90fed4f515dec789b"
integrity sha512-yLR6WaE2lbF0x4K2qE2p9PEXKLDjUjnR/xmjS3wHAYxtlsI9MLLBJUZirAHKzUZDGLxje7w/cXR49WOUo4rbsA==

for-in@^1.0.2:
version "1.0.2"
resolved "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz"
Expand Down

0 comments on commit e62c6e1

Please sign in to comment.