Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
98df6a5fd8
@ -7,52 +7,107 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"log"
|
"log"
|
||||||
"net/mail"
|
"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 (
|
const (
|
||||||
FROM = "geek@loafle.com"
|
FROM = "geek@loafle.com"
|
||||||
SERVER_NAME = "smtp.worksmobile.com:465"
|
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 {
|
type EmailService struct {
|
||||||
userUrl string `json:"userUrl"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEmailService() *EmailService{
|
func NewEmailService() *EmailService{
|
||||||
e := &EmailService{}
|
return &EmailService{}
|
||||||
return e
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *EmailService) CheckEmail(url string) bool {
|
|
||||||
|
|
||||||
|
|
||||||
|
func (es *EmailService) checkError(err error) {
|
||||||
return false
|
if err != nil {
|
||||||
|
log.Panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func EmailSendForAuth(email string) (error){
|
func (es *EmailService) getSendMailMessage(e *Email) (string) {
|
||||||
|
to := mail.Address{"Park Byung Eun",e.Member.Email}
|
||||||
to := mail.Address{"Park Byung Eun",email}
|
from := mail.Address{"Overflow", e.From}
|
||||||
from := mail.Address{"Overflow", FROM}
|
//body := "This is an Example Email\n with two lines \n http://localhost:8080/v1/overflow/services"
|
||||||
subj := "This is the Test Email"
|
|
||||||
body := "This is an Example Email\n with two lines \n http://localhost:8080/v1/overflow/services"
|
|
||||||
|
|
||||||
// Setup headers
|
// Setup headers
|
||||||
headers := make(map[string]string)
|
headers := make(map[string]string)
|
||||||
headers["From"] = from.String()
|
headers["From"] = from.String()
|
||||||
headers["To"] = to.String()
|
headers["To"] = to.String()
|
||||||
headers["Subject"] = subj
|
headers["Subject"] = e.Subj
|
||||||
|
|
||||||
//Setup Message
|
|
||||||
message := ""
|
message := ""
|
||||||
|
|
||||||
for k, v := range headers {
|
for k, v := range headers {
|
||||||
message += fmt.Sprintf("%s: %s\r\n",k,v)
|
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)
|
host, _, _ := net.SplitHostPort(SERVER_NAME)
|
||||||
auth := smtp.PlainAuth("",FROM, "@loafle@5795", host)
|
auth := smtp.PlainAuth("",FROM, SERVER_PASS, host)
|
||||||
|
|
||||||
// TLS config
|
// TLS config
|
||||||
tlsconfig := &tls.Config {
|
tlsconfig := &tls.Config {
|
||||||
@ -60,49 +115,96 @@ func EmailSendForAuth(email string) (error){
|
|||||||
ServerName: host,
|
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)
|
conn, err := tls.Dial("tcp", SERVER_NAME, tlsconfig)
|
||||||
if err != nil {
|
es.checkError(err)
|
||||||
log.Panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
c, err := smtp.NewClient(conn, host)
|
c, err := smtp.NewClient(conn, host)
|
||||||
if err != nil {
|
es.checkError(err)
|
||||||
log.Panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Auth
|
// Auth
|
||||||
if err = c.Auth(auth); err != nil {
|
err = c.Auth(auth)
|
||||||
log.Panic(err)
|
es.checkError(err)
|
||||||
}
|
|
||||||
|
|
||||||
// To && From
|
// To && From
|
||||||
if err = c.Mail(from.Address); err != nil {
|
err = c.Mail(from.Address)
|
||||||
log.Panic(err)
|
es.checkError(err)
|
||||||
}
|
|
||||||
|
|
||||||
if err = c.Rcpt(to.Address); err != nil {
|
err = c.Rcpt(to.Address)
|
||||||
log.Panic(err)
|
es.checkError(err)
|
||||||
}
|
|
||||||
|
|
||||||
// Data
|
// Data
|
||||||
w, err := c.Data()
|
w, err := c.Data()
|
||||||
if err != nil {
|
es.checkError(err)
|
||||||
log.Panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = w.Write([]byte(message))
|
_, err = w.Write([]byte(message))
|
||||||
if err != nil {
|
es.checkError(err)
|
||||||
log.Panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = w.Close()
|
err = w.Close()
|
||||||
if err != nil {
|
es.checkError(err)
|
||||||
log.Panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Quit()
|
c.Quit()
|
||||||
|
|
||||||
|
// Todo Save Email For DB
|
||||||
|
es.saveEmail(e)
|
||||||
|
|
||||||
return err
|
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)
|
||||||
|
}
|
@ -2,11 +2,43 @@ package email
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"log"
|
"fmt"
|
||||||
|
"git.loafle.net/overflow/overflow_proxy_service/proxy/member"
|
||||||
|
"encoding/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestEmailSendForAuth(t *testing.T) {
|
func getEmailObj() (*Email,*EmailService) {
|
||||||
err := EmailSendForAuth("geek@loafle.com")
|
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)
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ func InvokeDB(targetDb, methodName string, param map[string]string) (string) {
|
|||||||
in.Method = methodName
|
in.Method = methodName
|
||||||
in.Param = param
|
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 {
|
if err != nil {
|
||||||
log.Fatal("Rpc Error: ", err)
|
log.Fatal("Rpc Error: ", err)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user