How to use setup method of lang Package

Best Gauge code snippet using lang.setup

auth.go

Source:auth.go Github

copy

Full Screen

...6 "net/http"7 "net/url"8 "regexp"9 "shopping-lists-and-recipes/internal/databases"10 "shopping-lists-and-recipes/internal/setup"11 "shopping-lists-and-recipes/packages/admin"12 "shopping-lists-and-recipes/packages/authentication"13 "shopping-lists-and-recipes/packages/messages"14 "shopping-lists-and-recipes/packages/shared"15 "strconv"16 uuid "github.com/gofrs/uuid"17)18// SignIn - обработчик для авторизации пользователя POST запросом19//20// POST21//22// ожидается заголовок ApiKey с API ключом23// ожидается заголовок Lang - Язык (ru или en)24// в теле запроса JSON объект AuthRequestData25// Email и пароль должны быть пропущены через через encodeURIComponent и btoa26func SignIn(w http.ResponseWriter, req *http.Request) {27 if !AuthBasic(w, req) {28 return29 }30 switch {31 case req.Method == http.MethodPost:32 // Читаем тело запроса в структуру33 var AuthRequest authentication.AuthRequestData34 err := json.NewDecoder(req.Body).Decode(&AuthRequest)35 if shared.HandleBadRequestError(setup.ServerSettings.Lang, w, req, err) {36 return37 }38 re := regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")39 if !re.MatchString(AuthRequest.Email) {40 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrBadEmail.Error(), ErrBadEmail, http.StatusBadRequest)41 return42 }43 AuthRequest.Password, err = url.QueryUnescape(AuthRequest.Password)44 if shared.HandleBadRequestError(setup.ServerSettings.Lang, w, req, err) {45 return46 }47 // Вызываем проверку пароля и выдачу токена48 secretauth(w, req, AuthRequest)49 default:50 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, shared.ErrNotAllowedMethod.Error(), shared.ErrNotAllowedMethod, http.StatusMethodNotAllowed)51 }52}53// SignUp - обработчик для регистрации пользователя POST запросом54//55// POST56//57// ожидается заголовок ApiKey с API ключом58// в теле запроса JSON объект AuthSignUpRequestData59// Email, имя пользователя и пароль должны быть60// пропущены через через encodeURIComponent и btoa61func SignUp(w http.ResponseWriter, req *http.Request) {62 if !AuthBasic(w, req) {63 return64 }65 switch {66 case req.Method == http.MethodPost:67 // Читаем тело запроса в структуру68 var SignUpRequest authentication.AuthSignUpRequestData69 err := json.NewDecoder(req.Body).Decode(&SignUpRequest)70 if shared.HandleBadRequestError(setup.ServerSettings.Lang, w, req, err) {71 return72 }73 re := regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")74 if !re.MatchString(SignUpRequest.Email) {75 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrBadEmail.Error(), ErrBadEmail, http.StatusBadRequest)76 return77 }78 SignUpRequest.Password, err = url.QueryUnescape(SignUpRequest.Password)79 if shared.HandleBadRequestError(setup.ServerSettings.Lang, w, req, err) {80 return81 }82 SignUpRequest.Name, err = url.QueryUnescape(SignUpRequest.Name)83 if shared.HandleBadRequestError(setup.ServerSettings.Lang, w, req, err) {84 return85 }86 // Создаём пользователя87 err = admin.CreateUser(&setup.ServerSettings.SQL, SignUpRequest.Name, SignUpRequest.Email, SignUpRequest.Password, setup.ServerSettings.SMTP.Use, setup.ServerSettings.SQL.ConnPool)88 if err != nil {89 if errors.Is(err, databases.ErrEmailIsOccupied) || errors.Is(err, admin.ErrBasicFieldsNotFilled) {90 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, err.Error(), err, http.StatusBadRequest)91 return92 }93 }94 if shared.HandleInternalServerError(setup.ServerSettings.Lang, w, req, err) {95 return96 }97 // Авторизация пользователя98 secretauth(w, req, ConvertToSignInRequest(SignUpRequest))99 // Отправляем письмо-подтверждение100 messages.SendEmailConfirmationLetter(&setup.ServerSettings.SQL, SignUpRequest.Email, shared.CurrentPrefix+req.Host, setup.ServerSettings.SQL.ConnPool)101 default:102 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, shared.ErrNotAllowedMethod.Error(), shared.ErrNotAllowedMethod, http.StatusMethodNotAllowed)103 }104}105// ResendEmail - отправляет письмо подтверждение повторно в ответ на POST запрос106//107// POST108//109// ожидается заголовок ApiKey с API ключом110// ожидается заголовок Email с электронной почтой111func ResendEmail(w http.ResponseWriter, req *http.Request) {112 if !AuthBasic(w, req) {113 return114 }115 switch {116 case req.Method == http.MethodPost:117 Email := req.Header.Get("Email")118 if len(Email) > 0 {119 re := regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")120 if !re.MatchString(Email) {121 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrBadEmail.Error(), ErrBadEmail, http.StatusBadRequest)122 return123 }124 mailexist, err := databases.PostgreSQLCheckUserMailExists(Email, setup.ServerSettings.SQL.ConnPool)125 if shared.HandleInternalServerError(setup.ServerSettings.Lang, w, req, err) {126 return127 }128 if mailexist {129 messages.SendEmailConfirmationLetter(&setup.ServerSettings.SQL, Email, shared.CurrentPrefix+req.Host, setup.ServerSettings.SQL.ConnPool)130 shared.HandleSuccessMessage(setup.ServerSettings.Lang, w, req, MesEmailSent)131 } else {132 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrBadEmail.Error(), ErrBadEmail, http.StatusBadRequest)133 }134 } else {135 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrBadEmail.Error(), ErrBadEmail, http.StatusBadRequest)136 }137 default:138 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, shared.ErrNotAllowedMethod.Error(), shared.ErrNotAllowedMethod, http.StatusMethodNotAllowed)139 }140}141// ConfirmEmail - обработчик вызывающий отправку подтверждения почты, принимает POST запрос142//143// POST144//145// ожидается заголовок ApiKey с API ключом146// ожидается заголовок Token с токеном для доступа147func ConfirmEmail(w http.ResponseWriter, req *http.Request) {148 if !AuthBasic(w, req) {149 return150 }151 switch {152 case req.Method == http.MethodPost:153 Token := req.Header.Get("Token")154 Token, err := url.QueryUnescape(Token)155 if shared.HandleBadRequestError(setup.ServerSettings.Lang, w, req, err) {156 return157 }158 // Если токен не протух и существует в базе записали подтверждение пользователя159 err = databases.PostgreSQLGetTokenConfirmEmail(Token, setup.ServerSettings.SQL.ConnPool)160 if err != nil {161 if shared.HandleOtherError(setup.ServerSettings.Lang, w, req, err.Error(), err, http.StatusBadRequest) {162 return163 }164 }165 shared.HandleSuccessMessage(setup.ServerSettings.Lang, w, req, MesEmailConfirmed)166 default:167 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, shared.ErrNotAllowedMethod.Error(), shared.ErrNotAllowedMethod, http.StatusMethodNotAllowed)168 }169}170// RequestResetEmail - отправляет письмо со ссылкой для сброса пароля171//172// POST173//174// ожидается заголовок ApiKey с API ключом175// ожидается заголовок Email с электронной почтой176func RequestResetEmail(w http.ResponseWriter, req *http.Request) {177 if !AuthBasic(w, req) {178 return179 }180 switch {181 case req.Method == http.MethodPost:182 Email := req.Header.Get("Email")183 if len(Email) > 0 {184 re := regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")185 if !re.MatchString(Email) {186 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrBadEmail.Error(), ErrBadEmail, http.StatusBadRequest)187 return188 }189 mailexist, err := databases.PostgreSQLCheckUserMailExists(Email, setup.ServerSettings.SQL.ConnPool)190 if shared.HandleInternalServerError(setup.ServerSettings.Lang, w, req, err) {191 return192 }193 if mailexist {194 messages.SendEmailPasswordReset(&setup.ServerSettings.SQL, Email, shared.CurrentPrefix+req.Host, setup.ServerSettings.SQL.ConnPool)195 shared.HandleSuccessMessage(setup.ServerSettings.Lang, w, req, MesEmailSent)196 } else {197 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrBadEmail.Error(), ErrBadEmail, http.StatusBadRequest)198 }199 } else {200 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrBadEmail.Error(), ErrBadEmail, http.StatusBadRequest)201 }202 default:203 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, shared.ErrNotAllowedMethod.Error(), shared.ErrNotAllowedMethod, http.StatusMethodNotAllowed)204 }205}206// ResetPassword - сбрасывает пароль для заданного пользователя207//208// POST209//210// ожидается заголовок ApiKey с API ключом211// ожидается заголовок Token с токеном для доступа212// ожидается заголовок NewPassword c новым паролем213func ResetPassword(w http.ResponseWriter, req *http.Request) {214 if !AuthBasic(w, req) {215 return216 }217 switch {218 case req.Method == http.MethodPost:219 Token := req.Header.Get("Token")220 Token, err := url.QueryUnescape(Token)221 if shared.HandleBadRequestError(setup.ServerSettings.Lang, w, req, err) {222 return223 }224 // Разбираем и декодируем зашифрованный base64 пароль225 NewPassword := req.Header.Get("NewPassword")226 NewPassword, err = url.QueryUnescape(NewPassword)227 Hash, err := authentication.Argon2GenerateHash(NewPassword, &authentication.HashParams)228 if shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrPasswdHashCalc.Error(), err, http.StatusInternalServerError) {229 return230 }231 // Если токен не протух и существует в базе обновляем пароль пользователя232 err = databases.PostgreSQLGetTokenResetPassword(Token, Hash, setup.ServerSettings.SQL.ConnPool)233 if err != nil {234 if shared.HandleOtherError(setup.ServerSettings.Lang, w, req, err.Error(), err, http.StatusUnauthorized) {235 return236 }237 }238 shared.HandleSuccessMessage(setup.ServerSettings.Lang, w, req, MesPasswdChanged)239 default:240 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, shared.ErrNotAllowedMethod.Error(), shared.ErrNotAllowedMethod, http.StatusMethodNotAllowed)241 }242}243// HandleUsers - обработчик для работы с пользователями, принимает http запросы GET, POST и DELETE244//245// Аутентификация246//247// Куки248// Session - шифрованная сессия249// Email - шифрованный электронный адрес пользователя250//251// или252//253// Заголовки:254// Auth - Токен доступа255//256// и257//258// ApiKey - Постоянный ключ доступа к API *259//260// GET261//262// ожидается заголовок Page с номером страницы263// ожидается заголовок Limit с максимумом элементов на странице264//265// POST266//267// тело запроса должно быть заполнено JSON объектом268// идентичным по структуре UserDB при этом пароль269// пропущен через через encodeURIComponent и btoa270// и записан в заголовке NewPassword271//272// DELETE273//274// ожидается заголовок UserID с UUID пользователя пропущенным через encodeURIComponent и btoa (закодированным base64)275func HandleUsers(w http.ResponseWriter, req *http.Request) {276 role, auth := AuthGeneral(w, req)277 if !auth {278 return279 }280 switch {281 case req.Method == http.MethodGet:282 if setup.ServerSettings.CheckRoleForRead(role, "HandleUsers") {283 PageStr := req.Header.Get("Page")284 LimitStr := req.Header.Get("Limit")285 var usersresp databases.UsersResponse286 if PageStr != "" && LimitStr != "" {287 Page, err := strconv.Atoi(PageStr)288 if shared.HandleInternalServerError(setup.ServerSettings.Lang, w, req, err) {289 return290 }291 Limit, err := strconv.Atoi(LimitStr)292 if shared.HandleInternalServerError(setup.ServerSettings.Lang, w, req, err) {293 return294 }295 usersresp, err = databases.PostgreSQLUsersSelect(Page, Limit, setup.ServerSettings.SQL.ConnPool)296 if err != nil {297 if errors.Is(err, databases.ErrLimitOffsetInvalid) {298 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, err.Error(), err, http.StatusBadRequest)299 return300 }301 if shared.HandleInternalServerError(setup.ServerSettings.Lang, w, req, err) {302 return303 }304 }305 shared.WriteObjectToJSON(setup.ServerSettings.Lang, w, req, usersresp)306 } else {307 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, shared.ErrHeadersNotFilled.Error(), shared.ErrHeadersNotFilled, http.StatusBadRequest)308 return309 }310 } else {311 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, shared.ErrForbidden.Error(), shared.ErrForbidden, http.StatusForbidden)312 }313 case req.Method == http.MethodPost:314 if setup.ServerSettings.CheckRoleForChange(role, "HandleUsers") {315 // Создание и изменение пользователя316 var User databases.User317 err := json.NewDecoder(req.Body).Decode(&User)318 if shared.HandleBadRequestError(setup.ServerSettings.Lang, w, req, err) {319 return320 }321 remai := regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")322 if !remai.MatchString(User.Email) {323 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrBadEmail.Error(), ErrBadEmail, http.StatusBadRequest)324 return325 }326 if len(User.Phone) > 0 {327 repho := regexp.MustCompile(`^((8|\+7)[\- ]?)?(\(?\d{3,4}\)?[\- ]?)?[\d\- ]{5,10}$`)328 if !repho.MatchString(User.Phone) {329 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrBadPhone.Error(), ErrBadPhone, http.StatusBadRequest)330 return331 }332 }333 if !setup.ServerSettings.CheckExistingRole(User.Role) {334 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrBadRole.Error(), ErrBadRole, http.StatusBadRequest)335 return336 }337 // Значение по умолчанию для хеша и пароля338 Hash := ""339 UpdatePassword := false340 NewPassword := req.Header.Get("NewPassword")341 if len(NewPassword) > 0 {342 NewPassword, err = url.QueryUnescape(NewPassword)343 if len(NewPassword) < 6 {344 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrPasswordLength.Error(), ErrPasswordLength, http.StatusBadRequest)345 return346 }347 Hash, err = authentication.Argon2GenerateHash(NewPassword, &authentication.HashParams)348 if shared.HandleInternalServerError(setup.ServerSettings.Lang, w, req, err) {349 return350 }351 UpdatePassword = true352 }353 if len(NewPassword) == 0 && uuid.Nil.String() == User.GUID.String() {354 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrPasswordNewUserNotFilled.Error(), ErrPasswordNewUserNotFilled, http.StatusBadRequest)355 return356 }357 // Получаем обновлённого юзера (если новый с GUID)358 User, err = databases.PostgreSQLUsersInsertUpdate(User, Hash, UpdatePassword, true, setup.ServerSettings.SQL.ConnPool)359 if err != nil {360 if errors.Is(err, databases.ErrEmailIsOccupied) {361 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrEmailRegistered.Error(), err, http.StatusBadRequest)362 return363 }364 }365 if shared.HandleInternalServerError(setup.ServerSettings.Lang, w, req, err) {366 return367 }368 // Пишем в тело ответа369 shared.WriteObjectToJSON(setup.ServerSettings.Lang, w, req, User)370 } else {371 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, shared.ErrForbidden.Error(), shared.ErrForbidden, http.StatusForbidden)372 }373 case req.Method == http.MethodDelete:374 if setup.ServerSettings.CheckRoleForDelete(role, "HandleUsers") {375 // Удаление пользователя376 UserIDtoDelStr := req.Header.Get("UserID")377 if len(UserIDtoDelStr) > 0 {378 UserIDtoDelStr, err := url.QueryUnescape(UserIDtoDelStr)379 UserID, err := uuid.FromString(UserIDtoDelStr)380 if shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrIncorrectUserID.Error(), err, http.StatusBadRequest) {381 return382 }383 err = databases.PostgreSQLUsersDelete(UserID, setup.ServerSettings.SQL.ConnPool, setup.ServerSettings.Lang)384 if err != nil {385 if errors.Is(err, databases.ErrUserNotFound) {386 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrUnableToDeleteAbsent.Error(), err, http.StatusBadRequest)387 return388 }389 }390 if shared.HandleInternalServerError(setup.ServerSettings.Lang, w, req, err) {391 return392 }393 shared.HandleSuccessMessage(setup.ServerSettings.Lang, w, req, MesUserDeleted)394 } else {395 shared.HandleBadRequestError(setup.ServerSettings.Lang, w, req, shared.ErrHeadersNotFilled)396 }397 } else {398 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, shared.ErrForbidden.Error(), shared.ErrForbidden, http.StatusForbidden)399 }400 default:401 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, shared.ErrNotAllowedMethod.Error(), shared.ErrNotAllowedMethod, http.StatusMethodNotAllowed)402 }403}404// HandleSessions - обработчик для работы с сессиями, принимает http запросы GET, POST и DELETE405//406// Аутентификация407//408// Куки409// Session - шифрованная сессия410// Email - шифрованный электронный адрес пользователя411//412// или413//414// Заголовки:415// Auth - Токен доступа416//417// и418//419// ApiKey - Постоянный ключ доступа к API *420//421// GET422//423// ожидается заголовок Page с номером страницы424// ожидается заголовок Limit с максимумом элементов на странице425//426// DELETE427//428// ожидается заголовок Email для удаления сессий из списка сессий по электронной почте429//430// или431//432// ожидается заголовок Token для удаления сессий из списка сессий по токену433func HandleSessions(w http.ResponseWriter, req *http.Request) {434 role, auth := AuthGeneral(w, req)435 if !auth {436 return437 }438 var err error439 switch {440 case req.Method == http.MethodGet:441 if setup.ServerSettings.CheckRoleForRead(role, "HandleSessions") {442 PageStr := req.Header.Get("Page")443 LimitStr := req.Header.Get("Limit")444 var sessionsresp databases.SessionsResponse445 if PageStr != "" && LimitStr != "" {446 Page, err := strconv.Atoi(PageStr)447 if shared.HandleInternalServerError(setup.ServerSettings.Lang, w, req, err) {448 return449 }450 Limit, err := strconv.Atoi(LimitStr)451 if shared.HandleInternalServerError(setup.ServerSettings.Lang, w, req, err) {452 return453 }454 sessionsresp, err = databases.PostgreSQLSessionsSelect(Page, Limit, setup.ServerSettings.SQL.ConnPool)455 if err != nil {456 if errors.Is(err, databases.ErrLimitOffsetInvalid) {457 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, err.Error(), err, http.StatusBadRequest)458 return459 }460 if shared.HandleInternalServerError(setup.ServerSettings.Lang, w, req, err) {461 return462 }463 }464 shared.WriteObjectToJSON(setup.ServerSettings.Lang, w, req, sessionsresp)465 } else {466 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, shared.ErrHeadersNotFilled.Error(), shared.ErrHeadersNotFilled, http.StatusBadRequest)467 return468 }469 } else {470 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, shared.ErrForbidden.Error(), shared.ErrForbidden, http.StatusForbidden)471 }472 case req.Method == http.MethodDelete:473 if setup.ServerSettings.CheckRoleForDelete(role, "HandleSessions") {474 // Удаление сессии по электронной почте475 Email := req.Header.Get("Email")476 Token := req.Header.Get("Token")477 if len(Email) > 0 || len(Token) > 0 {478 if len(Email) > 0 {479 err = DeleteSessionByEmail(Email)480 if err != nil {481 if errors.Is(err, ErrSessionNotFoundByEmail) {482 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrSessionNotFoundByEmail.Error(), err, http.StatusBadRequest)483 return484 }485 }486 shared.HandleSuccessMessage(setup.ServerSettings.Lang, w, req, MesSessionsDeleted)487 }488 if len(Token) > 0 {489 err = DeleteSessionByToken(Token)490 if err != nil {491 if errors.Is(err, ErrSessionNotFoundByToken) {492 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrSessionNotFoundByToken.Error(), err, http.StatusBadRequest)493 return494 }495 }496 shared.HandleSuccessMessage(setup.ServerSettings.Lang, w, req, MesSessionDeleted)497 }498 } else {499 shared.HandleBadRequestError(setup.ServerSettings.Lang, w, req, shared.ErrHeadersNotFilled)500 }501 } else {502 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, shared.ErrForbidden.Error(), shared.ErrForbidden, http.StatusForbidden)503 }504 default:505 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, shared.ErrNotAllowedMethod.Error(), shared.ErrNotAllowedMethod, http.StatusMethodNotAllowed)506 }507}508// CurrentUser - обработчик для получения и сохранения данных текущего пользователя509//510// Аутентификация511//512// Куки513// Session - шифрованная сессия514// Email - шифрованный электронный адрес пользователя515//516// или517//518// Заголовки:519// Auth - Токен доступа520//521// и522//523// ApiKey - Постоянный ключ доступа к API *524//525// GET526//527// Ничего не требуется528//529// POST530//531// тело запроса должно быть заполнено JSON объектом532// идентичным по структуре UserDB при этом пароль533// пропущен через через encodeURIComponent и btoa534// и записан в заголовке NewPassword535func CurrentUser(w http.ResponseWriter, req *http.Request) {536 role, auth := AuthGeneral(w, req)537 if !auth {538 return539 }540 var err error541 switch {542 case req.Method == http.MethodGet:543 if setup.ServerSettings.CheckRoleForRead(role, "CurrentUser") {544 // Получаем данные текущего пользователя545 Email := GetCurrentUserEmail(w, req)546 FoundUser, err := databases.PostgreSQLGetUserByEmail(Email, setup.ServerSettings.SQL.ConnPool)547 if err != nil {548 if errors.Is(databases.ErrNoUserWithEmail, err) {549 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, err.Error(), err, http.StatusBadRequest)550 return551 }552 }553 if shared.HandleInternalServerError(setup.ServerSettings.Lang, w, req, err) {554 return555 }556 shared.WriteObjectToJSON(setup.ServerSettings.Lang, w, req, FoundUser)557 } else {558 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, shared.ErrForbidden.Error(), shared.ErrForbidden, http.StatusForbidden)559 }560 case req.Method == http.MethodPost:561 if setup.ServerSettings.CheckRoleForChange(role, "CurrentUser") {562 // Создание и изменение пользователя563 var User databases.User564 err = json.NewDecoder(req.Body).Decode(&User)565 if shared.HandleBadRequestError(setup.ServerSettings.Lang, w, req, err) {566 return567 }568 // Получаем данные текущего пользователя569 User.Email = GetCurrentUserEmail(w, req)570 FoundUser, err := databases.PostgreSQLGetUserByEmail(User.Email, setup.ServerSettings.SQL.ConnPool)571 if err != nil {572 if errors.Is(databases.ErrNoUserWithEmail, err) {573 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, err.Error(), err, http.StatusBadRequest)574 return575 }576 }577 if shared.HandleInternalServerError(setup.ServerSettings.Lang, w, req, err) {578 return579 }580 User.GUID = FoundUser.GUID581 User.IsAdmin = FoundUser.IsAdmin582 User.Role = FoundUser.Role583 User.Confirmed = FoundUser.Confirmed584 User.SecondFactor = FoundUser.SecondFactor585 remai := regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")586 if !remai.MatchString(User.Email) {587 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrBadEmail.Error(), ErrBadEmail, http.StatusBadRequest)588 return589 }590 if len(User.Phone) > 0 {591 repho := regexp.MustCompile(`^((8|\+7)[\- ]?)?(\(?\d{3,4}\)?[\- ]?)?[\d\- ]{5,10}$`)592 if !repho.MatchString(User.Phone) {593 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrBadPhone.Error(), ErrBadPhone, http.StatusBadRequest)594 return595 }596 }597 if !setup.ServerSettings.CheckExistingRole(User.Role) {598 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrBadRole.Error(), ErrBadRole, http.StatusBadRequest)599 return600 }601 // Значение по умолчанию для хеша и пароля602 Hash := ""603 UpdatePassword := false604 NewPassword := req.Header.Get("NewPassword")605 if len(NewPassword) > 0 {606 NewPassword, err = url.QueryUnescape(NewPassword)607 if len(NewPassword) < 6 {608 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrPasswordLength.Error(), ErrPasswordLength, http.StatusBadRequest)609 return610 }611 Hash, err = authentication.Argon2GenerateHash(NewPassword, &authentication.HashParams)612 if shared.HandleInternalServerError(setup.ServerSettings.Lang, w, req, err) {613 return614 }615 UpdatePassword = true616 }617 if len(NewPassword) == 0 && uuid.Nil.String() == User.GUID.String() {618 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrPasswordNewUserNotFilled.Error(), ErrPasswordNewUserNotFilled, http.StatusBadRequest)619 return620 }621 // Получаем обновлённого юзера622 User, err = databases.PostgreSQLCurrentUserUpdate(User, Hash, UpdatePassword, setup.ServerSettings.SQL.ConnPool)623 if err != nil {624 if errors.Is(err, databases.ErrEmailIsOccupied) {625 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, ErrEmailRegistered.Error(), err, http.StatusInternalServerError)626 return627 }628 }629 if shared.HandleInternalServerError(setup.ServerSettings.Lang, w, req, err) {630 return631 }632 // Пишем в тело ответа633 shared.WriteObjectToJSON(setup.ServerSettings.Lang, w, req, User)634 } else {635 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, shared.ErrForbidden.Error(), shared.ErrForbidden, http.StatusForbidden)636 }637 default:638 shared.HandleOtherError(setup.ServerSettings.Lang, w, req, shared.ErrNotAllowedMethod.Error(), shared.ErrNotAllowedMethod, http.StatusMethodNotAllowed)639 }640}...

