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 secret string
admin string admin string
adminSecret string adminSecret string
apiKey string
} }
type ProviderIndex struct { type ProviderIndex struct {
@ -13,17 +14,22 @@ type ProviderIndex struct {
Providers []string Providers []string
} }
func NewService(secret, admin, adminSecret string) (*Service, error) { func NewService(secret, admin, adminSecret, apiKey string) (*Service, error) {
if secret == "" || admin == "" || adminSecret == "" { if secret == "" || admin == "" || adminSecret == "" || apiKey == "" {
return nil, errors.New("error building Auth service. Verify your env variables") return nil, errors.New("error building Auth service. Verify your env variables")
} }
return &Service{ return &Service{
secret, secret,
admin, admin,
adminSecret, adminSecret,
apiKey,
}, nil }, nil
} }
func (as *Service) Authenticate(email, password string) bool { func (as *Service) Authenticate(email, password string) bool {
return email == as.admin && password == as.adminSecret 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 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) { func (s *Service) Create(from, to time.Time) (*calendar.Event, error) {
l := s.CalendarList.List() l := s.CalendarList.List()
r, e := l.Do() r, e := l.Do()

View file

@ -32,9 +32,6 @@ type Server struct {
cs *calendar.Service cs *calendar.Service
hc *config.Host hc *config.Host
addr string addr string
secretKey string
apiKey string
Health *HealthHandler
} }
type options struct { type options struct {
@ -42,7 +39,6 @@ type options struct {
fs *embed.FS fs *embed.FS
debug *bool debug *bool
secretKey *string secretKey *string
apiKey *string
origins []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) { func New(bs *booking.Service, as *auth.Service, ps *pdf.PdfService, cs *calendar.Service, hc *config.Host, opts ...Option) (*Server, error) {
option := new(options) option := new(options)
for _, opt := range opts { for _, opt := range opts {
@ -110,8 +99,6 @@ func New(bs *booking.Service, as *auth.Service, ps *pdf.PdfService, cs *calendar
cs: cs, cs: cs,
hc: hc, hc: hc,
addr: fmt.Sprintf("0.0.0.0:%d", *option.port), addr: fmt.Sprintf("0.0.0.0:%d", *option.port),
secretKey: *option.secretKey,
apiKey: *option.apiKey,
} }
s.MountHandlers() s.MountHandlers()

View file

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