First implementation of firebase auth

This commit is contained in:
Jason Streifling 2024-08-17 18:21:56 +02:00
parent 46bfd23b2a
commit bebfe994ae
3 changed files with 61 additions and 3 deletions

View File

@ -17,6 +17,7 @@ type Config struct {
KeyFile string
Link string
LogFile string
PDFDir string
PicsDir string
Port string
RSSFile string
@ -29,6 +30,7 @@ func newConfig() *Config {
DBName: "cpolis",
KeyFile: "/var/www/cpolis/cpolis.key",
LogFile: "/var/log/cpolis.log",
PDFDir: "/var/www/cpolis/pdfs",
PicsDir: "/var/www/cpolis/pics",
RSSFile: "/var/www/cpolis/cpolis.rss",
WebDir: "/var/www/cpolis/web",
@ -79,6 +81,7 @@ func (c *Config) handleCliArgs() error {
flag.StringVar(&c.KeyFile, "key", c.KeyFile, "key file")
flag.StringVar(&c.Link, "link", c.Link, "Channel Link")
flag.StringVar(&c.LogFile, "log", c.LogFile, "log file")
flag.StringVar(&c.PDFDir, "pdfs", c.PDFDir, "pdf directory")
flag.StringVar(&c.PicsDir, "pics", c.PicsDir, "pictures directory")
flag.StringVar(&c.RSSFile, "rss", c.RSSFile, "RSS file")
flag.StringVar(&c.Title, "title", c.Title, "Channel title")
@ -96,6 +99,11 @@ func (c *Config) handleCliArgs() error {
return fmt.Errorf("error finding absolute path for log file: %v", err)
}
c.PDFDir, err = filepath.Abs(c.PDFDir)
if err != nil {
return fmt.Errorf("error finding absolute path for pdfs dir: %v", err)
}
c.PicsDir, err = filepath.Abs(c.PicsDir)
if err != nil {
return fmt.Errorf("error finding absolute path for pics dir: %v", err)

View File

@ -12,7 +12,7 @@ type Client struct {
*auth.Client
}
func NewApp() (*Client, error) {
func NewClient() (*Client, error) {
var err error
client := new(Client)
@ -32,8 +32,13 @@ func NewApp() (*Client, error) {
return client, nil
}
func (c *Client) Verify(token string) (*auth.Token, error) {
func (c *Client) Verify(idToken string) (*auth.Token, error) {
ctx := context.Background()
return nil, nil
token, err := c.VerifyIDTokenAndCheckRevoked(ctx, idToken)
if err != nil {
return nil, err
}
return token, nil
}

45
cmd/frontend/firebase.go Normal file
View File

@ -0,0 +1,45 @@
package frontend
import (
"log"
"net/http"
"strings"
b "streifling.com/jason/cpolis/cmd/backend"
)
func ServePDFs(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)
}
client, err := b.NewClient()
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, 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)
}
}
}