Full Screen

Full Screen

setup.go

Source:setup.go Github

copy

Full Screen

...32 if err != nil {33 respond(500, "Failed to fetch default values", gc)34 return35 }36 gc.HTML(200, "setup.html", gin.H{37 "cssVersion": cssVersion,38 "lang": app.storage.lang.Setup[lang],39 "emailLang": app.storage.lang.Email[emailLang],40 "language": app.storage.lang.Setup[lang].JSON,41 "messages": string(msg),42 })43}44type testReq struct {45 ServerType string `json:"type"`46 Server string `json:"server"`47 Username string `json:"username"`48 Password string `json:"password"`49}50func (app *appContext) TestJF(gc *gin.Context) {51 var req testReq52 gc.BindJSON(&req)53 if !(strings.HasPrefix(req.Server, "http://") || strings.HasPrefix(req.Server, "https://")) {54 req.Server = "http://" + req.Server55 }56 serverType := mediabrowser.JellyfinServer57 if req.ServerType == "emby" {58 serverType = mediabrowser.EmbyServer59 }60 tempjf, _ := mediabrowser.NewServer(serverType, req.Server, "jfa-go-setup", app.version, "auth", "auth", mediabrowser.NewNamedTimeoutHandler("authJF", req.Server, true), 30)61 user, status, err := tempjf.Authenticate(req.Username, req.Password)62 if !(status == 200 || status == 204) || err != nil {63 msg := ""64 switch status {65 case 0:66 msg = "errorConnectionRefused"67 status = 50068 case 401:69 msg = "errorInvalidUserPass"70 case 403:71 msg = "errorUserDisabled"72 case 404:73 msg = "error404"74 }75 app.info.Printf("Auth failed with code %d (%s)", status, err)76 if msg != "" {77 respond(status, msg, gc)78 } else {79 respondBool(status, false, gc)80 }81 return82 }83 if !user.Policy.IsAdministrator {84 respond(403, "errorNotAdmin", gc)85 return86 }87 gc.JSON(200, map[string]bool{"success": true})88}89// The first filesystem passed should be the localFS, to ensure the local lang files are loaded first.90func (st *Storage) loadLangSetup(filesystems ...fs.FS) error {91 st.lang.Setup = map[string]setupLang{}92 var english setupLang93 loadedLangs := make([]map[string]bool, len(filesystems))94 var load loadLangFunc95 load = func(fsIndex int, fname string) error {96 filesystem := filesystems[fsIndex]97 index := strings.TrimSuffix(fname, filepath.Ext(fname))98 lang := setupLang{}99 f, err := fs.ReadFile(filesystem, FSJoin(st.lang.SetupPath, fname))100 if err != nil {101 return err102 }103 err = json.Unmarshal(f, &lang)104 if err != nil {105 return err106 }107 st.lang.Common.patchCommon(&lang.Strings, index)108 if fname != "en-us.json" {109 if lang.Meta.Fallback != "" {110 fallback, ok := st.lang.Setup[lang.Meta.Fallback]111 err = nil112 if !ok {113 err = load(fsIndex, lang.Meta.Fallback+".json")114 fallback = st.lang.Setup[lang.Meta.Fallback]115 }116 if err == nil {117 loadedLangs[fsIndex][lang.Meta.Fallback+".json"] = true118 patchLang(&lang.Strings, &fallback.Strings, &english.Strings)119 patchLang(&lang.StartPage, &fallback.StartPage, &english.StartPage)120 patchLang(&lang.Updates, &fallback.Updates, &english.Updates)121 patchLang(&lang.EndPage, &fallback.EndPage, &english.EndPage)122 patchLang(&lang.Language, &fallback.Language, &english.Language)123 patchLang(&lang.Login, &fallback.Login, &english.Login)124 patchLang(&lang.JellyfinEmby, &fallback.JellyfinEmby, &english.JellyfinEmby)125 patchLang(&lang.Email, &fallback.Email, &english.Email)126 patchLang(&lang.Messages, &fallback.Messages, &english.Messages)127 patchLang(&lang.Notifications, &fallback.Notifications, &english.Notifications)128 patchLang(&lang.PasswordResets, &fallback.PasswordResets, &english.PasswordResets)129 patchLang(&lang.InviteEmails, &fallback.InviteEmails, &english.InviteEmails)130 patchLang(&lang.PasswordValidation, &fallback.PasswordValidation, &english.PasswordValidation)131 patchLang(&lang.HelpMessages, &fallback.HelpMessages, &english.HelpMessages)132 }133 }134 if (lang.Meta.Fallback != "" && err != nil) || lang.Meta.Fallback == "" {135 patchLang(&lang.Strings, &english.Strings)136 patchLang(&lang.StartPage, &english.StartPage)137 patchLang(&lang.Updates, &english.Updates)138 patchLang(&lang.EndPage, &english.EndPage)139 patchLang(&lang.Language, &english.Language)140 patchLang(&lang.Login, &english.Login)141 patchLang(&lang.JellyfinEmby, &english.JellyfinEmby)142 patchLang(&lang.Email, &english.Email)143 patchLang(&lang.Messages, &english.Messages)144 patchLang(&lang.Notifications, &english.Notifications)145 patchLang(&lang.PasswordResets, &english.PasswordResets)146 patchLang(&lang.InviteEmails, &english.InviteEmails)147 patchLang(&lang.PasswordValidation, &english.PasswordValidation)148 patchLang(&lang.HelpMessages, &english.HelpMessages)149 }150 }151 stringSettings, err := json.Marshal(lang)152 if err != nil {153 return err154 }155 lang.JSON = string(stringSettings)156 st.lang.Setup[index] = lang157 return nil158 }159 engFound := false160 var err error161 for i := range filesystems {162 loadedLangs[i] = map[string]bool{}163 err = load(i, "en-us.json")164 if err == nil {165 engFound = true166 }167 loadedLangs[i]["en-us.json"] = true168 }169 if !engFound {170 return err171 }172 english = st.lang.Setup["en-us"]173 setupLoaded := false174 for i := range filesystems {175 files, err := fs.ReadDir(filesystems[i], st.lang.SetupPath)176 if err != nil {177 return err178 }179 for _, f := range files {180 if !loadedLangs[i][f.Name()] {181 err = load(i, f.Name())182 if err == nil {183 setupLoaded = true184 loadedLangs[i][f.Name()] = true185 }186 }187 }188 }189 if !setupLoaded {190 return err191 }192 return nil193}...

