satellite/console: add REST registration API endpoint (#3303)

This commit is contained in:
Nikolai Siedov 2019-10-17 19:34:27 +03:00 committed by GitHub
parent ee55d14f35
commit f4162bd33f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,8 +22,9 @@ import (
"github.com/zeebo/errs"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"gopkg.in/spacemonkeygo/monkit.v2"
monkit "gopkg.in/spacemonkeygo/monkit.v2"
"storj.io/storj/internal/post"
"storj.io/storj/pkg/auth"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/console/consoleweb/consoleapi"
@ -123,6 +124,7 @@ func NewServer(logger *zap.Logger, config Config, service *console.Service, mail
router.Handle("/api/v0/graphql", http.HandlerFunc(server.grapqlHandler))
router.Handle("/api/v0/token", http.HandlerFunc(server.tokenHandler))
router.Handle("/api/v0/register", http.HandlerFunc(server.registerHandler))
router.Handle("/registrationToken/", http.HandlerFunc(server.createRegistrationTokenHandler))
router.Handle("/robots.txt", http.HandlerFunc(server.seoHandler))
@ -233,6 +235,60 @@ func (server *Server) tokenHandler(w http.ResponseWriter, r *http.Request) {
}
}
// registerHandler registers new User.
func (server *Server) registerHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var err error
defer mon.Task()(&ctx)(&err)
var request struct {
UserInfo console.CreateUser `json:"userInfo"`
SecretInput string `json:"secret"`
ReferrerUserID string `json:"referrerUserID"`
}
err = json.NewDecoder(r.Body).Decode(&request)
if err != nil {
server.serveJSONError(w, http.StatusBadRequest, err)
return
}
secret, err := console.RegistrationSecretFromBase64(request.SecretInput)
if err != nil {
server.serveJSONError(w, http.StatusBadRequest, err)
return
}
user, err := server.service.CreateUser(ctx, request.UserInfo, secret, request.ReferrerUserID)
if err != nil {
server.serveJSONError(w, http.StatusInternalServerError, err)
return
}
token, err := server.service.GenerateActivationToken(ctx, user.ID, user.Email)
if err != nil {
server.serveJSONError(w, http.StatusInternalServerError, err)
return
}
link := server.config.ExternalAddress + consoleql.ActivationPath + token
server.mailService.SendRenderedAsync(
ctx,
[]post.Address{{Address: user.Email, Name: user.FullName}},
&consoleql.AccountActivationEmail{
ActivationLink: link,
Origin: server.config.ExternalAddress,
},
)
err = json.NewEncoder(w).Encode(&user.ID)
if err != nil {
server.log.Error("registration handler could not encode error", zap.Error(err))
return
}
}
// bucketUsageReportHandler generate bucket usage report page for project
func (server *Server) bucketUsageReportHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()