Commit 38bc24f9 authored by Mathias Engelbrecht Pilegård's avatar Mathias Engelbrecht Pilegård
Browse files

replaced custom password recovery email with okt

parent a074fc0b
......@@ -34,7 +34,7 @@ func printAllUsers(users []User) {
func GetUsers() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Fetching users...")
fmt.Printf("\n Fetching users...")
_, client, err := okta.NewClient(context.Background(), okta.WithOrgUrl("https://" + os.Getenv("CLIENT_ID")), okta.WithToken("007XkBhvAfeVf1jUB0xhdwQJJEvWNSBQ6bbO4MoEaV"))
if err != nil {
......@@ -75,9 +75,39 @@ func GetUsers() http.HandlerFunc {
}
}
// Gets user data from OKTA SDK API from user.id
func GetUser(w http.ResponseWriter, r *http.Request) User {
tempUserData := User{}
_, client, err := okta.NewClient(context.Background(), okta.WithOrgUrl("https://" + os.Getenv("CLIENT_ID")), okta.WithToken("007XkBhvAfeVf1jUB0xhdwQJJEvWNSBQ6bbO4MoEaV"))
if err != nil {
ReturnError(w, http.StatusInternalServerError, err.Error())
return tempUserData
}
if err := json.NewDecoder(r.Body).Decode(&tempUserData);
err != nil {
ReturnError(w, http.StatusInternalServerError, err.Error())
return tempUserData
}
user, resp, err := client.User.GetUser(context.Background(), tempUserData.Email)
if _, err := w.Write([]byte(resp.Self)); err != nil {
ReturnError(w, http.StatusInternalServerError, err.Error())
return tempUserData
}
tempUserData.FirstName = fmt.Sprintf("%v", (*user.Profile)["firstName"])
tempUserData.LastName = fmt.Sprintf("%v", (*user.Profile)["lastName"])
tempUserData.Email = fmt.Sprintf("%v", (*user.Profile)["email"])
tempUserData.Login = fmt.Sprintf("%v", (*user.Profile)["login"])
return tempUserData
}
func CreateUser() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Creating new user...")
fmt.Printf("\n Creating new user...")
_, client, err := okta.NewClient(context.Background(), okta.WithOrgUrl("https://" + os.Getenv("CLIENT_ID")), okta.WithToken("007XkBhvAfeVf1jUB0xhdwQJJEvWNSBQ6bbO4MoEaV"))
if err != nil {
......@@ -130,7 +160,7 @@ func CreateUser() http.HandlerFunc {
Login:fmt.Sprintf("%v", (*user.Profile)["login"]),
}
fmt.Println("User activation success. New user created:")
fmt.Printf("\n User activation success. New user created:")
printUser(newUser)
w.WriteHeader(http.StatusOK)
......@@ -139,6 +169,7 @@ func CreateUser() http.HandlerFunc {
func RemoveUser() http.HandlerFunc {
return func(w http.ResponseWriter, r*http.Request) {
fmt.Printf("\n Removing User:", GetUser(w, r).Email)
_, client, err := okta.NewClient(context.Background(), okta.WithOrgUrl("https://" + os.Getenv("CLIENT_ID")), okta.WithToken("007XkBhvAfeVf1jUB0xhdwQJJEvWNSBQ6bbO4MoEaV"))
if err != nil {
......@@ -146,19 +177,8 @@ func RemoveUser() http.HandlerFunc {
return
}
tempUserData := User{}
if err := json.NewDecoder(r.Body).Decode(&tempUserData);
err != nil {
ReturnError(w, http.StatusInternalServerError, err.Error())
return
}
fmt.Println("\n Removing User:", tempUserData.Email)
// User.Id == email
// Deactivate user first
resp, err := client.User.DeactivateUser(context.Background(), tempUserData.Email, &query.Params{})
resp, err := client.User.DeactivateUser(context.Background(), GetUser(w, r).Email, &query.Params{})
if err != nil {
ReturnError(w, http.StatusInternalServerError, err.Error())
return
......@@ -184,13 +204,27 @@ func RemoveUser() http.HandlerFunc {
return
}
fmt.Println("User removal success")
fmt.Printf("\n User removal success")
}
}
func ResetUserPassword() http.HandlerFunc {
return func(w http.ResponseWriter, r*http.Request) {
fmt.Printf("\n User requesting password reset. Sending email with password recovery link")
_, client, err := okta.NewClient(context.Background(), okta.WithOrgUrl("https://" + os.Getenv("CLIENT_ID")), okta.WithToken("007XkBhvAfeVf1jUB0xhdwQJJEvWNSBQ6bbO4MoEaV"))
if err != nil {
ReturnError(w, http.StatusInternalServerError, err.Error())
return
}
forgotPasswordToken, resp, err := client.User.ForgotPasswordGenerateOneTimeToken(context.Background(), GetUser(w, r).Email, &query.Params{})
if _, err := w.Write([]byte(resp.Self));
err != nil {
ReturnError(w, http.StatusInternalServerError, err.Error())
return
}
SendEmail()
fmt.Println(forgotPasswordToken.ResetPasswordUrl)
}
}
\ No newline at end of file
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style type="text/css">"*:not(br):not(tr):not(html)" {
font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif !important;
-webkit-box-sizing: border-box !important;
box-sizing: border-box !important
}"cite:before" {
content: "\2014 \0020" !important
}@media only screen and (max-width: 600px){
.email-body_inner,
.email-footer {
width: 100% !important
}
}
@media only screen and (max-width: 500px){
.button {
width: 100% !important
}
}
</style></head>
<body dir="ltr" style="height:100%;margin:0;line-height:1.4;background-color:#F2F4F6;color:#74787E;-webkit-text-size-adjust:none;width:100%">
<table class="email-wrapper" width="100%" cellpadding="0" cellspacing="0" style="width:100%;margin:0;padding:0;background-color:#F2F4F6">
<tbody><tr>
<td class="content" style="color:#74787E;font-size:15px;line-height:18px;align:center;padding:0">
<table class="email-content" width="100%" cellpadding="0" cellspacing="0" style="width:100%;margin:0;padding:0">
<tbody><tr>
<td class="email-masthead" style="color:#74787E;font-size:15px;line-height:18px;padding:25px 0;text-align:center">
<a class="email-masthead_name" href="https://hermesEmail-hermes.com/" target="_blank" style="font-size:16px;font-weight:bold;color:#2F3133;text-decoration:none;text-shadow:0 1px 0 white">
<img src="http://www.duchess-france.org/wp-content/uploads/2016/01/gopher.png" class="email-logo" style="max-height:50px"/>
</a>
</td>
</tr>
<tr>
<td class="email-body" width="100%" style="color:#74787E;font-size:15px;line-height:18px;width:100%;margin:0;padding:0;border-top:1px solid #EDEFF2;border-bottom:1px solid #EDEFF2;background-color:#FFF">
<table class="email-body_inner" align="center" width="570" cellpadding="0" cellspacing="0" style="width:570px;margin:0 auto;padding:0">
<tbody><tr>
<td class="content-cell" style="color:#74787E;font-size:15px;line-height:18px;padding:35px">
<h1 style="margin-top:0;color:#2F3133;font-size:19px;font-weight:bold">Hi Jon Snow,</h1>
<p style="margin-top:0;color:#74787E;font-size:16px;line-height:1.5em">You have received this email because a password reset request for Hermes account was received.</p>
<p style="margin-top:0;color:#74787E;font-size:16px;line-height:1.5em">Click the button below to reset your password:</p>
<!--[if mso]>
<div style="margin: 30px auto;v-text-anchor:middle;text-align:center">
<v:roundrect xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:w="urn:schemas-microsoft-com:office:word"
href="https://hermes-example.com/reset-password?token=d9729feb74992cc3482b350163a1a010"
style="height:45px;v-text-anchor:middle;width:200px;background-color:#DC4D2F;"
arcsize="10%"
strokecolor="#DC4D2F" fillcolor="#DC4D2F"
>
<w:anchorlock/>
<center style="color: #FFFFFF;font-size: 15px;text-align: center;font-family:sans-serif;font-weight:bold;">
Reset your password
</center>
</v:roundrect>
</div>
<![endif]-->
<!--[if !mso]><!-- -->
<table class="body-action" align="center" width="100%" cellpadding="0" cellspacing="0" style="width:100%;margin:30px auto;padding:0;text-align:center">
<tbody><tr>
<td align="center" style="padding:10px 5px;color:#74787E;font-size:15px;line-height:18px">
<div>
<a href="https://hermes-example.com/reset-password?token=d9729feb74992cc3482b350163a1a010" class="button" style="display:inline-block;border-radius:3px;font-size:15px;line-height:45px;text-align:center;text-decoration:none;-webkit-text-size-adjust:none;mso-hide:all;color:#ffffff;background-color:#DC4D2F;width:200px" target="_blank" width="200">
Reset your password
</a>
</div>
</td>
</tr>
</tbody></table>
<!--[endif]---->
<p style="margin-top:0;color:#74787E;font-size:16px;line-height:1.5em">If you did not request a password reset, no further action is required on your part.</p>
<p style="margin-top:0;color:#74787E;font-size:16px;line-height:1.5em">
Thanks,
<br/>
Hermes
</p>
<table class="body-sub" style="width:100%;margin-top:25px;padding-top:25px;border-top:1px solid #EDEFF2;table-layout:fixed">
<tbody>
<tr>
<td style="padding:10px 5px;color:#74787E;font-size:15px;line-height:18px">
<p class="sub" style="margin-top:0;color:#74787E;line-height:1.5em;font-size:12px">If you’re having trouble with the button &#39;Reset your password&#39;, copy and paste the URL below into your web browser.</p>
<p class="sub" style="margin-top:0;color:#74787E;line-height:1.5em;font-size:12px"><a href="https://hermes-example.com/reset-password?token=d9729feb74992cc3482b350163a1a010" style="color:#3869D4;word-break:break-all">https://hermes-example.com/reset-password?token=d9729feb74992cc3482b350163a1a010</a></p>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody></table>
</td>
</tr>
<tr>
<td style="padding:10px 5px;color:#74787E;font-size:15px;line-height:18px">
<table class="email-footer" align="center" width="570" cellpadding="0" cellspacing="0" style="width:570px;margin:0 auto;padding:0;text-align:center">
<tbody><tr>
<td class="content-cell" style="color:#74787E;font-size:15px;line-height:18px;padding:35px">
<p class="sub center" style="margin-top:0;line-height:1.5em;color:#AEAEAE;font-size:12px;text-align:center">
Copyright © 2020 Hermes. All rights reserved.
</p>
</td>
</tr>
</tbody></table>
</td>
</tr>
</tbody></table>
</td>
</tr>
</tbody></table>
</body></html>
\ No newline at end of file
......@@ -4,15 +4,16 @@ import (
"fmt"
"github.com/go-gomail/gomail"
"github.com/matcornic/hermes/v2"
//"golang.org/x/crypto/ssh/terminal"
"io/ioutil"
"net/mail"
"os"
//"strconv"
)
const passwordResetEmail = "Password reset request"
const verifyUserEmail = "Verify email"
type hermesEmail interface {
Email() hermes.Email
Email(User) hermes.Email
Name() string
}
......@@ -30,11 +31,12 @@ type sendOptions struct {
Subject string
}
func SendEmail() {
func SendEmail(user User, emailType string) {
h := hermes.Hermes{
Product: hermes.Product{
Name: "Hermes",
Link: "https://hermesEmail-hermes.com/",
Name: "PipIt support team",
// TODO: change link to correct site when web application is deployed
Link: "http://localhost:3000/",
Logo: "http://www.duchess-france.org/wp-content/uploads/2016/01/gopher.png",
},
}
......@@ -43,21 +45,26 @@ func SendEmail() {
Server: "smtp.gmail.com",
Port: 587,
SenderEmail: "pilegaard.mathias@gmail.com",
SenderIdentity: "Mathias Pilegaard",
SenderIdentity: "PipIt support team",
SMTPPassword: "Jgq!F6onF5p9J!NT",
}
options := sendOptions{
To: "mathiaspil98@live.dk",
Subject: "Hermes mail",
To: user.Email,
Subject: "PipIt account service: " + emailType,
}
email := []hermesEmail{
new(reset),
}
// TODO: add "else if" or some other solution for switching between email templates
// if(emailType == passwordResetEmail) {
// fmt.Printf("\n sending a password reset email")
// }
email := []hermesEmail{
new(reset),
}
for _, e := range email {
GenerateEmail(h, e.Email(), e.Name())
GenerateEmail(h, e.Email(user), e.Name())
}
for _, e := range email {
......@@ -103,6 +110,7 @@ func Send(smtpConfig smtpAuthentication, options sendOptions, htmlBody string) e
m.SetBody("text/html", htmlBody)
d := gomail.NewDialer(smtpConfig.Server, smtpConfig.Port, smtpConfig.SenderEmail, smtpConfig.SMTPPassword)
fmt.Printf("\n Email sent.")
return d.DialAndSend(m)
}
\ No newline at end of file
......@@ -11,19 +11,21 @@ func (r *reset) Name() string {
return "reset"
}
func (r *reset) Email() hermes.Email {
func (r *reset) Email(user User) hermes.Email {
return hermes.Email{
Body: hermes.Body{
Name: "Jon Snow",
// This
Name: user.FirstName,
Intros: []string{
"You have received this email because a password reset request for Hermes account was received.",
"You have received this email because a password reset request for your PipIt account was received.",
},
Actions: []hermes.Action{
{
Instructions: "Click the button below to reset your password:",
Button: hermes.Button{
Color: "#DC4D2F",
Color: "#207cca",
Text: "Reset your password",
// This
Link: "https://hermes-example.com/reset-password?token=d9729feb74992cc3482b350163a1a010",
},
},
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment