This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow custom email server for invitations (#796)
* Allow custom email server for invitations * strings * fix lint * lint2 * fix returns * fix message * break * has creds
- Loading branch information
Showing
8 changed files
with
230 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package email | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"firebase.google.com/go/auth" | ||
"github.com/google/exposure-notifications-server/pkg/secrets" | ||
) | ||
|
||
// ProviderType represents a type of email provider. | ||
type ProviderType string | ||
|
||
const ( | ||
ProviderTypeNoop ProviderType = "NOOP" | ||
ProviderTypeFirebase ProviderType = "FIREBASE" | ||
ProviderTypeSMTP ProviderType = "SIMPLE_SMTP" | ||
) | ||
|
||
// Config represents the env var based configuration for email SMTP server connection. | ||
type Config struct { | ||
ProviderType ProviderType | ||
|
||
User string `env:"EMAIL_USER" json:",omitempty"` | ||
Password string `env:"EMAIL_PASSWORD" json:",omitempty"` | ||
SMTPHost string `env:"EMAIL_SMTP_HOST" json:",omitempty"` | ||
SMTPPort string `env:"EMAIL_SMTP_PORT" json:",omitempty"` | ||
|
||
// Secrets is the secret configuration. This is used to resolve values that | ||
// are actually pointers to secrets before returning them to the caller. The | ||
// table implementation is the source of truth for which values are secrets | ||
// and which are plaintext. | ||
Secrets secrets.Config | ||
} | ||
|
||
type Provider interface { | ||
// SendNewUserInvitation sends an invite to join the server. | ||
SendNewUserInvitation(ctx context.Context, email string) error | ||
} | ||
|
||
func (c *Config) HasSMTPCreds() bool { | ||
return c.User != "" && c.Password != "" && c.SMTPHost != "" && c.SMTPPort != "" | ||
} | ||
|
||
func ProviderFor(ctx context.Context, c *Config, auth *auth.Client) (Provider, error) { | ||
switch typ := c.ProviderType; typ { | ||
case ProviderTypeFirebase: | ||
return NewFirebase(ctx) | ||
case ProviderTypeSMTP: | ||
return NewSMTP(ctx, c.User, c.Password, c.SMTPHost, c.SMTPPort, auth) | ||
default: | ||
return nil, fmt.Errorf("unknown email provider type: %v", typ) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package email is logic for sending email invitations | ||
package email | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/google/exposure-notifications-verification-server/internal/firebase" | ||
) | ||
|
||
var _ Provider = (*firebase.Client)(nil) | ||
|
||
// NewFirebase creates a new SMTP email sender with the given auth. | ||
func NewFirebase(ctx context.Context) (Provider, error) { | ||
firebaseInternal, err := firebase.New(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to configure internal firebase client: %w", err) | ||
} | ||
return firebaseInternal, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package email is logic for sending email invitations | ||
package email | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"mime/quotedprintable" | ||
"net/smtp" | ||
|
||
"firebase.google.com/go/auth" | ||
) | ||
|
||
var _ Provider = (*SMTPProvider)(nil) | ||
|
||
// SMTPProvider sends messages via an external SMTP server. | ||
type SMTPProvider struct { | ||
FirebaseAuth *auth.Client | ||
|
||
User string | ||
Password string | ||
SMTPHost string | ||
SMTPPort string | ||
} | ||
|
||
// NewSMTP creates a new Smtp email sender with the given auth. | ||
func NewSMTP(ctx context.Context, user, password, host, port string, auth *auth.Client) (Provider, error) { | ||
return &SMTPProvider{ | ||
FirebaseAuth: auth, | ||
User: user, | ||
Password: password, | ||
SMTPHost: host, | ||
SMTPPort: port, | ||
}, nil | ||
} | ||
|
||
// SendNewUserInvitation sends a password reset email to the user. | ||
func (s *SMTPProvider) SendNewUserInvitation(ctx context.Context, toEmail string) error { | ||
// Header | ||
header := make(map[string]string) | ||
header["From"] = s.User | ||
header["To"] = toEmail | ||
header["Subject"] = "COVID-19 Verification Server Invitation" | ||
|
||
header["MIME-Version"] = "1.0" | ||
header["Content-Type"] = `text/html; charset="utf-8"` | ||
header["Content-Disposition"] = "inline" | ||
header["Content-Transfer-Encoding"] = "quoted-printable" | ||
|
||
headerMessage := "" | ||
for key, value := range header { | ||
headerMessage += fmt.Sprintf("%s: %s\r\n", key, value) | ||
} | ||
|
||
inviteLink, err := s.FirebaseAuth.PasswordResetLink(ctx, toEmail) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Message. | ||
body := fmt.Sprintf( | ||
`You've been invited to the COVID-19 Verification Server. | ||
Use the link below to set up your account.<br>%s`, inviteLink) | ||
var bodyMessage bytes.Buffer | ||
temp := quotedprintable.NewWriter(&bodyMessage) | ||
temp.Write([]byte(body)) | ||
temp.Close() | ||
|
||
finalMessage := headerMessage + "\r\n" + bodyMessage.String() | ||
|
||
// Authentication. | ||
auth := smtp.PlainAuth("", s.User, s.Password, s.SMTPHost) | ||
|
||
// Sending email. | ||
err = smtp.SendMail(s.SMTPHost+":"+s.SMTPPort, auth, s.User, []string{toEmail}, []byte(finalMessage)) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |