forked from jason/cpolis
Created package "calls" and verification for frontend and calls
This commit is contained in:
44
cmd/calls/pdf.go
Normal file
44
cmd/calls/pdf.go
Normal file
@ -0,0 +1,44 @@
|
||||
package calls
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
b "streifling.com/jason/cpolis/cmd/backend"
|
||||
)
|
||||
|
||||
func ServePDFList(c *b.Config) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if tokenIsVerified(w, r) {
|
||||
files, err := os.ReadDir(c.PDFDir)
|
||||
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())
|
||||
}
|
||||
|
||||
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 ServePDF(c *b.Config) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if tokenIsVerified(w, r) {
|
||||
http.ServeFile(w, r, fmt.Sprint(c.PDFDir, "/", r.PathValue("id")))
|
||||
}
|
||||
}
|
||||
}
|
15
cmd/calls/rss.go
Normal file
15
cmd/calls/rss.go
Normal file
@ -0,0 +1,15 @@
|
||||
package calls
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
b "streifling.com/jason/cpolis/cmd/backend"
|
||||
)
|
||||
|
||||
func ServeRSS(c *b.Config) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if tokenIsVerified(w, r) {
|
||||
http.ServeFile(w, r, c.RSSFile)
|
||||
}
|
||||
}
|
||||
}
|
34
cmd/calls/verification.go
Normal file
34
cmd/calls/verification.go
Normal file
@ -0,0 +1,34 @@
|
||||
package calls
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
b "streifling.com/jason/cpolis/cmd/backend"
|
||||
)
|
||||
|
||||
// tokenIsVerified verifies that a request is authorized. It returns a bool.
|
||||
func tokenIsVerified(w http.ResponseWriter, r *http.Request) bool {
|
||||
idToken := r.Header.Get("Authorization")
|
||||
if idToken == "" {
|
||||
log.Println("Authorization header missing")
|
||||
http.Error(w, "Authorization header missing", http.StatusUnauthorized)
|
||||
return false
|
||||
}
|
||||
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user