Просмотр исходного кода

change billing redirect mechanism

Alexander Belanger 3 лет назад
Родитель
Сommit
2896475599
1 измененных файлов с 26 добавлено и 9 удалено
  1. 26 9
      api/server/handlers/billing/redirect_billing.go

+ 26 - 9
api/server/handlers/billing/redirect_billing.go

@@ -4,8 +4,10 @@ import (
 	"encoding/json"
 	"fmt"
 	"net/http"
+	"net/url"
 	"strings"
 
+	"github.com/gorilla/schema"
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/shared"
 	"github.com/porter-dev/porter/api/server/shared/apierrors"
@@ -33,7 +35,13 @@ type CreateBillingCookieRequest struct {
 }
 
 type CreateBillingCookieResponse struct {
-	Cookie string `json:"cookie"`
+	Token   string `json:"token"`
+	TokenID string `json:"token_id"`
+}
+
+type VerifyUserRequest struct {
+	TokenID string `schema:"token_id" form:"required"`
+	Token   string `schema:"token" form:"required"`
 }
 
 func (c *RedirectBillingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@@ -86,15 +94,24 @@ func (c *RedirectBillingHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
 
 	dst := &CreateBillingCookieResponse{}
 
-	if dst != nil {
-		err = json.NewDecoder(res.Body).Decode(dst)
+	err = json.NewDecoder(res.Body).Decode(dst)
 
-		if err != nil {
-			c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
-			return
-		}
+	if err != nil {
+		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	redirectData := &VerifyUserRequest{
+		TokenID: dst.TokenID,
+		Token:   dst.Token,
 	}
 
-	w.Header().Add("Set-Cookie", dst.Cookie)
-	http.Redirect(w, r, c.Config().ServerConf.BillingPublicServerURL, 302)
+	vals := make(map[string][]string)
+	err = schema.NewEncoder().Encode(redirectData, vals)
+
+	urlVals := url.Values(vals)
+	encodedURLVals := urlVals.Encode()
+
+	reqURL := fmt.Sprintf("%s/api/v1/verify?%s", c.Config().ServerConf.BillingPublicServerURL, encodedURLVals)
+	http.Redirect(w, r, reqURL, 302)
 }