gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

passwordchange.go (3378B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package user
     19 
     20 import (
     21 	"errors"
     22 	"net/http"
     23 
     24 	"github.com/gin-gonic/gin"
     25 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
     26 	apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util"
     27 	"github.com/superseriousbusiness/gotosocial/internal/gtserror"
     28 	"github.com/superseriousbusiness/gotosocial/internal/oauth"
     29 )
     30 
     31 // PasswordChangePOSTHandler swagger:operation POST /api/v1/user/password_change userPasswordChange
     32 //
     33 // Change the password of authenticated user.
     34 //
     35 // The parameters can also be given in the body of the request, as JSON, if the content-type is set to 'application/json'.
     36 // The parameters can also be given in the body of the request, as XML, if the content-type is set to 'application/xml'.
     37 //
     38 //	---
     39 //	tags:
     40 //	- user
     41 //
     42 //	consumes:
     43 //	- application/json
     44 //	- application/xml
     45 //	- application/x-www-form-urlencoded
     46 //
     47 //	produces:
     48 //	- application/json
     49 //
     50 //	security:
     51 //	- OAuth2 Bearer:
     52 //		- write:user
     53 //
     54 //	responses:
     55 //		'200':
     56 //			description: Change successful
     57 //		'400':
     58 //			description: bad request
     59 //		'401':
     60 //			description: unauthorized
     61 //		'403':
     62 //			description: forbidden
     63 //		'406':
     64 //			description: not acceptable
     65 //		'500':
     66 //			description: internal error
     67 func (m *Module) PasswordChangePOSTHandler(c *gin.Context) {
     68 	authed, err := oauth.Authed(c, true, true, true, true)
     69 	if err != nil {
     70 		apiutil.ErrorHandler(c, gtserror.NewErrorUnauthorized(err, err.Error()), m.processor.InstanceGetV1)
     71 		return
     72 	}
     73 
     74 	if _, err := apiutil.NegotiateAccept(c, apiutil.JSONAcceptHeaders...); err != nil {
     75 		apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1)
     76 		return
     77 	}
     78 
     79 	form := &apimodel.PasswordChangeRequest{}
     80 	if err := c.ShouldBind(form); err != nil {
     81 		apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
     82 		return
     83 	}
     84 
     85 	if form.OldPassword == "" {
     86 		err := errors.New("password change request missing field old_password")
     87 		apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
     88 		return
     89 	}
     90 
     91 	if form.NewPassword == "" {
     92 		err := errors.New("password change request missing field new_password")
     93 		apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
     94 		return
     95 	}
     96 
     97 	if errWithCode := m.processor.User().PasswordChange(c.Request.Context(), authed.User, form.OldPassword, form.NewPassword); errWithCode != nil {
     98 		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
     99 		return
    100 	}
    101 
    102 	c.JSON(http.StatusOK, gin.H{"status": "OK"})
    103 }