Created
April 9, 2022 23:12
-
-
Save natesales/97156caf3a1276eee60c7e0fc9ca937a to your computer and use it in GitHub Desktop.
PeeringDB OAuth token exchange error PoC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"io" | |
"log" | |
"net/http" | |
"os" | |
"github.com/gofiber/fiber/v2" | |
"golang.org/x/oauth2" | |
) | |
const ( | |
peeringDbProfileUrl = "https://auth.peeringdb.com/profile/v1" | |
peeringDbAuthUrl = "https://auth.peeringdb.com/oauth2/authorize/" | |
peeringDbTokenUrl = "https://auth.peeringdb.com/oauth2/token/" | |
) | |
func main() { | |
app := fiber.New(fiber.Config{DisableStartupMessage: true}) | |
oauth := oauth2.Config{ | |
ClientID: os.Getenv("PDB_CLIENT_ID"), | |
ClientSecret: os.Getenv("PDB_CLIENT_SECRET"), | |
RedirectURL: "https://localhost/redirect", | |
Endpoint: oauth2.Endpoint{ | |
AuthURL: peeringDbAuthUrl, | |
TokenURL: peeringDbTokenUrl, | |
}, | |
} | |
app.Get("/", func(ctx *fiber.Ctx) error { | |
return ctx.Redirect(oauth.AuthCodeURL(peeringDbProfileUrl)) | |
}) | |
app.Get("/redirect", func(ctx *fiber.Ctx) error { | |
state := ctx.FormValue("state") | |
if state != peeringDbProfileUrl { | |
return ctx.Status(http.StatusBadRequest).SendString("Invalid state") | |
} | |
token, err := oauth.Exchange(context.Background(), ctx.FormValue("code")) | |
if err != nil { | |
log.Fatal(err) | |
} | |
req, err := http.NewRequest("GET", peeringDbProfileUrl, nil) | |
if err != nil { | |
log.Fatal(err) | |
} | |
req.Header.Set("Authorization", "Bearer "+token.AccessToken) | |
resp, err := http.DefaultClient.Do(req) | |
if err != nil { | |
log.Fatal(err) | |
} | |
respBody, err := io.ReadAll(resp.Body) | |
if err != nil { | |
log.Fatal(err) | |
} | |
return ctx.SendString(string(respBody)) | |
}) | |
log.Println("Starting POC on https://localhost") | |
log.Fatal(app.ListenTLS(":443", "cert.pem", "key.pem")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment