github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/auth/auth.go (about) 1 /* 2 * Copyright (C) 2019 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package auth 19 20 import ( 21 "github.com/mysteriumnetwork/node/config" 22 "github.com/rs/zerolog/log" 23 ) 24 25 // Authenticator wraps CredentialsManager to provide 26 // an easy way of authentication for builtin UI. 27 type Authenticator struct { 28 manager *CredentialsManager 29 } 30 31 // NewAuthenticator creates an authenticator. 32 func NewAuthenticator() *Authenticator { 33 pswDir := config.GetString(config.FlagDataDir) 34 return &Authenticator{ 35 manager: NewCredentialsManager(pswDir), 36 } 37 } 38 39 // CheckCredentials checks if provided username and password combo is valid 40 // comparing it to stored credentials. 41 func (a *Authenticator) CheckCredentials(username, password string) error { 42 return a.manager.Validate(username, password) 43 } 44 45 // ChangePassword changes user password. 46 func (a *Authenticator) ChangePassword(username, oldPassword, newPassword string) error { 47 err := a.manager.Validate(username, oldPassword) 48 if err != nil { 49 log.Info().Err(err).Msg("Bad credentials for changing password") 50 return ErrUnauthorized 51 } 52 err = a.manager.SetPassword(newPassword) 53 if err != nil { 54 log.Info().Err(err).Msg("Error changing password") 55 return err 56 } 57 log.Info().Msgf("%q user password changed successfully", username) 58 return nil 59 }