2019-01-24 16:26:36 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package consoleweb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2019-04-10 00:14:19 +01:00
|
|
|
"html/template"
|
2019-08-08 13:12:39 +01:00
|
|
|
"mime"
|
2019-01-24 16:26:36 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2019-08-08 13:12:39 +01:00
|
|
|
"net/url"
|
2019-04-10 00:14:19 +01:00
|
|
|
"path"
|
2019-01-24 16:26:36 +00:00
|
|
|
"path/filepath"
|
2019-03-19 17:55:43 +00:00
|
|
|
"strconv"
|
2019-03-26 15:56:16 +00:00
|
|
|
"strings"
|
2019-04-10 00:14:19 +01:00
|
|
|
"time"
|
2019-01-24 16:26:36 +00:00
|
|
|
|
|
|
|
"github.com/graphql-go/graphql"
|
2019-04-10 00:14:19 +01:00
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
2019-01-24 16:26:36 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
2019-02-06 13:19:14 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2019-06-04 12:55:38 +01:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2019-01-24 16:26:36 +00:00
|
|
|
|
|
|
|
"storj.io/storj/pkg/auth"
|
|
|
|
"storj.io/storj/satellite/console"
|
|
|
|
"storj.io/storj/satellite/console/consoleweb/consoleql"
|
2019-03-02 15:22:20 +00:00
|
|
|
"storj.io/storj/satellite/mailservice"
|
2019-01-24 16:26:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
authorization = "Authorization"
|
|
|
|
contentType = "Content-Type"
|
|
|
|
|
|
|
|
authorizationBearer = "Bearer "
|
|
|
|
|
|
|
|
applicationJSON = "application/json"
|
|
|
|
applicationGraphql = "application/graphql"
|
|
|
|
)
|
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
var (
|
|
|
|
// Error is satellite console error type
|
|
|
|
Error = errs.Class("satellite console error")
|
|
|
|
|
|
|
|
mon = monkit.Package()
|
|
|
|
)
|
2019-01-24 16:26:36 +00:00
|
|
|
|
|
|
|
// Config contains configuration for console web server
|
|
|
|
type Config struct {
|
2019-06-15 17:19:19 +01:00
|
|
|
Address string `help:"server address of the graphql api gateway and frontend app" devDefault:"127.0.0.1:8081" releaseDefault:":10100"`
|
2019-03-26 15:56:16 +00:00
|
|
|
StaticDir string `help:"path to static resources" default:""`
|
|
|
|
ExternalAddress string `help:"external endpoint of the satellite if hosted" default:""`
|
2019-06-03 14:46:57 +01:00
|
|
|
StripeKey string `help:"stripe api key" default:""`
|
2019-02-05 17:31:53 +00:00
|
|
|
|
2019-03-19 17:55:43 +00:00
|
|
|
// TODO: remove after Vanguard release
|
2019-05-28 15:32:51 +01:00
|
|
|
AuthToken string `help:"auth token needed for access to registration token creation endpoint" default:""`
|
|
|
|
AuthTokenSecret string `help:"secret used to sign auth tokens" releaseDefault:"" devDefault:"my-suppa-secret-key"`
|
2019-03-19 17:55:43 +00:00
|
|
|
|
2019-02-05 17:31:53 +00:00
|
|
|
PasswordCost int `internal:"true" help:"password hashing cost (0=automatic)" default:"0"`
|
2019-09-27 17:48:53 +01:00
|
|
|
|
|
|
|
SatelliteName string `help:"used to display at web satellite console" default:"Storj"`
|
|
|
|
SatelliteOperator string `help:"name of organization which set up satellite" default:"Storj Labs" `
|
|
|
|
LetUsKnowURL string `help:"url link to let us know page" default:"https://storjlabs.atlassian.net/servicedesk/customer/portals"`
|
|
|
|
ContactInfoURL string `help:"url link to contacts page" default:"https://forum.storj.io"`
|
|
|
|
TermsAndConditionsURL string `help:"url link to terms and conditions page" default:"https://storj.io/storage-sla/"`
|
|
|
|
SEO string `help:"used to communicate with web crawlers and other web robots" default:"User-agent: *\nDisallow: \nDisallow: /cgi-bin/"`
|
2019-01-24 16:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Server represents console web server
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Endpoint
|
2019-01-24 16:26:36 +00:00
|
|
|
type Server struct {
|
|
|
|
log *zap.Logger
|
|
|
|
|
2019-03-02 15:22:20 +00:00
|
|
|
config Config
|
|
|
|
service *console.Service
|
|
|
|
mailService *mailservice.Service
|
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
listener net.Listener
|
2019-03-02 15:22:20 +00:00
|
|
|
server http.Server
|
2019-01-24 16:26:36 +00:00
|
|
|
|
2019-08-13 13:37:01 +01:00
|
|
|
schema graphql.Schema
|
|
|
|
templates struct {
|
|
|
|
index *template.Template
|
|
|
|
pageNotFound *template.Template
|
|
|
|
usageReport *template.Template
|
|
|
|
resetPassword *template.Template
|
|
|
|
success *template.Template
|
|
|
|
activated *template.Template
|
|
|
|
}
|
2019-01-24 16:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer creates new instance of console server
|
2019-03-02 15:22:20 +00:00
|
|
|
func NewServer(logger *zap.Logger, config Config, service *console.Service, mailService *mailservice.Service, listener net.Listener) *Server {
|
2019-01-24 16:26:36 +00:00
|
|
|
server := Server{
|
2019-03-02 15:22:20 +00:00
|
|
|
log: logger,
|
|
|
|
config: config,
|
|
|
|
listener: listener,
|
|
|
|
service: service,
|
|
|
|
mailService: mailService,
|
2019-01-24 16:26:36 +00:00
|
|
|
}
|
|
|
|
|
2019-05-14 16:13:18 +01:00
|
|
|
logger.Sugar().Debugf("Starting Satellite UI on %s...", server.listener.Addr().String())
|
2019-02-28 20:12:52 +00:00
|
|
|
|
2019-03-26 15:56:16 +00:00
|
|
|
if server.config.ExternalAddress != "" {
|
|
|
|
if !strings.HasSuffix(server.config.ExternalAddress, "/") {
|
2019-05-29 14:14:25 +01:00
|
|
|
server.config.ExternalAddress += "/"
|
2019-03-26 15:56:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
server.config.ExternalAddress = "http://" + server.listener.Addr().String() + "/"
|
|
|
|
}
|
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
fs := http.FileServer(http.Dir(server.config.StaticDir))
|
|
|
|
|
|
|
|
mux.Handle("/api/graphql/v0", http.HandlerFunc(server.grapqlHandler))
|
|
|
|
|
|
|
|
if server.config.StaticDir != "" {
|
2019-03-08 14:01:11 +00:00
|
|
|
mux.Handle("/activation/", http.HandlerFunc(server.accountActivationHandler))
|
2019-04-10 20:16:10 +01:00
|
|
|
mux.Handle("/password-recovery/", http.HandlerFunc(server.passwordRecoveryHandler))
|
2019-05-13 16:53:52 +01:00
|
|
|
mux.Handle("/cancel-password-recovery/", http.HandlerFunc(server.cancelPasswordRecoveryHandler))
|
2019-03-19 17:55:43 +00:00
|
|
|
mux.Handle("/registrationToken/", http.HandlerFunc(server.createRegistrationTokenHandler))
|
2019-04-10 00:14:19 +01:00
|
|
|
mux.Handle("/usage-report/", http.HandlerFunc(server.bucketUsageReportHandler))
|
2019-08-08 13:12:39 +01:00
|
|
|
mux.Handle("/static/", server.gzipHandler(http.StripPrefix("/static", fs)))
|
2019-09-09 19:33:05 +01:00
|
|
|
mux.Handle("/robots.txt", http.HandlerFunc(server.seoHandler))
|
2019-03-08 14:01:11 +00:00
|
|
|
mux.Handle("/", http.HandlerFunc(server.appHandler))
|
2019-01-24 16:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
server.server = http.Server{
|
2019-09-20 18:40:26 +01:00
|
|
|
Handler: mux,
|
|
|
|
MaxHeaderBytes: ContentLengthLimit.Int(),
|
2019-01-24 16:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &server
|
|
|
|
}
|
|
|
|
|
2019-08-13 13:37:01 +01:00
|
|
|
// Run starts the server that host webapp and api endpoint
|
|
|
|
func (server *Server) Run(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
server.schema, err = consoleql.CreateSchema(server.log, server.service, server.mailService)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = server.initializeTemplates()
|
|
|
|
if err != nil {
|
|
|
|
// TODO: should it return error if some template can not be initialized or just log about it?
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
var group errgroup.Group
|
|
|
|
group.Go(func() error {
|
|
|
|
<-ctx.Done()
|
2019-08-22 12:40:15 +01:00
|
|
|
return server.server.Shutdown(context.Background())
|
2019-08-13 13:37:01 +01:00
|
|
|
})
|
|
|
|
group.Go(func() error {
|
|
|
|
defer cancel()
|
|
|
|
return server.server.Serve(server.listener)
|
|
|
|
})
|
|
|
|
|
|
|
|
return group.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes server and underlying listener
|
|
|
|
func (server *Server) Close() error {
|
|
|
|
return server.server.Close()
|
|
|
|
}
|
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
// appHandler is web app http handler function
|
2019-08-13 13:37:01 +01:00
|
|
|
func (server *Server) appHandler(w http.ResponseWriter, r *http.Request) {
|
2019-07-30 11:13:24 +01:00
|
|
|
header := w.Header()
|
|
|
|
|
|
|
|
cspValues := []string{
|
|
|
|
"default-src 'self'",
|
|
|
|
"script-src 'self' *.stripe.com cdn.segment.com",
|
|
|
|
"frame-src 'self' *.stripe.com",
|
|
|
|
"img-src 'self' data:",
|
|
|
|
}
|
|
|
|
|
2019-09-09 19:33:05 +01:00
|
|
|
header.Set(contentType, "text/html; charset=UTF-8")
|
2019-07-30 11:13:24 +01:00
|
|
|
header.Set("Content-Security-Policy", strings.Join(cspValues, "; "))
|
2019-09-09 19:33:05 +01:00
|
|
|
header.Set("X-Content-Type-Options", "nosniff")
|
2019-07-30 11:13:24 +01:00
|
|
|
|
2019-08-13 13:37:01 +01:00
|
|
|
if server.templates.index == nil || server.templates.index.Execute(w, nil) != nil {
|
|
|
|
server.log.Error("satellite/console/server: index template could not be executed")
|
|
|
|
server.serveError(w, r, http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2019-01-24 16:26:36 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 00:14:19 +01:00
|
|
|
// bucketUsageReportHandler generate bucket usage report page for project
|
2019-08-08 13:12:39 +01:00
|
|
|
func (server *Server) bucketUsageReportHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
2019-04-10 00:14:19 +01:00
|
|
|
var err error
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-04-10 00:14:19 +01:00
|
|
|
|
2019-10-04 08:23:52 +01:00
|
|
|
tokenCookie, err := r.Cookie("_tokenKey")
|
2019-09-13 16:38:29 +01:00
|
|
|
if err != nil {
|
|
|
|
server.serveError(w, r, http.StatusUnauthorized)
|
2019-04-10 00:14:19 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-08 13:12:39 +01:00
|
|
|
auth, err := server.service.Authorize(auth.WithAPIKey(ctx, []byte(tokenCookie.Value)))
|
2019-04-10 00:14:19 +01:00
|
|
|
if err != nil {
|
2019-09-13 16:38:29 +01:00
|
|
|
server.serveError(w, r, http.StatusUnauthorized)
|
2019-04-10 00:14:19 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-13 16:38:29 +01:00
|
|
|
ctx = console.WithAuth(ctx, auth)
|
2019-04-10 00:14:19 +01:00
|
|
|
|
|
|
|
// parse query params
|
2019-09-13 16:38:29 +01:00
|
|
|
projectID, err := uuid.Parse(r.URL.Query().Get("projectID"))
|
2019-04-10 00:14:19 +01:00
|
|
|
if err != nil {
|
2019-09-13 16:38:29 +01:00
|
|
|
server.serveError(w, r, http.StatusBadRequest)
|
2019-04-10 00:14:19 +01:00
|
|
|
return
|
|
|
|
}
|
2019-08-08 13:12:39 +01:00
|
|
|
sinceStamp, err := strconv.ParseInt(r.URL.Query().Get("since"), 10, 64)
|
2019-04-10 00:14:19 +01:00
|
|
|
if err != nil {
|
2019-09-13 16:38:29 +01:00
|
|
|
server.serveError(w, r, http.StatusBadRequest)
|
2019-04-10 00:14:19 +01:00
|
|
|
return
|
|
|
|
}
|
2019-08-08 13:12:39 +01:00
|
|
|
beforeStamp, err := strconv.ParseInt(r.URL.Query().Get("before"), 10, 64)
|
2019-04-10 00:14:19 +01:00
|
|
|
if err != nil {
|
2019-09-13 16:38:29 +01:00
|
|
|
server.serveError(w, r, http.StatusBadRequest)
|
2019-04-10 00:14:19 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-13 16:38:29 +01:00
|
|
|
since := time.Unix(sinceStamp, 0)
|
|
|
|
before := time.Unix(beforeStamp, 0)
|
2019-04-23 13:56:15 +01:00
|
|
|
|
2019-08-08 13:12:39 +01:00
|
|
|
server.log.Debug("querying bucket usage report",
|
2019-06-18 00:37:44 +01:00
|
|
|
zap.Stringer("projectID", projectID),
|
|
|
|
zap.Stringer("since", since),
|
|
|
|
zap.Stringer("before", before))
|
2019-04-10 00:14:19 +01:00
|
|
|
|
2019-08-08 13:12:39 +01:00
|
|
|
bucketRollups, err := server.service.GetBucketUsageRollups(ctx, *projectID, since, before)
|
2019-04-10 00:14:19 +01:00
|
|
|
if err != nil {
|
2019-09-13 16:38:29 +01:00
|
|
|
server.log.Error("bucket usage report error", zap.Error(err))
|
|
|
|
server.serveError(w, r, http.StatusInternalServerError)
|
2019-04-10 00:14:19 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-13 13:37:01 +01:00
|
|
|
if err = server.templates.usageReport.Execute(w, bucketRollups); err != nil {
|
2019-09-13 16:38:29 +01:00
|
|
|
server.log.Error("bucket usage report error", zap.Error(err))
|
2019-04-10 00:14:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 17:55:43 +00:00
|
|
|
// accountActivationHandler is web app http handler function
|
2019-08-08 13:12:39 +01:00
|
|
|
func (server *Server) createRegistrationTokenHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(nil)
|
2019-03-19 17:55:43 +00:00
|
|
|
w.Header().Set(contentType, applicationJSON)
|
|
|
|
|
|
|
|
var response struct {
|
|
|
|
Secret string `json:"secret"`
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := json.NewEncoder(w).Encode(&response)
|
|
|
|
if err != nil {
|
2019-08-08 13:12:39 +01:00
|
|
|
server.log.Error(err.Error())
|
2019-03-19 17:55:43 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-08-08 13:12:39 +01:00
|
|
|
authToken := r.Header.Get("Authorization")
|
|
|
|
if authToken != server.config.AuthToken {
|
2019-03-19 17:55:43 +00:00
|
|
|
w.WriteHeader(401)
|
|
|
|
response.Error = "unauthorized"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-08 13:12:39 +01:00
|
|
|
projectsLimitInput := r.URL.Query().Get("projectsLimit")
|
2019-03-19 17:55:43 +00:00
|
|
|
|
|
|
|
projectsLimit, err := strconv.Atoi(projectsLimitInput)
|
|
|
|
if err != nil {
|
|
|
|
response.Error = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-08 13:12:39 +01:00
|
|
|
token, err := server.service.CreateRegToken(ctx, projectsLimit)
|
2019-03-19 17:55:43 +00:00
|
|
|
if err != nil {
|
|
|
|
response.Error = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Secret = token.Secret.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// accountActivationHandler is web app http handler function
|
2019-08-08 13:12:39 +01:00
|
|
|
func (server *Server) accountActivationHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(nil)
|
2019-08-08 13:12:39 +01:00
|
|
|
activationToken := r.URL.Query().Get("token")
|
2019-03-08 14:01:11 +00:00
|
|
|
|
2019-08-08 13:12:39 +01:00
|
|
|
err := server.service.ActivateAccount(ctx, activationToken)
|
2019-03-08 14:01:11 +00:00
|
|
|
if err != nil {
|
2019-08-08 13:12:39 +01:00
|
|
|
server.log.Error("activation: failed to activate account",
|
2019-04-09 13:20:29 +01:00
|
|
|
zap.String("token", activationToken),
|
|
|
|
zap.Error(err))
|
|
|
|
|
2019-08-13 13:37:01 +01:00
|
|
|
// TODO: when new error pages will be created - change http.StatusNotFound on appropriate one
|
|
|
|
server.serveError(w, r, http.StatusNotFound)
|
2019-03-08 14:01:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-13 13:37:01 +01:00
|
|
|
if err = server.templates.activated.Execute(w, nil); err != nil {
|
|
|
|
server.log.Error("satellite/console/server: account activated template could not be executed", zap.Error(err))
|
|
|
|
server.serveError(w, r, http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2019-03-08 14:01:11 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 13:12:39 +01:00
|
|
|
func (server *Server) passwordRecoveryHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(nil)
|
2019-08-13 13:37:01 +01:00
|
|
|
|
2019-08-08 13:12:39 +01:00
|
|
|
recoveryToken := r.URL.Query().Get("token")
|
2019-04-10 20:16:10 +01:00
|
|
|
if len(recoveryToken) == 0 {
|
2019-08-13 13:37:01 +01:00
|
|
|
server.serveError(w, r, http.StatusNotFound)
|
2019-04-10 20:16:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-08 13:12:39 +01:00
|
|
|
switch r.Method {
|
2019-06-26 14:36:47 +01:00
|
|
|
case http.MethodPost:
|
2019-08-08 13:12:39 +01:00
|
|
|
err := r.ParseForm()
|
2019-04-10 20:16:10 +01:00
|
|
|
if err != nil {
|
2019-08-13 13:37:01 +01:00
|
|
|
server.serveError(w, r, http.StatusNotFound)
|
2019-07-25 16:01:44 +01:00
|
|
|
return
|
2019-04-10 20:16:10 +01:00
|
|
|
}
|
|
|
|
|
2019-08-08 13:12:39 +01:00
|
|
|
password := r.FormValue("password")
|
|
|
|
passwordRepeat := r.FormValue("passwordRepeat")
|
2019-04-10 20:16:10 +01:00
|
|
|
if strings.Compare(password, passwordRepeat) != 0 {
|
2019-08-13 13:37:01 +01:00
|
|
|
server.serveError(w, r, http.StatusNotFound)
|
2019-04-10 20:16:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-08 13:12:39 +01:00
|
|
|
err = server.service.ResetPassword(ctx, recoveryToken, password)
|
2019-04-10 20:16:10 +01:00
|
|
|
if err != nil {
|
2019-08-13 13:37:01 +01:00
|
|
|
server.serveError(w, r, http.StatusNotFound)
|
2019-07-25 16:01:44 +01:00
|
|
|
return
|
2019-04-10 20:16:10 +01:00
|
|
|
}
|
2019-07-25 16:01:44 +01:00
|
|
|
|
2019-08-13 13:37:01 +01:00
|
|
|
if err := server.templates.success.Execute(w, nil); err != nil {
|
|
|
|
server.log.Error("satellite/console/server: success reset password template could not be executed", zap.Error(err))
|
|
|
|
server.serveError(w, r, http.StatusNotFound)
|
2019-07-25 16:01:44 +01:00
|
|
|
return
|
2019-04-10 20:16:10 +01:00
|
|
|
}
|
2019-08-13 13:37:01 +01:00
|
|
|
case http.MethodGet:
|
|
|
|
if err := server.templates.resetPassword.Execute(w, nil); err != nil {
|
|
|
|
server.log.Error("satellite/console/server: reset password template could not be executed", zap.Error(err))
|
|
|
|
server.serveError(w, r, http.StatusNotFound)
|
2019-07-25 16:01:44 +01:00
|
|
|
return
|
2019-04-10 20:16:10 +01:00
|
|
|
}
|
2019-06-26 14:36:47 +01:00
|
|
|
default:
|
2019-08-13 13:37:01 +01:00
|
|
|
server.serveError(w, r, http.StatusNotFound)
|
2019-06-26 14:36:47 +01:00
|
|
|
return
|
2019-04-10 20:16:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-08 13:12:39 +01:00
|
|
|
func (server *Server) cancelPasswordRecoveryHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(nil)
|
2019-08-08 13:12:39 +01:00
|
|
|
recoveryToken := r.URL.Query().Get("token")
|
2019-05-13 16:53:52 +01:00
|
|
|
|
|
|
|
// No need to check error as we anyway redirect user to support page
|
2019-08-08 13:12:39 +01:00
|
|
|
_ = server.service.RevokeResetPasswordToken(ctx, recoveryToken)
|
2019-05-13 16:53:52 +01:00
|
|
|
|
2019-08-13 13:37:01 +01:00
|
|
|
// TODO: Should place this link to config
|
2019-08-08 13:12:39 +01:00
|
|
|
http.Redirect(w, r, "https://storjlabs.atlassian.net/servicedesk/customer/portals", http.StatusSeeOther)
|
2019-05-13 16:53:52 +01:00
|
|
|
}
|
|
|
|
|
2019-08-13 13:37:01 +01:00
|
|
|
func (server *Server) serveError(w http.ResponseWriter, r *http.Request, status int) {
|
|
|
|
// TODO: show different error pages depend on status
|
|
|
|
// F.e. switch(status)
|
|
|
|
// case http.StatusNotFound: server.executeTemplate(w, r, notFound, nil)
|
|
|
|
// case http.StatusInternalServerError: server.executeTemplate(w, r, internalError, nil)
|
|
|
|
w.WriteHeader(status)
|
|
|
|
|
|
|
|
if err := server.templates.pageNotFound.Execute(w, nil); err != nil {
|
|
|
|
server.log.Error("error occurred in console/server", zap.Error(err))
|
|
|
|
}
|
2019-04-10 20:16:10 +01:00
|
|
|
}
|
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
// grapqlHandler is graphql endpoint http handler function
|
2019-08-08 13:12:39 +01:00
|
|
|
func (server *Server) grapqlHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(nil)
|
2019-01-24 16:26:36 +00:00
|
|
|
w.Header().Set(contentType, applicationJSON)
|
|
|
|
|
2019-08-08 13:12:39 +01:00
|
|
|
token := getToken(r)
|
2019-09-20 18:40:26 +01:00
|
|
|
query, err := getQuery(w, r)
|
2019-01-24 16:26:36 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
ctx = auth.WithAPIKey(ctx, []byte(token))
|
2019-08-08 13:12:39 +01:00
|
|
|
auth, err := server.service.Authorize(ctx)
|
2019-01-24 16:26:36 +00:00
|
|
|
if err != nil {
|
|
|
|
ctx = console.WithAuthFailure(ctx, err)
|
|
|
|
} else {
|
|
|
|
ctx = console.WithAuth(ctx, auth)
|
|
|
|
}
|
|
|
|
|
2019-03-02 15:22:20 +00:00
|
|
|
rootObject := make(map[string]interface{})
|
2019-03-26 15:56:16 +00:00
|
|
|
|
2019-08-08 13:12:39 +01:00
|
|
|
rootObject["origin"] = server.config.ExternalAddress
|
2019-03-08 14:01:11 +00:00
|
|
|
rootObject[consoleql.ActivationPath] = "activation/?token="
|
2019-04-10 20:16:10 +01:00
|
|
|
rootObject[consoleql.PasswordRecoveryPath] = "password-recovery/?token="
|
2019-05-13 16:53:52 +01:00
|
|
|
rootObject[consoleql.CancelPasswordRecoveryPath] = "cancel-password-recovery/?token="
|
2019-03-26 15:56:16 +00:00
|
|
|
rootObject[consoleql.SignInPath] = "login"
|
2019-09-27 17:48:53 +01:00
|
|
|
rootObject[consoleql.LetUsKnowURL] = server.config.LetUsKnowURL
|
|
|
|
rootObject[consoleql.ContactInfoURL] = server.config.ContactInfoURL
|
|
|
|
rootObject[consoleql.TermsAndConditionsURL] = server.config.TermsAndConditionsURL
|
2019-03-02 15:22:20 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
result := graphql.Do(graphql.Params{
|
2019-08-08 13:12:39 +01:00
|
|
|
Schema: server.schema,
|
2019-01-24 16:26:36 +00:00
|
|
|
Context: ctx,
|
|
|
|
RequestString: query.Query,
|
|
|
|
VariableValues: query.Variables,
|
|
|
|
OperationName: query.OperationName,
|
2019-03-02 15:22:20 +00:00
|
|
|
RootObject: rootObject,
|
2019-01-24 16:26:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(result)
|
|
|
|
if err != nil {
|
2019-08-08 13:12:39 +01:00
|
|
|
server.log.Error(err.Error())
|
2019-01-24 16:26:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-08 13:12:39 +01:00
|
|
|
sugar := server.log.Sugar()
|
2019-01-24 16:26:36 +00:00
|
|
|
sugar.Debug(result)
|
|
|
|
}
|
|
|
|
|
2019-09-09 19:33:05 +01:00
|
|
|
// seoHandler used to communicate with web crawlers and other web robots
|
|
|
|
func (server *Server) seoHandler(w http.ResponseWriter, req *http.Request) {
|
|
|
|
header := w.Header()
|
|
|
|
|
|
|
|
header.Set(contentType, mime.TypeByExtension(".txt"))
|
|
|
|
header.Set("X-Content-Type-Options", "nosniff")
|
|
|
|
|
2019-09-27 17:48:53 +01:00
|
|
|
_, err := w.Write([]byte(server.config.SEO))
|
2019-09-09 19:33:05 +01:00
|
|
|
if err != nil {
|
|
|
|
server.log.Error(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-08 13:12:39 +01:00
|
|
|
// gzipHandler is used to gzip static content to minify resources if browser support such decoding
|
|
|
|
func (server *Server) gzipHandler(fn http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
isGzipSupported := strings.Contains(r.Header.Get("Accept-Encoding"), "gzip")
|
|
|
|
extension := filepath.Ext(r.RequestURI)
|
|
|
|
// we have gzipped only fonts, js and css bundles
|
|
|
|
formats := map[string]bool{
|
|
|
|
".js": true,
|
|
|
|
".ttf": true,
|
|
|
|
".css": true,
|
|
|
|
}
|
|
|
|
isNeededFormatToGzip := formats[extension]
|
|
|
|
|
2019-08-13 13:37:01 +01:00
|
|
|
// because we have some static content outside of console frontend app.
|
|
|
|
// for example: 404 page, account activation, passsrowd reset, etc.
|
|
|
|
// TODO: find better solution, its a temporary fix
|
|
|
|
isFromStaticDir := strings.Contains(r.URL.Path, "/static/dist/")
|
|
|
|
|
2019-09-09 19:33:05 +01:00
|
|
|
w.Header().Set(contentType, mime.TypeByExtension(extension))
|
|
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
|
|
|
2019-08-08 13:12:39 +01:00
|
|
|
// in case if old browser doesn't support gzip decoding or if file extension is not recommended to gzip
|
|
|
|
// just return original file
|
2019-08-13 13:37:01 +01:00
|
|
|
if !isGzipSupported || !isNeededFormatToGzip || !isFromStaticDir {
|
2019-08-08 13:12:39 +01:00
|
|
|
fn.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Encoding", "gzip")
|
|
|
|
|
|
|
|
// updating request URL
|
|
|
|
newRequest := new(http.Request)
|
|
|
|
*newRequest = *r
|
|
|
|
newRequest.URL = new(url.URL)
|
|
|
|
*newRequest.URL = *r.URL
|
|
|
|
newRequest.URL.Path += ".gz"
|
|
|
|
|
|
|
|
fn.ServeHTTP(w, newRequest)
|
|
|
|
})
|
2019-01-24 16:26:36 +00:00
|
|
|
}
|
2019-08-13 13:37:01 +01:00
|
|
|
|
|
|
|
// initializeTemplates is used to initialize all templates
|
|
|
|
func (server *Server) initializeTemplates() (err error) {
|
|
|
|
server.templates.index, err = template.ParseFiles(filepath.Join(server.config.StaticDir, "dist", "index.html"))
|
|
|
|
if err != nil {
|
|
|
|
server.log.Error("dist folder is not generated. use 'npm run build' command", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
server.templates.activated, err = template.ParseFiles(filepath.Join(server.config.StaticDir, "static", "activation", "activated.html"))
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
server.templates.success, err = template.ParseFiles(filepath.Join(server.config.StaticDir, "static", "resetPassword", "success.html"))
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
server.templates.resetPassword, err = template.ParseFiles(filepath.Join(server.config.StaticDir, "static", "resetPassword", "resetPassword.html"))
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
server.templates.usageReport, err = template.ParseFiles(path.Join(server.config.StaticDir, "static", "reports", "usageReport.html"))
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
server.templates.pageNotFound, err = template.ParseFiles(path.Join(server.config.StaticDir, "static", "errors", "404.html"))
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|