Skip to content

Commit

Permalink
auth: Authenticate token from gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
inciner8r committed Mar 28, 2024
1 parent 15ab546 commit fe44fdc
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 23 deletions.
63 changes: 47 additions & 16 deletions api/v1/authenticate/paseto/paseto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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()
// }

}
4 changes: 4 additions & 0 deletions api/v1/authenticate/paseto/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ package paseto
type GenericAuthHeaders struct {
Authorization string
}

type webappResponse struct {
WalletAddress string `json:"walletAddress"`
}
32 changes: 27 additions & 5 deletions webapp/src/components/Auth.tsx
Original file line number Diff line number Diff line change
@@ -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);
}, []);

Expand Down
3 changes: 2 additions & 1 deletion webapp/src/modules/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
2 changes: 1 addition & 1 deletion webapp/src/modules/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit fe44fdc

Please sign in to comment.