added support for oauth callback url
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
simple web app with a database connection for testing purpose
|
simple web app with a database connection for testing purpose
|
||||||
return 42 + the number in input
|
return 42 + the number in input
|
||||||
|
support oauth callback url
|
||||||
|
|
||||||
> [!NOTE]
|
> [!NOTE]
|
||||||
> code generated with chat gpt
|
> code generated with chat gpt
|
||||||
|
|||||||
180
main.go
180
main.go
@@ -3,10 +3,12 @@ package main
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
_ "github.com/lib/pq" // PostgreSQL driver
|
_ "github.com/lib/pq" // PostgreSQL driver
|
||||||
)
|
)
|
||||||
@@ -126,6 +128,181 @@ func selectHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
fmt.Fprintf(w, "<h1>Result: %d</h1>", result)
|
fmt.Fprintf(w, "<h1>Result: %d</h1>", result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Structure de base pour passer les données au template HTML
|
||||||
|
type CallbackData struct {
|
||||||
|
Code string
|
||||||
|
State string
|
||||||
|
Other map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
// oauth2_callback handles HTTP requests and display a message according to queryParams
|
||||||
|
func oauth2_callback(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Récupération des paramètres query
|
||||||
|
queryParams := r.URL.Query()
|
||||||
|
code := queryParams.Get("code")
|
||||||
|
state := queryParams.Get("state")
|
||||||
|
|
||||||
|
// Construction d'une map pour les autres paramètres
|
||||||
|
otherParams := make(map[string]string)
|
||||||
|
for key, values := range queryParams {
|
||||||
|
if key != "code" && key != "state" {
|
||||||
|
otherParams[key] = strings.Join(values, ", ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Définition du template HTML avec le comportement demandé
|
||||||
|
tmpl := `
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||||
|
<title>OAuth2 Callback</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: Arial, sans-serif;
|
||||||
|
background-color: #f4f4f4;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
h2, h3 {
|
||||||
|
color: #333;
|
||||||
|
}
|
||||||
|
p {
|
||||||
|
font-size: 16px;
|
||||||
|
}
|
||||||
|
button {
|
||||||
|
padding: 10px 20px;
|
||||||
|
background-color: #007BFF;
|
||||||
|
color: white;
|
||||||
|
border: none;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background-color 0.3s;
|
||||||
|
}
|
||||||
|
button:hover {
|
||||||
|
background-color: #0056b3;
|
||||||
|
}
|
||||||
|
.fade-out {
|
||||||
|
animation: fadeOut 2s forwards;
|
||||||
|
}
|
||||||
|
@keyframes fadeOut {
|
||||||
|
0% { opacity: 1; }
|
||||||
|
100% { opacity: 0; }
|
||||||
|
}
|
||||||
|
pre {
|
||||||
|
background-color: #eaeaea;
|
||||||
|
padding: 10px;
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<script>
|
||||||
|
function copyToClipboard(text) {
|
||||||
|
navigator.clipboard.writeText(text).then(function() {
|
||||||
|
var msg = document.getElementById('copy-message');
|
||||||
|
msg.textContent = 'Copied to clipboard!';
|
||||||
|
msg.classList.add('fade-out');
|
||||||
|
setTimeout(function() {
|
||||||
|
msg.textContent = ''; // Hide message after fading out
|
||||||
|
}, 2000); // Hide after 2 seconds
|
||||||
|
}, function(err) {
|
||||||
|
alert('Error copying to clipboard: ' + err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
{{if and .Code .State}}
|
||||||
|
<h2>Succès: Code et State reçus</h2>
|
||||||
|
<p>Code: {{.Code}}</p>
|
||||||
|
<p>State: {{.State}}</p>
|
||||||
|
<button onclick="copyToClipboard('Code: {{.Code}}\t State: {{.State}}')">Copier dans le presse-papier</button>
|
||||||
|
<p id="copy-message" style="color: green; font-weight: bold;"></p>
|
||||||
|
{{else}}
|
||||||
|
<h2>Aucun code et state reçus</h2>
|
||||||
|
{{end}}
|
||||||
|
{{if .Other}}
|
||||||
|
<h3>Autres paramètres reçus:</h3>
|
||||||
|
<pre>
|
||||||
|
{{range $key, $value := .Other}}
|
||||||
|
{{$key}}: {{$value}}
|
||||||
|
{{end}}
|
||||||
|
</pre>
|
||||||
|
{{else}}
|
||||||
|
<p>Aucun autre paramètre reçu.</p>
|
||||||
|
{{end}}
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`
|
||||||
|
|
||||||
|
// Parsing du template
|
||||||
|
t, err := template.New("callback").Parse(tmpl)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Error parsing template", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remplissage des données et exécution du template
|
||||||
|
data := CallbackData{
|
||||||
|
Code: code,
|
||||||
|
State: state,
|
||||||
|
Other: otherParams,
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "text/html")
|
||||||
|
if err := t.Execute(w, data); err != nil {
|
||||||
|
http.Error(w, "Error rendering template", http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func test_oauth2_callback(w http.ResponseWriter, r *http.Request) {
|
||||||
|
html := `
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>OAuth2 Callback Test</title>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: Arial, sans-serif;
|
||||||
|
margin: 0;
|
||||||
|
padding: 20px;
|
||||||
|
background-color: #f4f4f4;
|
||||||
|
}
|
||||||
|
h1 {
|
||||||
|
color: #333;
|
||||||
|
}
|
||||||
|
form {
|
||||||
|
margin-top: 20px;
|
||||||
|
}
|
||||||
|
input[type="text"], button {
|
||||||
|
padding: 10px;
|
||||||
|
margin-top: 10px;
|
||||||
|
width: 100%;
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
button {
|
||||||
|
background-color: #007BFF;
|
||||||
|
color: white;
|
||||||
|
border: none;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background-color 0.3s;
|
||||||
|
}
|
||||||
|
button:hover {
|
||||||
|
background-color: #0056b3;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Welcome to OAuth2 Test</h1>
|
||||||
|
<form action="/oauth-callback" method="get">
|
||||||
|
<label>Paramètre Code:</label><br>
|
||||||
|
<input type="text" name="code"><br>
|
||||||
|
<label>Paramètre State:</label><br>
|
||||||
|
<input type="text" name="state"><br>
|
||||||
|
<button type="submit">Envoyer</button>
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`
|
||||||
|
w.Header().Set("Content-Type", "text/html")
|
||||||
|
w.Write([]byte(html))
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
@@ -148,6 +325,9 @@ func main() {
|
|||||||
// Define the handler for the `/readiness` probe
|
// Define the handler for the `/readiness` probe
|
||||||
http.HandleFunc("/readiness", readinessHandler)
|
http.HandleFunc("/readiness", readinessHandler)
|
||||||
|
|
||||||
|
http.HandleFunc("/oauth-callback", oauth2_callback)
|
||||||
|
http.HandleFunc("/test-oauth-callback", test_oauth2_callback)
|
||||||
|
|
||||||
// Start the HTTP server
|
// Start the HTTP server
|
||||||
port := ":8080"
|
port := ":8080"
|
||||||
log.Printf("Server starting on port %s\n", port)
|
log.Printf("Server starting on port %s\n", port)
|
||||||
|
|||||||
Reference in New Issue
Block a user