@@ -43,6 +43,7 @@ type UserManagementService interface {
4343 FreezeUser (ctx context.Context , userID uuid.UUID , request FreezeUserRequest ) api.HTTPError
4444 UnfreezeUser (ctx context.Context , userID uuid.UUID ) api.HTTPError
4545 DisableMFA (ctx context.Context , userID uuid.UUID ) api.HTTPError
46+ CreateRestKey (ctx context.Context , userID uuid.UUID , request CreateRestKeyRequest ) (* string , api.HTTPError )
4647}
4748
4849type ProjectManagementService interface {
@@ -128,6 +129,7 @@ func NewUserManagement(log *zap.Logger, mon *monkit.Scope, service UserManagemen
128129 usersRouter .HandleFunc ("/{userID}/freeze-events" , handler .handleFreezeUser ).Methods ("POST" )
129130 usersRouter .HandleFunc ("/{userID}/freeze-events" , handler .handleUnfreezeUser ).Methods ("DELETE" )
130131 usersRouter .HandleFunc ("/mfa/{userID}" , handler .handleDisableMFA ).Methods ("DELETE" )
132+ usersRouter .HandleFunc ("/rest-keys/{userID}" , handler .handleCreateRestKey ).Methods ("POST" )
131133
132134 return handler
133135}
@@ -574,6 +576,52 @@ func (h *UserManagementHandler) handleDisableMFA(w http.ResponseWriter, r *http.
574576 }
575577}
576578
579+ func (h * UserManagementHandler ) handleCreateRestKey (w http.ResponseWriter , r * http.Request ) {
580+ ctx := r .Context ()
581+ var err error
582+ defer h .mon .Task ()(& ctx )(& err )
583+
584+ w .Header ().Set ("Content-Type" , "application/json" )
585+
586+ userIDParam , ok := mux .Vars (r )["userID" ]
587+ if ! ok {
588+ api .ServeError (h .log , w , http .StatusBadRequest , errs .New ("missing userID route param" ))
589+ return
590+ }
591+
592+ userID , err := uuid .FromString (userIDParam )
593+ if err != nil {
594+ api .ServeError (h .log , w , http .StatusBadRequest , err )
595+ return
596+ }
597+
598+ payload := CreateRestKeyRequest {}
599+ if err = json .NewDecoder (r .Body ).Decode (& payload ); err != nil {
600+ api .ServeError (h .log , w , http .StatusBadRequest , err )
601+ return
602+ }
603+
604+ if err = h .auth .VerifyHost (r ); err != nil {
605+ api .ServeError (h .log , w , http .StatusForbidden , err )
606+ return
607+ }
608+
609+ if h .auth .IsRejected (w , r , 32768 ) {
610+ return
611+ }
612+
613+ retVal , httpErr := h .service .CreateRestKey (ctx , userID , payload )
614+ if httpErr .Err != nil {
615+ api .ServeError (h .log , w , httpErr .Status , httpErr .Err )
616+ return
617+ }
618+
619+ err = json .NewEncoder (w ).Encode (retVal )
620+ if err != nil {
621+ h .log .Debug ("failed to write json CreateRestKey response" , zap .Error (ErrUsersAPI .Wrap (err )))
622+ }
623+ }
624+
577625func (h * ProjectManagementHandler ) handleGetProject (w http.ResponseWriter , r * http.Request ) {
578626 ctx := r .Context ()
579627 var err error
@@ -598,7 +646,7 @@ func (h *ProjectManagementHandler) handleGetProject(w http.ResponseWriter, r *ht
598646 return
599647 }
600648
601- if h .auth .IsRejected (w , r , 65536 ) {
649+ if h .auth .IsRejected (w , r , 131072 ) {
602650 return
603651 }
604652
@@ -644,7 +692,7 @@ func (h *ProjectManagementHandler) handleUpdateProjectLimits(w http.ResponseWrit
644692 return
645693 }
646694
647- if h .auth .IsRejected (w , r , 131072 ) {
695+ if h .auth .IsRejected (w , r , 262144 ) {
648696 return
649697 }
650698
0 commit comments