package main import ( "context" "encoding/json" "fmt" "html/template" "io/ioutil" "net/http" "os" "strings" "github.com/sirupsen/logrus" "golang.org/x/oauth2" "github.com/zitadel/oidc/pkg/client/profile" ) var ( client = http.DefaultClient ) func main() { keyPath := os.Getenv("KEY_PATH") issuer := os.Getenv("ISSUER") port := os.Getenv("PORT") scopes := strings.Split(os.Getenv("SCOPES"), " ") if keyPath != "" { ts, err := profile.NewJWTProfileTokenSourceFromKeyFile(issuer, keyPath, scopes) if err != nil { logrus.Fatalf("error creating token source %s", err.Error()) } client = oauth2.NewClient(context.Background(), ts) } http.HandleFunc("/jwt-profile", func(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { tpl := `
Result for {{.URL}}: {{.Response}}
{{end}} ` err := r.ParseForm() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } testURL := r.Form.Get("url") var data struct { URL string Response interface{} } if testURL != "" { data.URL = testURL data.Response, err = callExampleEndpoint(client, testURL) if err != nil { data.Response = err } } t, err := template.New("login").Parse(tpl) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } err = t.Execute(w, data) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } }) lis := fmt.Sprintf("127.0.0.1:%s", port) logrus.Infof("listening on http://%s/", lis) logrus.Fatal(http.ListenAndServe("127.0.0.1:"+port, nil)) } func callExampleEndpoint(client *http.Client, testURL string) (interface{}, error) { req, err := http.NewRequest("GET", testURL, nil) if err != nil { return nil, err } resp, err := client.Do(req) if err != nil { return nil, err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } if resp.StatusCode != http.StatusOK { return "", fmt.Errorf("http status not ok: %s %s", resp.Status, body) } if strings.HasPrefix(resp.Header.Get("content-type"), "text/plain") { return string(body), nil } return body, err }