Skip to content

Commit

Permalink
Removed template and simplified logic for invite URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
glpatcern committed Jun 7, 2023
1 parent 580a83f commit 4760782
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 39 deletions.
9 changes: 0 additions & 9 deletions internal/http/services/sciencemesh/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,3 @@ func (h *tokenHandler) initSubjectTemplate(subjTempl string) error {
h.tplSubj = tpl
return nil
}

func (h *tokenHandler) initInviteLinkTemplate(inviteTempl string) error {
tpl, err := template.New("tpl_invite").Parse(inviteTempl)
if err != nil {
return err
}
h.tplInviteLink = tpl
return nil
}
39 changes: 9 additions & 30 deletions internal/http/services/sciencemesh/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"html/template"
"mime"
"net/http"
"strings"

gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
Expand All @@ -38,16 +37,14 @@ import (
"github.com/cs3org/reva/pkg/smtpclient"
)

const defaultInviteLinkPrefix = "{{.MeshDirectoryURL}}?token={{.Token}}&providerDomain="

type tokenHandler struct {
gatewayClient gateway.GatewayAPIClient
smtpCredentials *smtpclient.SMTPCredentials
meshDirectoryURL string
providerDomain string

tplSubj *template.Template
tplBody *template.Template
tplInviteLink *template.Template
tplSubj *template.Template
tplBody *template.Template
}

func (h *tokenHandler) init(c *config) error {
Expand All @@ -74,7 +71,8 @@ func (h *tokenHandler) init(c *config) error {
if c.ProviderDomain == "" {
return errors.New("provider_domain missing from configuration")
}
return h.initInviteLinkTemplate(defaultInviteLinkPrefix + c.ProviderDomain)
h.providerDomain = c.ProviderDomain
return nil
}

type token struct {
Expand All @@ -84,12 +82,6 @@ type token struct {
InviteLink string `json:"invite_link"`
}

type inviteLinkParams struct {
User *userpb.User
Token string
MeshDirectoryURL string
}

// Generate generates an invitation token and if a recipient is specified,
// will send an email containing the link the user will use to accept the
// invitation.
Expand Down Expand Up @@ -134,24 +126,12 @@ func (h *tokenHandler) Generate(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}

func (h *tokenHandler) generateInviteLink(user *userpb.User, token *invitepb.InviteToken) (string, error) {
var inviteLink strings.Builder
if err := h.tplInviteLink.Execute(&inviteLink, inviteLinkParams{
User: user,
Token: token.Token,
MeshDirectoryURL: h.meshDirectoryURL,
}); err != nil {
return "", err
}

return inviteLink.String(), nil
func (h *tokenHandler) generateInviteLink(token *invitepb.InviteToken) string {
return h.meshDirectoryURL + "?token=" + token.Token + "&providerDomain=" + h.providerDomain
}

func (h *tokenHandler) prepareGenerateTokenResponse(user *userpb.User, tkn *invitepb.InviteToken) (*token, error) {
inviteLink, err := h.generateInviteLink(user, tkn)
if err != nil {
return nil, err
}
inviteLink := h.generateInviteLink(tkn)
res := &token{
Token: tkn.Token,
Description: tkn.Description,
Expand Down Expand Up @@ -281,9 +261,8 @@ func (h *tokenHandler) ListInvite(w http.ResponseWriter, r *http.Request) {
}

tokens := make([]*token, 0, len(res.InviteTokens))
user := ctxpkg.ContextMustGetUser(ctx)
for _, tkn := range res.InviteTokens {
inviteURL, err := h.generateInviteLink(user, tkn)
inviteURL := h.generateInviteLink(tkn)
if err != nil {
reqres.WriteError(w, r, reqres.APIErrorServerError, "error generating invite URL from OCM token", err)
return
Expand Down

0 comments on commit 4760782

Please sign in to comment.