From 15ab546c0ae729673b99f22fc0f0893c78b37619 Mon Sep 17 00:00:00 2001 From: Punarv Pawade Date: Thu, 14 Mar 2024 01:51:57 +0530 Subject: [PATCH 1/2] add auth endpoint to verify token --- webapp/src/App.tsx | 14 ++++++++------ webapp/src/components/Auth.tsx | 26 ++++++++++++++++++++++++++ webapp/src/index.tsx | 7 +++---- webapp/src/modules/Utils.ts | 3 +++ webapp/src/modules/api.ts | 20 +++++++++++++++++--- 5 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 webapp/src/components/Auth.tsx diff --git a/webapp/src/App.tsx b/webapp/src/App.tsx index dc1a308..d5b1a1d 100644 --- a/webapp/src/App.tsx +++ b/webapp/src/App.tsx @@ -1,20 +1,22 @@ // App.tsx -import { HashRouter as Router, Route, Routes } from 'react-router-dom'; -import Dashboard from './pages/Dashboard'; -import LandingPage from './pages/LandingPage'; -import Server from './pages/Server'; +import { BrowserRouter, Route, Routes } from "react-router-dom"; +import Dashboard from "./pages/Dashboard"; +import LandingPage from "./pages/LandingPage"; +import Server from "./pages/Server"; +import AuthComponent from "./components/Auth"; const App = () => { return ( - +
} /> + } /> } /> } />
-
+ ); }; diff --git a/webapp/src/components/Auth.tsx b/webapp/src/components/Auth.tsx new file mode 100644 index 0000000..9366f65 --- /dev/null +++ b/webapp/src/components/Auth.tsx @@ -0,0 +1,26 @@ +// AuthComponent.tsx +import React, { useEffect } from "react"; +import { useSearchParams, redirect } from "react-router-dom"; +import { verifyToken } from "../modules/api"; + +const AuthComponent = () => { + const verify = async (token: string | null) => { + const res = await verifyToken(token); + console.log(res.data); + }; + + useEffect(() => { + const [searchParams, setSearchParams] = useSearchParams(); + const token = searchParams.get("token"); + console.log("Token:", token); + verify(token); + }, []); + + return ( +
+

Authenticating...

+
+ ); +}; + +export default AuthComponent; diff --git a/webapp/src/index.tsx b/webapp/src/index.tsx index c9383dd..8502e36 100644 --- a/webapp/src/index.tsx +++ b/webapp/src/index.tsx @@ -1,5 +1,5 @@ import React from "react"; -import ReactDOM from "react-dom"; +import ReactDOM from "react-dom/client"; import "./index.css"; import App from "./App"; @@ -42,7 +42,7 @@ import { AptosWalletAdapterProvider } from "@aptos-labs/wallet-adapter-react"; const wallets = [new PetraWallet()]; -ReactDOM.render( +ReactDOM.createRoot(document.getElementById("root")!).render( {/* {/* */} - , - document.getElementById("root") + ); diff --git a/webapp/src/modules/Utils.ts b/webapp/src/modules/Utils.ts index 312a22d..d612940 100644 --- a/webapp/src/modules/Utils.ts +++ b/webapp/src/modules/Utils.ts @@ -1,4 +1,7 @@ export function getBaseUrl(): string { const { protocol, host } = window.location; return `${protocol}//${host}`; +} +export function getGatewayURL(): string | undefined { + return process.env.GATEWAY_URL; } \ No newline at end of file diff --git a/webapp/src/modules/api.ts b/webapp/src/modules/api.ts index 2ec2e9b..b386697 100644 --- a/webapp/src/modules/api.ts +++ b/webapp/src/modules/api.ts @@ -1,9 +1,9 @@ import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'; -import { getBaseUrl } from './Utils'; +import { getBaseUrl, getGatewayURL } from './Utils'; import Cookies from 'js-cookie'; const baseURL = getBaseUrl() - +const gatewayURL = getGatewayURL() export interface UpdateClientPayload { id: string; name: string; @@ -192,4 +192,18 @@ export const getToken = async (signature: string | string[] | undefined, challen throw error; } return response; -}; \ No newline at end of file +}; + +export async function verifyToken(token: string | null) { + const url = `${gatewayURL}/api/v1.0/webapp/auth` + const response = await axios.get(url, { + headers: { + "Authorization": `Bearer ${Cookies.get("token")}` + } + }); + if (response.status === 200) { + return response.data; + } else { + throw new Error(`Request failed with status: ${response.status}`); + } +} \ No newline at end of file From fe44fdc4bb6e7d93dec74e53a2a4f5c513ae39de Mon Sep 17 00:00:00 2001 From: Punarv Pawade Date: Fri, 29 Mar 2024 02:50:04 +0530 Subject: [PATCH 2/2] auth: Authenticate token from gateway --- api/v1/authenticate/paseto/paseto.go | 63 +++++++++++++++++++++------- api/v1/authenticate/paseto/types.go | 4 ++ webapp/src/components/Auth.tsx | 32 +++++++++++--- webapp/src/modules/Utils.ts | 3 +- webapp/src/modules/api.ts | 2 +- 5 files changed, 81 insertions(+), 23 deletions(-) diff --git a/api/v1/authenticate/paseto/paseto.go b/api/v1/authenticate/paseto/paseto.go index f4100e8..b4225a3 100644 --- a/api/v1/authenticate/paseto/paseto.go +++ b/api/v1/authenticate/paseto/paseto.go @@ -7,11 +7,9 @@ import ( "net/http" "strings" - gopaseto "aidanwoods.dev/go-paseto" + "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus" - "github.com/NetSepio/sotreus/util/pkg/auth" - "github.com/NetSepio/sotreus/util/pkg/claims" "github.com/gin-gonic/gin" ) @@ -40,24 +38,57 @@ func PASETO(c *gin.Context) { } token := headers.Authorization splitToken := strings.Split(token, "Bearer ") - pasetoToken := splitToken[1] - parser := gopaseto.NewParser() - parser.AddRule(gopaseto.NotExpired()) - publickey := auth.Getpublickey() - parsedToken, err := parser.ParseV4Public(publickey, pasetoToken, nil) + authToken := splitToken[1] + + //auth req to gateway + contractReq, err := http.NewRequest(http.MethodGet, "https://dev.gateway.sotreus.com/api/v1.0/webapp/auth", nil) if err != nil { - err = fmt.Errorf("failed to scan claims for paseto token, %s", err) - log.WithFields(log.Fields{ - "err": err, - }).Error("failed to bindfailed to scan claims for paseto token") + logrus.Errorf("failed to send request: %s", err) + c.AbortWithStatus(http.StatusUnauthorized) + return + } + contractReq.Header.Set("Authorization", "Bearer "+authToken) + client := &http.Client{} + resp, err := client.Do(contractReq) + if err != nil { + logrus.Errorf("failed to send request: %s", err) c.AbortWithStatus(http.StatusUnauthorized) return + } + if resp.StatusCode != 200 { + logrus.Errorf("Error in response: %s", err) + c.AbortWithStatus(http.StatusUnauthorized) + return + } + defer resp.Body.Close() + var responseBody webappResponse + err = json.NewDecoder(resp.Body).Decode(&responseBody) + fmt.Println("Wallet Address: ", responseBody.WalletAddress) + if err != nil { + fmt.Printf("Failed to decode response body: %s\n", err) + return } else { - jsonvalue := parsedToken.ClaimsJSON() - ClaimsValue := claims.CustomClaims{} - json.Unmarshal(jsonvalue, &ClaimsValue) - c.Set("walletAddress", ClaimsValue.WalletAddress) + c.Set("walletAddress", responseBody.WalletAddress) c.Next() } + // parser := gopaseto.NewParser() + // parser.AddRule(gopaseto.NotExpired()) + // publickey := auth.Getpublickey() + // parsedToken, err := parser.ParseV4Public(publickey, pasetoToken, nil) + + // if err != nil { + // err = fmt.Errorf("failed to scan claims for paseto token, %s", err) + // log.WithFields(log.Fields{ + // "err": err, + // }).Error("failed to bindfailed to scan claims for paseto token") + // c.AbortWithStatus(http.StatusUnauthorized) + // return + // } else { + // jsonvalue := parsedToken.ClaimsJSON() + // ClaimsValue := claims.CustomClaims{} + // json.Unmarshal(jsonvalue, &ClaimsValue) + // c.Set("walletAddress", ClaimsValue.WalletAddress) + // c.Next() + // } } diff --git a/api/v1/authenticate/paseto/types.go b/api/v1/authenticate/paseto/types.go index 725a3a3..b2c1956 100644 --- a/api/v1/authenticate/paseto/types.go +++ b/api/v1/authenticate/paseto/types.go @@ -3,3 +3,7 @@ package paseto type GenericAuthHeaders struct { Authorization string } + +type webappResponse struct { + WalletAddress string `json:"walletAddress"` +} diff --git a/webapp/src/components/Auth.tsx b/webapp/src/components/Auth.tsx index 9366f65..39a659c 100644 --- a/webapp/src/components/Auth.tsx +++ b/webapp/src/components/Auth.tsx @@ -1,18 +1,40 @@ // AuthComponent.tsx import React, { useEffect } from "react"; -import { useSearchParams, redirect } from "react-router-dom"; +import { useSearchParams, useNavigate } from "react-router-dom"; import { verifyToken } from "../modules/api"; +import { useWallet } from "@aptos-labs/wallet-adapter-react"; +import Cookies from "js-cookie"; const AuthComponent = () => { + const navigate = useNavigate(); + const { + connect, + wallets, + disconnect, + wallet, + account, + network, + connected, + signMessage: petraSignMesssage, + signMessageAndVerify, + } = useWallet(); + + const [walletAddress, setWalletAddress] = useSearchParams(); + const verify = async (token: string | null) => { - const res = await verifyToken(token); - console.log(res.data); + console.log(wallets[0]); + await verifyToken(token).then((res) => { + Cookies.set("wallet_address", res.payload.walletAddress); + }); + setWalletAddress(walletAddress); + connect(wallets[0].name); + Cookies.set("token", token!); + navigate("/"); }; + const [searchParams, setSearchParams] = useSearchParams(); useEffect(() => { - const [searchParams, setSearchParams] = useSearchParams(); const token = searchParams.get("token"); - console.log("Token:", token); verify(token); }, []); diff --git a/webapp/src/modules/Utils.ts b/webapp/src/modules/Utils.ts index d612940..bbfc492 100644 --- a/webapp/src/modules/Utils.ts +++ b/webapp/src/modules/Utils.ts @@ -3,5 +3,6 @@ export function getBaseUrl(): string { return `${protocol}//${host}`; } export function getGatewayURL(): string | undefined { - return process.env.GATEWAY_URL; + // return process.env.GATEWAY_URL; + return "https://dev.gateway.sotreus.com"; } \ No newline at end of file diff --git a/webapp/src/modules/api.ts b/webapp/src/modules/api.ts index b386697..bcb4fea 100644 --- a/webapp/src/modules/api.ts +++ b/webapp/src/modules/api.ts @@ -198,7 +198,7 @@ export async function verifyToken(token: string | null) { const url = `${gatewayURL}/api/v1.0/webapp/auth` const response = await axios.get(url, { headers: { - "Authorization": `Bearer ${Cookies.get("token")}` + "Authorization": `Bearer ${token}` } }); if (response.status === 200) {