diff --git a/proxy/email/email_service.go b/proxy/email/email_service.go index 137afc3..a8250ba 100644 --- a/proxy/email/email_service.go +++ b/proxy/email/email_service.go @@ -7,52 +7,107 @@ import ( "crypto/tls" "log" "net/mail" + "strings" + "github.com/google/uuid" + "encoding/json" + "git.loafle.net/overflow/overflow_proxy_service/proxy" + "git.loafle.net/overflow/overflow_proxy_service/proxy/member" + "git.loafle.net/overflow/commons_go/model/timestamp" ) const ( FROM = "geek@loafle.com" SERVER_NAME = "smtp.worksmobile.com:465" + SUBJECT = "This is the Test Email" + BODY_MSG = "This is an Example Email\n with two lines \n http://localhost:8080/v1/overflow/services" + SERVER_PASS = "@loafle@5795" ) +type Email struct { + Id json.Number `json:"id,Number,omitempty"` + Member member.Member `json:"member"` + From string `json:"_"` + Subj string `json:"_"` + AuthToken string `json:"authToken"` + IsInvalid bool `json:"isInvalid"` + IsConfirm bool `json:"isConfirm"` + CreateDate timestamp.Timestamp `json:"createDate,omitempty"` + UpdateDate timestamp.Timestamp `json:"updateDate,omitempty"` + ConfirmDate timestamp.Timestamp `json:"confirmDate,omitempty"` + SmtpServer string `json:"_"` + BodyMsg string `json:"_"` + +} + +func NewEmail(member member.Member, subject string) *Email { + + if subject == ""{ + subject = SUBJECT + } + + return &Email{ + Member:member, + From:FROM, + Subj:subject, + SmtpServer:SERVER_NAME, + } +} + type EmailService struct { - userUrl string `json:"userUrl"` + } func NewEmailService() *EmailService{ - e := &EmailService{} - return e + return &EmailService{} } -func (e *EmailService) CheckEmail(url string) bool { - - return false +func (es *EmailService) checkError(err error) { + if err != nil { + log.Panic(err) + } } -func EmailSendForAuth(email string) (error){ - - to := mail.Address{"Park Byung Eun",email} - from := mail.Address{"Overflow", FROM} - subj := "This is the Test Email" - body := "This is an Example Email\n with two lines \n http://localhost:8080/v1/overflow/services" +func (es *EmailService) getSendMailMessage(e *Email) (string) { + to := mail.Address{"Park Byung Eun",e.Member.Email} + from := mail.Address{"Overflow", e.From} + //body := "This is an Example Email\n with two lines \n http://localhost:8080/v1/overflow/services" // Setup headers headers := make(map[string]string) headers["From"] = from.String() headers["To"] = to.String() - headers["Subject"] = subj + headers["Subject"] = e.Subj - //Setup Message message := "" for k, v := range headers { message += fmt.Sprintf("%s: %s\r\n",k,v) } - message += "\r\n" + body + message += "\r\n" + BODY_MSG + + return message +} + +func (es *EmailService) generationAuthToken(e *Email) string { + var tempToken string + + uuid,_ := uuid.NewRandom() + tempToken += strings.ToUpper(uuid.String()) + + return tempToken +} + + +func (es *EmailService)SendEmailForAuth(e *Email) (error){ + to := mail.Address{"Park Byung Eun",e.Member.Email} + from := mail.Address{"Overflow", e.From} + + message := es.getSendMailMessage(e) host, _, _ := net.SplitHostPort(SERVER_NAME) - auth := smtp.PlainAuth("",FROM, "@loafle@5795", host) + auth := smtp.PlainAuth("",FROM, SERVER_PASS, host) // TLS config tlsconfig := &tls.Config { @@ -60,49 +115,96 @@ func EmailSendForAuth(email string) (error){ ServerName: host, } + //Todo auth token generation + e.AuthToken = es.generationAuthToken(e) + e.IsInvalid = false + e.IsConfirm = false + conn, err := tls.Dial("tcp", SERVER_NAME, tlsconfig) - if err != nil { - log.Panic(err) - } + es.checkError(err) c, err := smtp.NewClient(conn, host) - if err != nil { - log.Panic(err) - } + es.checkError(err) // Auth - if err = c.Auth(auth); err != nil { - log.Panic(err) - } + err = c.Auth(auth) + es.checkError(err) // To && From - if err = c.Mail(from.Address); err != nil { - log.Panic(err) - } + err = c.Mail(from.Address) + es.checkError(err) - if err = c.Rcpt(to.Address); err != nil { - log.Panic(err) - } + err = c.Rcpt(to.Address) + es.checkError(err) // Data w, err := c.Data() - if err != nil { - log.Panic(err) - } + es.checkError(err) _, err = w.Write([]byte(message)) - if err != nil { - log.Panic(err) - } + es.checkError(err) err = w.Close() - if err != nil { - log.Panic(err) - } + es.checkError(err) c.Quit() + // Todo Save Email For DB + es.saveEmail(e) return err } +func (es *EmailService) getEmailMap(e *Email) map[string]string { + + emMap := make(map[string]string) + + str, err := json.Marshal(e) + + if err != nil { + log.Fatal("Json Marshal Error: ", err) + } + + emMap["com.loafle.overflow.email.model.EmailAuth"] = string(str) + + return emMap +} + +func (es *EmailService) saveEmail(e *Email) { + + memMap := es.getEmailMap(e) + proxy.InvokeDB("emailAuth", "create", memMap) + +} + +func (es *EmailService) CheckAuthURL(e *Email) bool { + + //Todo Query from the database with an authentication token. + + emMap := es.getEmailMap(e) + + re := proxy.InvokeDB("emailAuth", "findByAuthToken", emMap) + + tempEmail := &Email{} + json.Unmarshal([]byte(re), tempEmail) + + r := strings.Compare(e.AuthToken, tempEmail.AuthToken) + + log.Println(tempEmail) + if r == 0 { + // Todo isConfirm change true and db update + tempEmail.IsConfirm = true + tempEmail.UpdateDate = timestamp.Now() + tempEmail.ConfirmDate = timestamp.Now() + + es.modifyEmailAuth(tempEmail) + return true + } + return false +} + +func (es *EmailService) modifyEmailAuth(e *Email) { + emMap := es.getEmailMap(e) + + proxy.InvokeDB("emailAuth", "update", emMap) +} \ No newline at end of file diff --git a/proxy/email/email_service_test.go b/proxy/email/email_service_test.go index b7432b1..3039afe 100644 --- a/proxy/email/email_service_test.go +++ b/proxy/email/email_service_test.go @@ -2,11 +2,43 @@ package email import ( "testing" - "log" + "fmt" + "git.loafle.net/overflow/overflow_proxy_service/proxy/member" + "encoding/json" ) -func TestEmailSendForAuth(t *testing.T) { - err := EmailSendForAuth("geek@loafle.com") +func getEmailObj() (*Email,*EmailService) { + es := NewEmailService() + m := member.Member{ + Email:"geek@loafle.com", + Name:"geek", + Company:"loafle", + Id:json.Number("4"), + } + e := NewEmail(m, "Hello Oveflow") - log.Println(err) + return e,es +} +func TestSendEmailForAuth(t *testing.T) { + e, es := getEmailObj() + + es.SendEmailForAuth(e) +} + +func TestEmailService_CheckAuthURL(t *testing.T) { + e, es := getEmailObj() + + //e.Id = json.Number("2") + e.AuthToken = "3C03F8AB-1D4D-4C8A-8C36-EE2D644988B5" + + rr := es.CheckAuthURL(e) + + fmt.Println(rr) +} +func TestAuthTokenGeneration(t *testing.T) { + + e, es := getEmailObj() + tt := es.generationAuthToken(e) + + fmt.Println(tt) } diff --git a/proxy/service.go b/proxy/service.go index 6f0ac8e..e798998 100644 --- a/proxy/service.go +++ b/proxy/service.go @@ -15,7 +15,7 @@ func InvokeDB(targetDb, methodName string, param map[string]string) (string) { in.Method = methodName in.Param = param - conn, err := grpc.Dial("192.168.1.209:50006", grpc.WithInsecure()) + conn, err := grpc.Dial("192.168.1.103:50006", grpc.WithInsecure()) if err != nil { log.Fatal("Rpc Error: ", err)