forked from jason/cpolis
		
	Extracted verification logic for frontend into seperate function tokenIsVerified() and created ServePDFListe()
This commit is contained in:
		@@ -1,45 +1,72 @@
 | 
			
		||||
package frontend
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"os"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	b "streifling.com/jason/cpolis/cmd/backend"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func ServePDFs(c *b.Config) http.HandlerFunc {
 | 
			
		||||
func tokenIsVerified(w http.ResponseWriter, r *http.Request) bool {
 | 
			
		||||
	idToken := r.Header.Get("Authorization")
 | 
			
		||||
	if idToken == "" {
 | 
			
		||||
		http.Error(w, "Authorization header missing", http.StatusUnauthorized)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	client, err := b.NewClient()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Println(err)
 | 
			
		||||
		http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
		return false
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err = client.Verify(idToken)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Println(err)
 | 
			
		||||
		http.Error(w, err.Error(), http.StatusUnauthorized)
 | 
			
		||||
		return false
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ServePDFList(c *b.Config) http.HandlerFunc {
 | 
			
		||||
	return func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		idToken := r.Header.Get("Authorization")
 | 
			
		||||
		if idToken == "" {
 | 
			
		||||
			http.Error(w, "Authorization header missing", http.StatusUnauthorized)
 | 
			
		||||
		}
 | 
			
		||||
		if tokenIsVerified(w, r) {
 | 
			
		||||
			files, err := os.ReadDir(c.PDFDir)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				log.Println(err)
 | 
			
		||||
				http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
		client, err := b.NewClient()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println(err)
 | 
			
		||||
			http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
			fileNames := make([]string, 0)
 | 
			
		||||
			for _, file := range files {
 | 
			
		||||
				fileNames = append(fileNames, file.Name())
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
		_, err = client.Verify(idToken)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println(err)
 | 
			
		||||
			http.Error(w, err.Error(), http.StatusUnauthorized)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		pdfIDsString := r.PathValue("ids")
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println(err)
 | 
			
		||||
			http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		pdfIDs := strings.Split(pdfIDsString, ",")
 | 
			
		||||
 | 
			
		||||
		for _, id := range pdfIDs {
 | 
			
		||||
			http.ServeFile(w, r, id)
 | 
			
		||||
			w.Header().Set("Content-Type", "application/json")
 | 
			
		||||
			if err = json.NewEncoder(w).Encode(fileNames); err != nil {
 | 
			
		||||
				log.Println(err)
 | 
			
		||||
				http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ServePDFs(c *b.Config) http.HandlerFunc {
 | 
			
		||||
	return func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		if tokenIsVerified(w, r) {
 | 
			
		||||
			pdfIDsString := r.PathValue("ids")
 | 
			
		||||
			pdfIDs := strings.Split(pdfIDsString, ",")
 | 
			
		||||
 | 
			
		||||
			for _, id := range pdfIDs {
 | 
			
		||||
				http.ServeFile(w, r, id)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user