Skip to content

Instantly share code, notes, and snippets.

@natesales
Created April 9, 2022 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natesales/97156caf3a1276eee60c7e0fc9ca937a to your computer and use it in GitHub Desktop.
Save natesales/97156caf3a1276eee60c7e0fc9ca937a to your computer and use it in GitHub Desktop.
PeeringDB OAuth token exchange error PoC
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