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) {