Full Screen

Full Screen

accesslog_test.go

Source:accesslog_test.go Github

copy

Full Screen

...7 "github.com/stretchr/testify/assert"8 "github.com/stretchr/testify/require"9)10func TestNoAccessLock(t *testing.T) {11 access := setupAccessManager()12 lock, err := access.currentAccessLock("lang", "pkg")13 assert.Nil(t, lock)14 assert.NoError(t, err)15}16func TestAcquireAccessLock(t *testing.T) {17 access := setupAccessManager()18 lock, err := access.acquireAccessLock("lang", "pkg", "test@kite.com")19 require.NotNil(t, lock)20 require.NoError(t, err)21 assert.Equal(t, "test@kite.com", lock.UserEmail)22}23func TestLockedOut(t *testing.T) {24 access := setupAccessManager()25 lockFoo, err := access.acquireAccessLock("lang", "pkg", "foo@kite.com")26 require.NotNil(t, lockFoo)27 require.NoError(t, err)28 lockBar, err := access.acquireAccessLock("lang", "pkg", "bar@kite.com")29 require.NotNil(t, lockBar)30 require.NoError(t, err)31 assert.Equal(t, lockFoo.UserEmail, lockBar.UserEmail)32}33func TestTakeOldAccessLock(t *testing.T) {34 access := setupAccessManager()35 access.timeout = 136 lockFoo, err := access.acquireAccessLock("lang", "pkg", "foo@kite.com")37 require.NotNil(t, lockFoo)38 require.NoError(t, err)39 time.Sleep(1 * time.Second)40 lockBar, err := access.acquireAccessLock("lang", "pkg", "bar@kite.com")41 require.NotNil(t, lockBar)42 require.NoError(t, err)43 assert.Equal(t, "bar@kite.com", lockBar.UserEmail)44}45func TestRenewAccessLock(t *testing.T) {46 access := setupAccessManager()47 access.timeout = 348 lockFoo, err := access.acquireAccessLock("lang", "pkg", "foo@kite.com")49 require.NotNil(t, lockFoo)50 require.NoError(t, err)51 time.Sleep(1 * time.Second)52 lockBar, err := access.acquireAccessLock("lang", "pkg", "bar@kite.com")53 require.NotNil(t, lockBar)54 require.NoError(t, err)55 assert.Equal(t, lockFoo.UserEmail, lockBar.UserEmail)56 lockFoo, err = access.acquireAccessLock("lang", "pkg", "foo@kite.com")57 require.NotNil(t, lockFoo)58 require.NoError(t, err)59 time.Sleep(2 * time.Second)60 lockBar, err = access.acquireAccessLock("lang", "pkg", "bar@kite.com")61 require.NotNil(t, lockBar)62 require.NoError(t, err)63 assert.Equal(t, lockFoo.UserEmail, lockBar.UserEmail)64}65func setupAccessManager() *accessManager {66 db := curation.GormDB("sqlite3", ":memory:")67 access := newAccessManager(db)68 access.Migrate()69 return access70}...

