From 003cc08d1946b0fbc93d347955ed1fed3e0e8385 Mon Sep 17 00:00:00 2001 From: geek Date: Tue, 6 Jun 2017 20:52:51 +0900 Subject: [PATCH 1/4] email test code ing... --- proxy/email/email_service.go | 157 ++++++++++++++++++++++-------- proxy/email/email_service_test.go | 33 ++++++- 2 files changed, 148 insertions(+), 42 deletions(-) diff --git a/proxy/email/email_service.go b/proxy/email/email_service.go index 137afc3..bf3c745 100644 --- a/proxy/email/email_service.go +++ b/proxy/email/email_service.go @@ -7,52 +7,126 @@ 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" ) 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"` + 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 { +func (es *EmailService) CheckAuthURL(e *Email) bool { + //Todo Query from the database with an authentication token. + memMap := make(map[string]string) + str, err := json.Marshal(e) + if err != nil { + log.Fatal("Json Marshal Error: ", err) + } + + memMap["com.loafle.overflow.email.model.EmailAuth"] = string(str) + + re := proxy.InvokeDB("emailAuth", "find", memMap) + tempEmail := &Email{} + json.Unmarshal([]byte(re), tempEmail) + + r := strings.Compare(e.AuthToken, tempEmail.AuthToken) + + if r == 0 { + return true + } return false } -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) checkError(err error) { + if err != nil { + log.Panic(err) + } +} + +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 +134,56 @@ func EmailSendForAuth(email string) (error){ ServerName: host, } + //Todo auth token generation + e.AuthToken = es.generationAuthToken(e) + e.IsInvalid = 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) saveEmail(e *Email) { + + memMap := make(map[string]string) + + str, err := json.Marshal(e) + + fmt.Println(string(str)) + if err != nil { + log.Fatal("Json Marshal Error: ", err) + } + + memMap["com.loafle.overflow.email.model.EmailAuth"] = string(str) + proxy.InvokeDB("emailAuth", "create", memMap) + +} diff --git a/proxy/email/email_service_test.go b/proxy/email/email_service_test.go index b7432b1..9a98729 100644 --- a/proxy/email/email_service_test.go +++ b/proxy/email/email_service_test.go @@ -2,11 +2,36 @@ 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) { + +} +func TestAuthTokenGeneration(t *testing.T) { + + e, es := getEmailObj() + tt := es.generationAuthToken(e) + + fmt.Println(tt) } From b511e5dfcaf97fac3a6252f92f9ad58257c50c5e Mon Sep 17 00:00:00 2001 From: geek Date: Wed, 7 Jun 2017 17:25:39 +0900 Subject: [PATCH 2/4] email auth --- proxy/email/email_service.go | 93 ++++++++++++++++++++----------- proxy/email/email_service_test.go | 7 +++ 2 files changed, 68 insertions(+), 32 deletions(-) diff --git a/proxy/email/email_service.go b/proxy/email/email_service.go index bf3c745..30f9974 100644 --- a/proxy/email/email_service.go +++ b/proxy/email/email_service.go @@ -12,6 +12,7 @@ import ( "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 ( @@ -23,14 +24,18 @@ const ( ) 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"` - SmtpServer string `json:"_"` - BodyMsg string `json:"_"` + 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:"_"` } @@ -56,30 +61,6 @@ func NewEmailService() *EmailService{ return &EmailService{} } -func (es *EmailService) CheckAuthURL(e *Email) bool { - - //Todo Query from the database with an authentication token. - memMap := make(map[string]string) - - str, err := json.Marshal(e) - - if err != nil { - log.Fatal("Json Marshal Error: ", err) - } - - memMap["com.loafle.overflow.email.model.EmailAuth"] = string(str) - - re := proxy.InvokeDB("emailAuth", "find", memMap) - tempEmail := &Email{} - json.Unmarshal([]byte(re), tempEmail) - - r := strings.Compare(e.AuthToken, tempEmail.AuthToken) - - if r == 0 { - return true - } - return false -} func (es *EmailService) checkError(err error) { @@ -137,6 +118,7 @@ func (es *EmailService)SendEmailForAuth(e *Email) (error){ //Todo auth token generation e.AuthToken = es.generationAuthToken(e) e.IsInvalid = false + e.IsConfirm = false conn, err := tls.Dial("tcp", SERVER_NAME, tlsconfig) es.checkError(err) @@ -187,3 +169,50 @@ func (es *EmailService) saveEmail(e *Email) { proxy.InvokeDB("emailAuth", "create", memMap) } + +func (es *EmailService) CheckAuthURL(e *Email) bool { + + //Todo Query from the database with an authentication token. + + memMap := make(map[string]string) + + str, err := json.Marshal(e) + + if err != nil { + log.Fatal("Json Marshal Error: ", err) + } + + memMap["com.loafle.overflow.email.model.EmailAuth"] = string(str) + + re := proxy.InvokeDB("emailAuth", "findByAuthToken", memMap) + 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) { + memMap := make(map[string]string) + + str, err := json.Marshal(e) + + fmt.Println(string(str)) + if err != nil { + log.Fatal("Json Marshal Error: ", err) + } + + memMap["com.loafle.overflow.email.model.EmailAuth"] = string(str) + proxy.InvokeDB("emailAuth", "update", memMap) +} \ No newline at end of file diff --git a/proxy/email/email_service_test.go b/proxy/email/email_service_test.go index 9a98729..3039afe 100644 --- a/proxy/email/email_service_test.go +++ b/proxy/email/email_service_test.go @@ -26,7 +26,14 @@ func TestSendEmailForAuth(t *testing.T) { } 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) { From 88773a6a685159a7f9fb242e4fb9982fbb79f117 Mon Sep 17 00:00:00 2001 From: geek Date: Wed, 7 Jun 2017 18:16:21 +0900 Subject: [PATCH 3/4] ddd --- proxy/email/email_service.go | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/proxy/email/email_service.go b/proxy/email/email_service.go index 30f9974..f73237f 100644 --- a/proxy/email/email_service.go +++ b/proxy/email/email_service.go @@ -125,6 +125,7 @@ func (es *EmailService)SendEmailForAuth(e *Email) (error){ c, err := smtp.NewClient(conn, host) es.checkError(err) + // Auth err = c.Auth(auth) es.checkError(err) @@ -170,11 +171,9 @@ func (es *EmailService) saveEmail(e *Email) { } -func (es *EmailService) CheckAuthURL(e *Email) bool { +func (es *EmailService) getEmailMap(e *Email) map[string]string { - //Todo Query from the database with an authentication token. - - memMap := make(map[string]string) + emMap := make(map[string]string) str, err := json.Marshal(e) @@ -182,9 +181,19 @@ func (es *EmailService) CheckAuthURL(e *Email) bool { log.Fatal("Json Marshal Error: ", err) } - memMap["com.loafle.overflow.email.model.EmailAuth"] = string(str) + emMap["com.loafle.overflow.email.model.EmailAuth"] = string(str) + + return emMap +} + +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) - re := proxy.InvokeDB("emailAuth", "findByAuthToken", memMap) tempEmail := &Email{} json.Unmarshal([]byte(re), tempEmail) @@ -204,15 +213,7 @@ func (es *EmailService) CheckAuthURL(e *Email) bool { } func (es *EmailService) modifyEmailAuth(e *Email) { - memMap := make(map[string]string) + emMap := es.getEmailMap(e) - str, err := json.Marshal(e) - - fmt.Println(string(str)) - if err != nil { - log.Fatal("Json Marshal Error: ", err) - } - - memMap["com.loafle.overflow.email.model.EmailAuth"] = string(str) - proxy.InvokeDB("emailAuth", "update", memMap) + proxy.InvokeDB("emailAuth", "update", emMap) } \ No newline at end of file From 3dad54be77b1a2de13bcd8e5edaa64be2346fa13 Mon Sep 17 00:00:00 2001 From: geek Date: Wed, 7 Jun 2017 18:19:02 +0900 Subject: [PATCH 4/4] sdf --- proxy/email/email_service.go | 23 +++++++---------------- proxy/service.go | 2 +- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/proxy/email/email_service.go b/proxy/email/email_service.go index f73237f..a8250ba 100644 --- a/proxy/email/email_service.go +++ b/proxy/email/email_service.go @@ -155,22 +155,6 @@ func (es *EmailService)SendEmailForAuth(e *Email) (error){ return err } -func (es *EmailService) saveEmail(e *Email) { - - memMap := make(map[string]string) - - str, err := json.Marshal(e) - - fmt.Println(string(str)) - if err != nil { - log.Fatal("Json Marshal Error: ", err) - } - - memMap["com.loafle.overflow.email.model.EmailAuth"] = string(str) - proxy.InvokeDB("emailAuth", "create", memMap) - -} - func (es *EmailService) getEmailMap(e *Email) map[string]string { emMap := make(map[string]string) @@ -186,6 +170,13 @@ func (es *EmailService) getEmailMap(e *Email) map[string]string { 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. 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)