eintopf.info@v0.13.16/service/passwordrec/transport.go (about) 1 // Copyright (C) 2022 The Eintopf authors 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <https://www.gnu.org/licenses/>. 15 16 package passwordrec 17 18 import ( 19 "encoding/json" 20 "io" 21 "net/http" 22 23 "github.com/go-chi/chi/v5" 24 25 "eintopf.info/internal/xhttp" 26 ) 27 28 // Router returns a new passwordrec router. 29 func Router(service *Service) func(chi.Router) { 30 server := &server{service} 31 return func(r chi.Router) { 32 r.Options("/requestRecovery", xhttp.CorsHandler) 33 34 // swagger:route POST /passwordrec/requestRecovery passwordrec recoveryRequest 35 // 36 // Requests a password recovery. 37 // 38 // Responses: 39 // 200: passwordRequestSuccess 40 // 500: internalError 41 // 503: serviceUnavailable 42 r.Post("/requestRecovery", server.requestRecovery) 43 44 r.Options("/setPassword", xhttp.CorsHandler) 45 46 // swagger:route POST /passwordrec/setPassword passwordrec setPassword 47 // 48 // Sets the new password, using a recovery token. 49 // 50 // 51 // Responses: 52 // 200: passwordSetSuccess 53 // 400: badRequest 54 // 500: internalError 55 // 503: serviceUnavailable 56 r.Post("/setPassword", server.setPassword) 57 } 58 } 59 60 type server struct { 61 service *Service 62 } 63 64 func (s *server) requestRecovery(w http.ResponseWriter, r *http.Request) { 65 req, err := readRecoveryRequest(r) 66 if err != nil { 67 xhttp.WriteBadRequest(r.Context(), w, err) 68 return 69 } 70 err = s.service.RequestRecovery(r.Context(), req.RecoveryRequest.Email) 71 if err != nil { 72 xhttp.WriteInternalError(r.Context(), w, err) 73 return 74 } 75 w.Write([]byte{}) 76 } 77 78 // swagger:parameters recoveryRequest 79 type recoveryRequest struct { 80 // in:body 81 RecoveryRequest *RecoveryRequest 82 } 83 84 type RecoveryRequest struct { 85 Email string `json:"email"` 86 } 87 88 func readRecoveryRequest(r *http.Request) (*recoveryRequest, error) { 89 data, err := io.ReadAll(r.Body) 90 if err != nil { 91 return nil, err 92 } 93 req := &RecoveryRequest{} 94 err = json.Unmarshal(data, req) 95 if err != nil { 96 return nil, err 97 } 98 return &recoveryRequest{RecoveryRequest: req}, nil 99 } 100 101 func (s *server) setPassword(w http.ResponseWriter, r *http.Request) { 102 req, err := readSetPasswordRequest(r) 103 if err != nil { 104 xhttp.WriteBadRequest(r.Context(), w, err) 105 return 106 } 107 err = s.service.SetPassword(r.Context(), req.SetPasswordRequest.Token, req.SetPasswordRequest.Password) 108 if err != nil { 109 xhttp.WriteError(r.Context(), w, err) 110 return 111 } 112 w.Write([]byte{}) 113 } 114 115 // swagger:parameters setPassword 116 type setPasswordRequest struct { 117 // in:body 118 SetPasswordRequest *SetPasswordRequest 119 } 120 121 type SetPasswordRequest struct { 122 Token string `json:"token"` 123 Password string `json:"password"` 124 } 125 126 func readSetPasswordRequest(r *http.Request) (*setPasswordRequest, error) { 127 data, err := io.ReadAll(r.Body) 128 if err != nil { 129 return nil, err 130 } 131 req := &SetPasswordRequest{} 132 err = json.Unmarshal(data, req) 133 if err != nil { 134 return nil, err 135 } 136 return &setPasswordRequest{SetPasswordRequest: req}, nil 137 }