Skip to content

Commit 678bb12

Browse files
Lizzy ThomsonStorj Robot
authored andcommitted
satellite/admin: create an endpoint for isAccountFrozen
Endpoint checks if an account is frozen or unfrozen. relates to #5398 Change-Id: I8ff44063870327e05cf729eaaaed1da6c5fa9217
1 parent 471f9e4 commit 678bb12

File tree

6 files changed

+54
-5
lines changed

6 files changed

+54
-5
lines changed

satellite/api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,8 @@ func NewAPI(log *zap.Logger, full *identity.FullIdentity, db DB,
599599
return nil, errs.Combine(err, peer.Close())
600600
}
601601

602+
accountFreezeService := console.NewAccountFreezeService(db.Console().AccountFreezeEvents(), db.Console().Users(), db.Console().Projects())
603+
602604
peer.Console.Endpoint = consoleweb.NewServer(
603605
peer.Log.Named("console:endpoint"),
604606
consoleConfig,
@@ -608,6 +610,7 @@ func NewAPI(log *zap.Logger, full *identity.FullIdentity, db DB,
608610
peer.Marketing.PartnersService,
609611
peer.Analytics.Service,
610612
peer.ABTesting.Service,
613+
accountFreezeService,
611614
peer.Console.Listener,
612615
config.Payments.StripeCoinPayments.StripePublicKey,
613616
config.Payments.UsagePrice,

satellite/console/consoleweb/consoleapi/auth.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@ type Auth struct {
5454
ActivateAccountURL string
5555
SatelliteName string
5656
service *console.Service
57+
accountFreezeService *console.AccountFreezeService
5758
analytics *analytics.Service
5859
mailService *mailservice.Service
5960
cookieAuth *consolewebauth.CookieAuth
6061
partners *rewards.PartnersService
6162
}
6263

6364
// NewAuth is a constructor for api auth controller.
64-
func NewAuth(log *zap.Logger, service *console.Service, mailService *mailservice.Service, cookieAuth *consolewebauth.CookieAuth, partners *rewards.PartnersService, analytics *analytics.Service, satelliteName string, externalAddress string, letUsKnowURL string, termsAndConditionsURL string, contactInfoURL string, generalRequestURL string) *Auth {
65+
func NewAuth(log *zap.Logger, service *console.Service, accountFreezeService *console.AccountFreezeService, mailService *mailservice.Service, cookieAuth *consolewebauth.CookieAuth, partners *rewards.PartnersService, analytics *analytics.Service, satelliteName string, externalAddress string, letUsKnowURL string, termsAndConditionsURL string, contactInfoURL string, generalRequestURL string) *Auth {
6566
return &Auth{
6667
log: log,
6768
ExternalAddress: externalAddress,
@@ -74,6 +75,7 @@ func NewAuth(log *zap.Logger, service *console.Service, mailService *mailservice
7475
CancelPasswordRecoveryURL: externalAddress + "cancel-password-recovery/",
7576
ActivateAccountURL: externalAddress + "activation/",
7677
service: service,
78+
accountFreezeService: accountFreezeService,
7779
mailService: mailService,
7880
cookieAuth: cookieAuth,
7981
partners: partners,
@@ -377,6 +379,38 @@ func loadSession(req *http.Request) string {
377379
return sessionCookie.Value
378380
}
379381

382+
// IsAccountFrozen checks to see if an account is frozen.
383+
func (a *Auth) IsAccountFrozen(w http.ResponseWriter, r *http.Request) {
384+
type FrozenResult struct {
385+
Frozen bool `json:"frozen"`
386+
}
387+
388+
ctx := r.Context()
389+
var err error
390+
defer mon.Task()(&ctx)(&err)
391+
392+
userID, err := a.service.GetUserID(ctx)
393+
if err != nil {
394+
a.serveJSONError(w, err)
395+
return
396+
}
397+
398+
frozenBool, err := a.accountFreezeService.IsUserFrozen(ctx, userID)
399+
if err != nil {
400+
a.serveJSONError(w, err)
401+
return
402+
}
403+
404+
w.Header().Set("Content-Type", "application/json")
405+
err = json.NewEncoder(w).Encode(FrozenResult{
406+
Frozen: frozenBool,
407+
})
408+
if err != nil {
409+
a.log.Error("could not encode account status", zap.Error(ErrAuthAPI.Wrap(err)))
410+
return
411+
}
412+
}
413+
380414
// UpdateAccount updates user's full name and short name.
381415
func (a *Auth) UpdateAccount(w http.ResponseWriter, r *http.Request) {
382416
ctx := r.Context()

satellite/console/consoleweb/consoleapi/auth_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func TestDeleteAccount(t *testing.T) {
293293

294294
actualHandler := func(r *http.Request) (status int, body []byte) {
295295
rr := httptest.NewRecorder()
296-
authController := consoleapi.NewAuth(log, nil, nil, nil, nil, nil, "", "", "", "", "", "")
296+
authController := consoleapi.NewAuth(log, nil, nil, nil, nil, nil, nil, "", "", "", "", "", "")
297297
authController.DeleteAccount(rr, r)
298298

299299
//nolint:bodyclose

satellite/console/consoleweb/endpoints_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ func TestAuth(t *testing.T) {
8686
require.NotEmpty(test.t, userIdentifier.ID)
8787
}
8888

89+
{ // Get_FreezeStatus
90+
resp, body := test.request(http.MethodGet, "/auth/account/freezestatus", nil)
91+
require.Equal(test.t, http.StatusOK, resp.StatusCode)
92+
require.Contains(test.t, body, "frozen")
93+
94+
var freezestatus struct{ Frozen bool }
95+
require.NoError(test.t, json.Unmarshal([]byte(body), &freezestatus))
96+
require.Equal(test.t, http.StatusOK, resp.StatusCode)
97+
require.False(test.t, freezestatus.Frozen)
98+
}
99+
89100
{ // Logout
90101
resp, _ := test.request(http.MethodPost, "/auth/logout", nil)
91102
cookie := findCookie(resp, "_tokenKey")

satellite/console/consoleweb/server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func (a *apiAuth) RemoveAuthCookie(w http.ResponseWriter) {
206206
}
207207

208208
// NewServer creates new instance of console server.
209-
func NewServer(logger *zap.Logger, config Config, service *console.Service, oidcService *oidc.Service, mailService *mailservice.Service, partners *rewards.PartnersService, analytics *analytics.Service, abTesting *abtesting.Service, listener net.Listener, stripePublicKey string, usagePrice paymentsconfig.ProjectUsagePrice, nodeURL storj.NodeURL) *Server {
209+
func NewServer(logger *zap.Logger, config Config, service *console.Service, oidcService *oidc.Service, mailService *mailservice.Service, partners *rewards.PartnersService, analytics *analytics.Service, abTesting *abtesting.Service, accountFreezeService *console.AccountFreezeService, listener net.Listener, stripePublicKey string, usagePrice paymentsconfig.ProjectUsagePrice, nodeURL storj.NodeURL) *Server {
210210
server := Server{
211211
log: logger,
212212
config: config,
@@ -278,12 +278,13 @@ func NewServer(logger *zap.Logger, config Config, service *console.Service, oidc
278278
server.withAuth(http.HandlerFunc(usageLimitsController.DailyUsage)),
279279
).Methods(http.MethodGet)
280280

281-
authController := consoleapi.NewAuth(logger, service, mailService, server.cookieAuth, partners, server.analytics, config.SatelliteName, server.config.ExternalAddress, config.LetUsKnowURL, config.TermsAndConditionsURL, config.ContactInfoURL, config.GeneralRequestURL)
281+
authController := consoleapi.NewAuth(logger, service, accountFreezeService, mailService, server.cookieAuth, partners, server.analytics, config.SatelliteName, server.config.ExternalAddress, config.LetUsKnowURL, config.TermsAndConditionsURL, config.ContactInfoURL, config.GeneralRequestURL)
282282
authRouter := router.PathPrefix("/api/v0/auth").Subrouter()
283283
authRouter.Handle("/account", server.withAuth(http.HandlerFunc(authController.GetAccount))).Methods(http.MethodGet)
284284
authRouter.Handle("/account", server.withAuth(http.HandlerFunc(authController.UpdateAccount))).Methods(http.MethodPatch)
285285
authRouter.Handle("/account/change-email", server.withAuth(http.HandlerFunc(authController.ChangeEmail))).Methods(http.MethodPost)
286286
authRouter.Handle("/account/change-password", server.withAuth(http.HandlerFunc(authController.ChangePassword))).Methods(http.MethodPost)
287+
authRouter.Handle("/account/freezestatus", server.withAuth(http.HandlerFunc(authController.IsAccountFrozen))).Methods(http.MethodGet)
287288
authRouter.Handle("/account/delete", server.withAuth(http.HandlerFunc(authController.DeleteAccount))).Methods(http.MethodPost)
288289
authRouter.Handle("/mfa/enable", server.withAuth(http.HandlerFunc(authController.EnableUserMFA))).Methods(http.MethodPost)
289290
authRouter.Handle("/mfa/disable", server.withAuth(http.HandlerFunc(authController.DisableUserMFA))).Methods(http.MethodPost)

satellite/console/emailreminders/chore.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (chore *Chore) Run(ctx context.Context) (err error) {
8787
chore.log.Error("error generating activation token", zap.Error(err))
8888
return nil
8989
}
90-
authController := consoleapi.NewAuth(chore.log, nil, nil, nil, nil, nil, "", chore.address, "", "", "", "")
90+
authController := consoleapi.NewAuth(chore.log, nil, nil, nil, nil, nil, nil, "", chore.address, "", "", "", "")
9191

9292
link := authController.ActivateAccountURL + "?token=" + token
9393
userName := u.ShortName

0 commit comments

Comments
 (0)