-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for sending link for authenticating users via email (#286)
Signed-off-by: Kush Sharma <[email protected]>
- Loading branch information
1 parent
2738405
commit a591ae3
Showing
22 changed files
with
206 additions
and
89 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
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,65 @@ | ||
package strategy | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"html/template" | ||
"strings" | ||
"time" | ||
|
||
"github.com/raystack/frontier/pkg/mailer" | ||
"gopkg.in/mail.v2" | ||
) | ||
|
||
const ( | ||
MailLinkAuthMethod string = "maillink" | ||
) | ||
|
||
// MailLink sends a mail with a one time password link to user's email id. | ||
// On successful verification, it creates a session | ||
type MailLink struct { | ||
dialer mailer.Dialer | ||
subject string | ||
body string | ||
Now func() time.Time | ||
host string | ||
} | ||
|
||
func NewMailLink(d mailer.Dialer, host, subject, body string) *MailLink { | ||
return &MailLink{ | ||
host: host, | ||
dialer: d, | ||
subject: subject, | ||
body: body, | ||
Now: func() time.Time { | ||
return time.Now().UTC() | ||
}, | ||
} | ||
} | ||
|
||
// SendMail sends a mail with a one time password embedded link to user's email id | ||
func (m MailLink) SendMail(id, to string) (string, error) { | ||
otp := GenerateNonceFromLetters(otpLen, otpLetterRunes) | ||
t, err := template.New("body").Parse(m.body) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to parse email template: %w", err) | ||
} | ||
var tpl bytes.Buffer | ||
|
||
link := fmt.Sprintf("%s?strategy_name=%s&code=%s&state=%s", strings.TrimRight(m.host, "/"), MailLinkAuthMethod, otp, id) | ||
err = t.Execute(&tpl, map[string]string{ | ||
"Link": link, | ||
}) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to parse email template: %w", err) | ||
} | ||
|
||
//TODO(kushsharma): apply rest of the headers | ||
msg := mail.NewMessage() | ||
msg.SetHeader("From", m.dialer.FromHeader()) | ||
msg.SetHeader("To", to) | ||
msg.SetHeader("Subject", m.subject) | ||
msg.SetBody("text/html", tpl.String()) | ||
msg.SetDateHeader("Date", m.Now()) | ||
return otp, m.dialer.DialAndSend(msg) | ||
} |
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
Oops, something went wrong.