diff --git a/package.json b/package.json index 95901e5..25e3242 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/commands/login.ts b/src/commands/login.ts index 5eb2510..0a5313f 100644 --- a/src/commands/login.ts +++ b/src/commands/login.ts @@ -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()); @@ -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(); }); diff --git a/src/lib/configStore.ts b/src/lib/configStore.ts index fb88405..a6db2e6 100644 --- a/src/lib/configStore.ts +++ b/src/lib/configStore.ts @@ -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 => { + 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; -} \ No newline at end of file + } +}; diff --git a/yarn.lock b/yarn.lock index feecab9..435d6a9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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"