| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"html/template" |
| 8 |
"io" |
| 9 |
"log/slog" |
| 10 |
"net/http" |
| 11 |
"os" |
| 12 |
"strings" |
| 13 |
|
| 14 |
"golang.org/x/oauth2" |
| 15 |
|
| 16 |
"github.com/zitadel/oidc/v3/pkg/client/profile" |
| 17 |
) |
| 18 |
|
| 19 |
var client = http.DefaultClient |
| 20 |
|
| 21 |
func main() { |
| 22 |
keyPath := os.Getenv("KEY_PATH") |
| 23 |
issuer := os.Getenv("ISSUER") |
| 24 |
port := os.Getenv("PORT") |
| 25 |
scopes := strings.Split(os.Getenv("SCOPES"), " ") |
| 26 |
|
| 27 |
if keyPath != "" { |
| 28 |
ts, err := profile.NewJWTProfileTokenSourceFromKeyFile(context.TODO(), issuer, keyPath, scopes) |
| 29 |
if err != nil { |
| 30 |
slog.Error("error creating token source", "error", err) |
| 31 |
os.Exit(1) |
| 32 |
} |
| 33 |
client = oauth2.NewClient(context.Background(), ts) |
| 34 |
} |
| 35 |
|
| 36 |
http.HandleFunc("/jwt-profile", func(w http.ResponseWriter, r *http.Request) { |
| 37 |
if r.Method == "GET" { |
| 38 |
tpl := ` |
| 39 |
<!DOCTYPE html> |
| 40 |
<html> |
| 41 |
<head> |
| 42 |
<meta charset="UTF-8"> |
| 43 |
<title>Login</title> |
| 44 |
</head> |
| 45 |
<body> |
| 46 |
<form method="POST" action="/jwt-profile" enctype="multipart/form-data"> |
| 47 |
<label for="key">Select a key file:</label> |
| 48 |
<input type="file" accept=".json" id="key" name="key"> |
| 49 |
<button type="submit">Get Token</button> |
| 50 |
</form> |
| 51 |
</body> |
| 52 |
</html>` |
| 53 |
t, err := template.New("login").Parse(tpl) |
| 54 |
if err != nil { |
| 55 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 56 |
return |
| 57 |
} |
| 58 |
err = t.Execute(w, nil) |
| 59 |
if err != nil { |
| 60 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 61 |
} |
| 62 |
} else { |
| 63 |
err := r.ParseMultipartForm(4 << 10) |
| 64 |
if err != nil { |
| 65 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 66 |
return |
| 67 |
} |
| 68 |
file, _, err := r.FormFile("key") |
| 69 |
if err != nil { |
| 70 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 71 |
return |
| 72 |
} |
| 73 |
defer file.Close() |
| 74 |
|
| 75 |
key, err := io.ReadAll(file) |
| 76 |
if err != nil { |
| 77 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 78 |
return |
| 79 |
} |
| 80 |
ts, err := profile.NewJWTProfileTokenSourceFromKeyFileData(context.TODO(), issuer, key, scopes) |
| 81 |
if err != nil { |
| 82 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 83 |
return |
| 84 |
} |
| 85 |
client = oauth2.NewClient(context.Background(), ts) |
| 86 |
token, err := ts.Token() |
| 87 |
if err != nil { |
| 88 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 89 |
return |
| 90 |
} |
| 91 |
data, err := json.Marshal(token) |
| 92 |
if err != nil { |
| 93 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 94 |
return |
| 95 |
} |
| 96 |
w.Write(data) |
| 97 |
} |
| 98 |
}) |
| 99 |
|
| 100 |
http.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) { |
| 101 |
tpl := ` |
| 102 |
<!DOCTYPE html> |
| 103 |
<html> |
| 104 |
<head> |
| 105 |
<meta charset="UTF-8"> |
| 106 |
<title>Test</title> |
| 107 |
</head> |
| 108 |
<body> |
| 109 |
<form method="POST" action="/test"> |
| 110 |
<label for="url">URL for test:</label> |
| 111 |
<input type="text" id="url" name="url" width="200px"> |
| 112 |
<button type="submit">Test Token</button> |
| 113 |
</form> |
| 114 |
{{if .URL}} |
| 115 |
<p> |
| 116 |
Result for {{.URL}}: {{.Response}} |
| 117 |
</p> |
| 118 |
{{end}} |
| 119 |
</body> |
| 120 |
</html>` |
| 121 |
err := r.ParseForm() |
| 122 |
if err != nil { |
| 123 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 124 |
return |
| 125 |
} |
| 126 |
testURL := r.Form.Get("url") |
| 127 |
var data struct { |
| 128 |
URL string |
| 129 |
Response any |
| 130 |
} |
| 131 |
if testURL != "" { |
| 132 |
data.URL = testURL |
| 133 |
data.Response, err = callExampleEndpoint(client, testURL) |
| 134 |
if err != nil { |
| 135 |
data.Response = err |
| 136 |
} |
| 137 |
} |
| 138 |
t, err := template.New("login").Parse(tpl) |
| 139 |
if err != nil { |
| 140 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 141 |
return |
| 142 |
} |
| 143 |
err = t.Execute(w, data) |
| 144 |
if err != nil { |
| 145 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 146 |
} |
| 147 |
}) |
| 148 |
lis := fmt.Sprintf("127.0.0.1:%s", port) |
| 149 |
slog.Info("listening", "url", "http://"+lis+"/") |
| 150 |
if err := http.ListenAndServe(lis, nil); err != nil { |
| 151 |
slog.Error("server terminated", "error", err) |
| 152 |
os.Exit(1) |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
func callExampleEndpoint(client *http.Client, testURL string) (any, error) { |
| 157 |
req, err := http.NewRequest("GET", testURL, nil) |
| 158 |
if err != nil { |
| 159 |
return nil, err |
| 160 |
} |
| 161 |
|
| 162 |
resp, err := client.Do(req) |
| 163 |
if err != nil { |
| 164 |
return nil, err |
| 165 |
} |
| 166 |
defer resp.Body.Close() |
| 167 |
|
| 168 |
body, err := io.ReadAll(resp.Body) |
| 169 |
if err != nil { |
| 170 |
return nil, err |
| 171 |
} |
| 172 |
|
| 173 |
if resp.StatusCode != http.StatusOK { |
| 174 |
return "", fmt.Errorf("http status not ok: %s %s", resp.Status, body) |
| 175 |
} |
| 176 |
|
| 177 |
if strings.HasPrefix(resp.Header.Get("content-type"), "text/plain") { |
| 178 |
return string(body), nil |
| 179 |
} |
| 180 |
return body, err |
| 181 |
} |