|
|
@ -3,78 +3,114 @@ package main
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
|
|
|
Host string
|
|
|
|
|
|
|
|
AuthPDSToken string
|
|
|
|
|
|
|
|
Secret string
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
func main() {
|
|
|
|
http.HandleFunc("/check-maths-user", CheckMathsUserHandler)
|
|
|
|
godotenv.Load()
|
|
|
|
port := ":8080"
|
|
|
|
|
|
|
|
|
|
|
|
if v, present := os.LookupEnv("HOST"); present {
|
|
|
|
|
|
|
|
Host = v
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Host = ":8080"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if v, present := os.LookupEnv("AUTHPDS_TOKEN"); present {
|
|
|
|
|
|
|
|
AuthPDSToken = v
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
log.Fatal(`missing AUTHPDS_TOKEN`)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fmt.Printf("Listening on port %s...\n", port)
|
|
|
|
if v, present := os.LookupEnv("SECRET"); present {
|
|
|
|
http.ListenAndServe(port, nil)
|
|
|
|
Secret = v
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
log.Fatal(`missing SECRET`)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
r := http.NewServeMux()
|
|
|
|
|
|
|
|
r.HandleFunc("/check-maths-user", checkMathsUserHandler)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.Printf("Starting server on %s...\n", Host)
|
|
|
|
|
|
|
|
http.ListenAndServe(Host, r)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func checkMathsUserHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
|
|
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func CheckMathsUserHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
// Get the username from the query parameters
|
|
|
|
// Get the username from the query parameters
|
|
|
|
userName := r.URL.Query().Get("user")
|
|
|
|
userName := r.URL.Query().Get("user")
|
|
|
|
|
|
|
|
|
|
|
|
// Get the AUTHORIZATION header value, which should include the secret token
|
|
|
|
|
|
|
|
authHeader := r.Header.Get("Authorization")
|
|
|
|
|
|
|
|
expectedAuthHeader := "Bearer " + os.Getenv("SECRET")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check if the provided authorization header matches the expected secret token
|
|
|
|
// Check if the provided authorization header matches the expected secret token
|
|
|
|
if authHeader != expectedAuthHeader {
|
|
|
|
if r.Header.Get("Authorization") != "Bearer "+Secret {
|
|
|
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
|
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get the AUTHPDS_TOKEN from an environment variable
|
|
|
|
userApiUrl := fmt.Sprintf("https://api.unipi.it/authPds/api/Carriera/studente/uid/%s/", userName)
|
|
|
|
authPdsToken := os.Getenv("AUTHPDS_TOKEN")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Make a GET request to the external API
|
|
|
|
// Create a GET request to the external API
|
|
|
|
apiURL := fmt.Sprintf("https://api.unipi.it/authPds/api/Carriera/studente/uid/%s/", userName)
|
|
|
|
ateneoRequest, err := http.NewRequest("GET", userApiUrl, nil)
|
|
|
|
req, err := http.NewRequest("GET", apiURL, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
req.Header.Add("accept", "*/*")
|
|
|
|
ateneoRequest.Header.Add("Accept", "*/*")
|
|
|
|
req.Header.Add("Authorization", "Bearer "+authPdsToken)
|
|
|
|
ateneoRequest.Header.Add("Authorization", "Bearer "+AuthPDSToken)
|
|
|
|
|
|
|
|
|
|
|
|
// Execute the request
|
|
|
|
// Make the request to the external API
|
|
|
|
client := &http.Client{}
|
|
|
|
ateneoResponse, err := http.DefaultClient.Do(ateneoRequest)
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
defer ateneoResponse.Body.Close()
|
|
|
|
|
|
|
|
|
|
|
|
// Check if the response contains the desired keywords
|
|
|
|
isMatematica, err := checkUtenteDiMatematica(ateneoResponse)
|
|
|
|
body := make([]byte, 0)
|
|
|
|
|
|
|
|
_, err = resp.Body.Read(body)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
containsKeywords := false
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
if err := json.NewEncoder(w).Encode(
|
|
|
|
|
|
|
|
map[string]any{
|
|
|
|
|
|
|
|
"result": isMatematica,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
); err != nil {
|
|
|
|
|
|
|
|
log.Printf(`encode error: %v`, err)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func checkUtenteDiMatematica(r *http.Response) (bool, error) {
|
|
|
|
|
|
|
|
// Check if the response contains the desired keywords
|
|
|
|
|
|
|
|
body, err := io.ReadAll(r.Body)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return false, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
keywords := []string{"MATEMATICA", "Mobilit", "Transizione"}
|
|
|
|
keywords := []string{"MATEMATICA", "Mobilit", "Transizione"}
|
|
|
|
for _, keyword := range keywords {
|
|
|
|
for _, keyword := range keywords {
|
|
|
|
if strings.Contains(string(body), keyword) {
|
|
|
|
if strings.Contains(string(body), keyword) {
|
|
|
|
containsKeywords = true
|
|
|
|
return true, nil
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create a JSON response
|
|
|
|
return false, nil
|
|
|
|
response := map[string]bool{
|
|
|
|
|
|
|
|
"result": containsKeywords,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(response)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|