This commit is contained in:
Ruidy 2024-08-06 14:13:33 +02:00
parent 25e48b03f9
commit 0e46357690
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
4 changed files with 25 additions and 33 deletions

View file

@ -6,6 +6,7 @@ type Service struct {
secret string
admin string
adminSecret string
apiKey string
}
type ProviderIndex struct {
@ -13,17 +14,22 @@ type ProviderIndex struct {
Providers []string
}
func NewService(secret, admin, adminSecret string) (*Service, error) {
if secret == "" || admin == "" || adminSecret == "" {
func NewService(secret, admin, adminSecret, apiKey string) (*Service, error) {
if secret == "" || admin == "" || adminSecret == "" || apiKey == "" {
return nil, errors.New("error building Auth service. Verify your env variables")
}
return &Service{
secret,
admin,
adminSecret,
apiKey,
}, nil
}
func (as *Service) Authenticate(email, password string) bool {
return email == as.admin && password == as.adminSecret
}
func (as *Service) ValidateApiKey(key string) bool {
return key == as.apiKey
}

View file

@ -52,7 +52,6 @@ func NewService(ctx context.Context, credJson string, opts ...Option) (*Service,
return &Service{Service: srv}, nil
}
// TODO: implement create event, list events in a period, delete event
func (s *Service) Create(from, to time.Time) (*calendar.Event, error) {
l := s.CalendarList.List()
r, e := l.Do()

View file

@ -25,16 +25,13 @@ import (
)
type Server struct {
Router *echo.Echo
bs *booking.Service
as *auth.Service
ps *pdf.PdfService
cs *calendar.Service
hc *config.Host
addr string
secretKey string
apiKey string
Health *HealthHandler
Router *echo.Echo
bs *booking.Service
as *auth.Service
ps *pdf.PdfService
cs *calendar.Service
hc *config.Host
addr string
}
type options struct {
@ -42,7 +39,6 @@ type options struct {
fs *embed.FS
debug *bool
secretKey *string
apiKey *string
origins []string
}
@ -86,13 +82,6 @@ func WithOrigins(origins []string) Option {
}
}
func WithApiKey(apiKey string) Option {
return func(o *options) error {
o.apiKey = &apiKey
return nil
}
}
func New(bs *booking.Service, as *auth.Service, ps *pdf.PdfService, cs *calendar.Service, hc *config.Host, opts ...Option) (*Server, error) {
option := new(options)
for _, opt := range opts {
@ -103,15 +92,13 @@ func New(bs *booking.Service, as *auth.Service, ps *pdf.PdfService, cs *calendar
}
s := &Server{
Router: NewRouter(*option.fs, *option.debug, *option.secretKey, option.origins),
bs: bs,
as: as,
ps: ps,
cs: cs,
hc: hc,
addr: fmt.Sprintf("0.0.0.0:%d", *option.port),
secretKey: *option.secretKey,
apiKey: *option.apiKey,
Router: NewRouter(*option.fs, *option.debug, *option.secretKey, option.origins),
bs: bs,
as: as,
ps: ps,
cs: cs,
hc: hc,
addr: fmt.Sprintf("0.0.0.0:%d", *option.port),
}
s.MountHandlers()

View file

@ -78,6 +78,7 @@ func run(c context.Context, getEnv func(string) string) error {
getEnv("SESSION_SECRET"),
getEnv("ADMIN"),
getEnv("ADMIN_SECRET"),
getEnv("API_KEY"),
)
if err != nil {
return fmt.Errorf("error starting auth service %s", err)
@ -105,16 +106,15 @@ func run(c context.Context, getEnv func(string) string) error {
origins := strings.Split(ogs, ",")
srv, err := server.New(
booking.NewService(db),
booking.NewService(db), // TODO: should validate the booking service building
as,
ps,
cs,
config.NewHost(),
config.NewHost(), // TODO: move to the database at some point
server.WithPort(port),
server.WithFileSystem(static),
server.WithDebug(strings.ToLower(getEnv("DEBUG")) == "true"),
server.WithSecretKey(getEnv("SECRET_KEY")),
server.WithApiKey(getEnv("API_KEY")),
server.WithOrigins(origins),
)
if err != nil {