Full Screen

Full Screen

setup

Using AI Code Generation

copy

Full Screen

1lang.setup()2lang.setup()3lang.setup()4lang.setup()5lang.setup()6lang.setup()7lang.setup()8lang.setup()9lang.setup()10lang.setup()11lang.setup()12lang.setup()13lang.setup()14lang.setup()15lang.setup()16lang.setup()17lang.setup()18lang.setup()19lang.setup()20lang.setup()21lang.setup()22lang.setup()23lang.setup()24lang.setup()25lang.setup()

Full Screen

Full Screen

setup

Using AI Code Generation

copy

Full Screen

1lang.setup();2lang.setup();3lang.setup();4lang.setup();5lang.setup();6lang.setup();7lang.setup();8lang.setup();9lang.setup();10lang.setup();11lang.setup();12lang.setup();13lang.setup();14lang.setup();15lang.setup();16lang.setup();17lang.setup();18lang.setup();19lang.setup();20lang.setup();21lang.setup();22lang.setup();23lang.setup();24lang.setup();25lang.setup();

Full Screen

Full Screen

setup

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(stringutil.Reverse("!oG ,olleH"))4}5import (6func main() {7 fmt.Println(stringutil.Reverse("!oG ,olleH"))8}9import (10func main() {11 fmt.Println(stringutil.Reverse("!oG ,olleH"))12}13import (14func main() {15 fmt.Println(stringutil.Reverse("!oG ,olleH"))16}17import (18func main() {19 fmt.Println(stringutil.Reverse("!oG ,olleH"))20}21import (22func main() {23 fmt.Println(stringutil.Reverse("!oG ,olleH"))24}25import (26func main() {27 fmt.Println(stringutil.Reverse("!oG ,olleH"))28}29import (30func main() {31 fmt.Println(stringutil.Reverse("!oG ,olleH"))32}33import (34func main() {35 fmt.Println(stringutil.Reverse("!oG ,olleH"))36}37import (38func main() {39 fmt.Println(stringutil.Reverse("!oG

Full Screen

Full Screen

setup

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 lang.setup("Go")4 fmt.Println(lang.name)5}6type Language struct {7}8func (l *Language) setup(name string) {9}10import "testing"11func TestSetup(t *testing.T) {12 lang.setup("Go")13 if lang.name != "Go" {14 t.Errorf("Expected Go, got %s", lang.name)15 }16}

Full Screen

Full Screen

setup

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 lang := new(Language)4 lang.setup()5 fmt.Println("Name:", lang.Name)6 fmt.Println("Version:", lang.Version)7}8import "fmt"9func main() {10 lang := new(Language)11 fmt.Println("Name:", lang.Name)12 fmt.Println("Version:", lang.Version)13}14type Language struct {15}16func (l *Language) setup() {17}18import "fmt"19func main() {20 lang := NewLanguage()21 fmt.Println("Name:", lang.Name)22 fmt.Println("Version:", lang.Version)23}24type Language struct {25}26func NewLanguage() *Language {27 return &Language{28 }29}30import "fmt"31func main() {32 fmt.Println("Name:", lang.Name)33 fmt.Println("Version:", lang.Version)34}35type Language struct {36}37var lang = &Language{38}

Full Screen

Full Screen

setup

Using AI Code Generation

copy

Full Screen

1 func main() {2 lang := lang.NewLang()3 lang.Setup("en")4 fmt.Println(lang.Get("hello"))5 }6 func main() {7 lang := lang.NewLang()8 lang.Setup("fr")9 fmt.Println(lang.Get("hello"))10 }11 func main() {12 lang := lang.NewLang()13 lang.Setup("es")14 fmt.Println(lang.Get("hello"))15 }16 func main() {17 lang.Setup("en")18 fmt.Println(lang.Get("hello"))19 }20 func main() {21 lang.Setup("en")22 fmt.Println(lang.Get("hello"))23 }24 func main() {25 lang.Setup("en")26 fmt.Println(lang.Get("hello"))27 }28 func main() {29 lang.Setup("en")30 fmt.Println(lang.Get("hello"))31 }

Full Screen

Full Screen

setup

Using AI Code Generation

copy

Full Screen

1var lang = new Lang();2lang.setup();3var Lang = function() {4 this.setup = function() {5 };6};7var Lang = function() {8 this.setup = function() {9 };10};11var Lang = function() {12 this.setup = function() {13 };14};15var Lang = function() {16 this.setup = function() {17 };18};19var Lang = function() {20 this.setup = function() {21 };22};23var Lang = function() {24 this.setup = function() {25 };26};27var Lang = function() {28 this.setup = function() {29 };30};31var Lang = function() {32 this.setup = function() {33 };34};35var Lang = function() {36 this.setup = function() {37 };38};39var Lang = function() {40 this.setup = function() {41 };42};43var Lang = function() {44 this.setup = function() {45 };46};47var Lang = function() {48 this.setup = function() {49 };50};51var Lang = function() {52 this.setup = function() {

Full Screen

Full Screen

setup

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 l.setup()4 l.sayHello()5}6import "fmt"7func main() {8 l := newLanguage("Go")9 l.sayHello()10}11import "fmt"12func main() {13 l := newLanguage("Go")14 l.sayHello()15}16import "fmt"17func main() {18 l := newLanguage("Go")19 l.sayHello()20}21import "fmt"22func main() {23 l := newLanguage("Go")24 l.sayHello()25}26import "fmt"27func main() {28 l := newLanguage("Go")29 l.sayHello()30}31import "fmt"32func main() {33 l := newLanguage("Go")34 l.sayHello()35}36import "fmt"37func main() {38 l := newLanguage("Go")39 l.sayHello()40}41import "fmt"42func main() {43 l := newLanguage("Go")44 l.sayHello()45}46import "fmt"47func main() {48 l := newLanguage("Go")

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Gauge automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful