Passwordless (#292)

* add two rpcs to User service:
	- Passwordless: endpoint that receives an email, topic and an optional message
	- PasswordlessML: endpoint that receives token and topic via MagicLink.

* Proposal to add Passwordless login feature to the endpoint user.

* remove currency run check

* Commit from GitHub Actions (Publish APIs & Clients)

* Create downstream.yml

* Commit from GitHub Actions (Publish APIs & Clients)

* update todo

* Commit from GitHub Actions (Publish APIs & Clients)

* Update publish.yml

* Commit from GitHub Actions (Publish APIs & Clients)

* Update publish.yml

* Commit from GitHub Actions (Publish APIs & Clients)

* Update and rename publish.yml to generate.yml

* Update generate.yml

* Commit from GitHub Actions (Generate Clients & Examples)

* Commit from GitHub Actions (Generate Clients & Examples)

* add comments

* Commit from GitHub Actions (Generate Clients & Examples)

* move otp to auth category

* charge for user verification

* Commit from GitHub Actions (Generate Clients & Examples)

* Update user.proto

* Commit from GitHub Actions (Generate Clients & Examples)

* Commit from GitHub Actions (Generate Clients & Examples)

* Change js client git repo url (#249)

* Commit from GitHub Actions (Generate Clients & Examples)

* use tableName func for Count

* Commit from GitHub Actions (Generate Clients & Examples)

* update notes example

* Commit from GitHub Actions (Generate Clients & Examples)

* Update .gitignore

* Update .gitignore

* Commit from GitHub Actions (Generate Clients & Examples)

* add new endpoints SendMagicLink and VerifyToken to M3O user serivce

Signed-off-by: Daniel Joudat <danieljoudat@gmail.com>

* Update user.proto

* add examples to examples.json | convert isvalid to is_valid | add some extra comments in user.proto

Signed-off-by: Daniel Joudat <danieljoudat@gmail.com>

Co-authored-by: Asim Aslam <asim@aslam.me>
Co-authored-by: asim <asim@users.noreply.github.com>
Co-authored-by: Janos Dobronszki <dobronszki@gmail.com>
Co-authored-by: crufter <crufter@users.noreply.github.com>
This commit is contained in:
Daniel Joudat
2021-12-10 12:57:00 +03:00
committed by GitHub
parent f82bc634c1
commit 1d6234155a
6 changed files with 827 additions and 99 deletions

View File

@@ -6,13 +6,16 @@ import (
"errors"
"fmt"
"net/url"
"path"
"strings"
"time"
_struct "github.com/golang/protobuf/ptypes/struct"
"github.com/micro/micro/v3/service/config"
microerr "github.com/micro/micro/v3/service/errors"
"github.com/micro/micro/v3/service/logger"
db "github.com/micro/services/db/proto"
"github.com/micro/services/pkg/cache"
user "github.com/micro/services/user/proto"
"github.com/sendgrid/sendgrid-go"
@@ -463,3 +466,57 @@ func (domain *Domain) List(ctx context.Context, o, l int32) ([]*user.Account, er
}
return ret, nil
}
func (domain *Domain) CacheToken(ctx context.Context, token, id, email string, ttl int) error {
obj := &tokenObject{
Id: id,
Email: email,
}
expires := time.Now().Add(time.Duration(ttl) * time.Second)
err := cache.Context(ctx).Set(token, obj, expires)
return err
}
func (domain *Domain) SendMLE(fromName, toAddress, toUsername, subject, textContent, token, address, endpoint string) error {
if domain.sengridKey == "" {
return fmt.Errorf("empty email api key")
}
from := mail.NewEmail(fromName, "support@m3o.com")
to := mail.NewEmail(toUsername, toAddress)
textContent = strings.Replace(textContent, "$micro_verification_link", fmt.Sprint("https://", path.Join(address, endpoint, token)), -1)
message := mail.NewSingleEmail(from, subject, to, textContent, "")
client := sendgrid.NewSendClient(domain.sengridKey)
response, err := client.Send(message)
logger.Info(response)
return err
}
func (domain *Domain) CacheReadToken(ctx context.Context, token string) (string, string, error) {
if token == "" {
return "", "", errors.New("token empty")
}
var obj tokenObject
expires, err := cache.Context(ctx).Get(token, obj)
if err == cache.ErrNotFound {
return "", "", errors.New("token not found")
} else if time.Until(expires).Seconds() < 0 {
return "", "", errors.New("token expired")
} else if err != nil {
return "", "", microerr.InternalServerError("CacheReadToken", err.Error())
}
return obj.Id, obj.Email, nil
}
type tokenObject struct {
Id string
Email string
}