Skip to content

Commit

Permalink
Fix naming convention
Browse files Browse the repository at this point in the history
Signed-off-by: William <[email protected]>
  • Loading branch information
kwesidev committed Apr 22, 2024
1 parent 51aa002 commit 81ebb33
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 51 deletions.
6 changes: 3 additions & 3 deletions internal/apiserver/api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ type APIServer struct {
port string
serverName string
db *sql.DB
userService services.UserServiceInterface
authService services.AuthServiceInterface
emailService *services.EmailService
userService services.UserService
authService services.AuthService
emailService services.EmailService
}

// NewAPIServer initializes the api server
Expand Down
10 changes: 5 additions & 5 deletions internal/controllers/auth_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ import (
type AuthController struct {
// Registered Services
db *sql.DB
userService services.UserServiceInterface
authService services.AuthServiceInterface
userService services.UserService
authService services.AuthService
validate *validator.Validate
}

// Creates a new Auth Controller for passing requests
func NewAuthController(db *sql.DB, authServiceInterface services.AuthServiceInterface, userServiceInterface services.UserServiceInterface) *AuthController {
func NewAuthController(db *sql.DB, authService services.AuthService, userService services.UserService) *AuthController {
return &AuthController{
db: db,
userService: userServiceInterface,
authService: authServiceInterface,
userService: userService,
authService: authService,
validate: validator.New(),
}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/controllers/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import (
type UserController struct {
// Registered Services
db *sql.DB
userService services.UserServiceInterface
authService services.AuthServiceInterface
userService services.UserService
authService services.AuthService
validate *validator.Validate
}

// Creates a new User Controller for passing requests
func NewUserController(db *sql.DB, userService services.UserServiceInterface, authService services.AuthServiceInterface) *UserController {
func NewUserController(db *sql.DB, userService services.UserService, authService services.AuthService) *UserController {
return &UserController{
db: db,
userService: userService,
Expand Down
38 changes: 19 additions & 19 deletions internal/services/auth_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"golang.org/x/crypto/bcrypt"
)

type AuthServiceInterface interface {
type AuthService interface {
LoginByUsernamePassword(username, password, ipAddress, userAgent string) (*models.AuthenticationResponse, error)
PasswordlessLogin(username, sendMethod, ipAddress, userAgent string) (*models.PasswordLessAuthResponse, error)
CompletePasswordLessLogin(code, requestId string) (*models.AuthenticationResponse, error)
Expand All @@ -24,16 +24,16 @@ type AuthServiceInterface interface {
VerifyTOTP(userId int, passCode, ipAddress, userAgent string) (*models.AuthenticationResponse, error)
ValidateTwoFactor(code, requestId string, ipAddress, userAgent string) (*models.AuthenticationResponse, error)
}
type AuthService struct {
type authService struct {
db *sql.DB
userService UserServiceInterface
emailService EmailServiceInterface
userService UserService
emailService EmailService
tokenTime time.Duration
}

func NewAuthService(db *sql.DB, userService UserServiceInterface, emailService EmailServiceInterface) *AuthService {
func NewAuthService(db *sql.DB, userService UserService, emailService EmailService) AuthService {
tokenTime, _ := time.ParseDuration(os.Getenv("TOKEN_EXPIRY_TIME"))
return &AuthService{
return &authService{
db: db,
userService: userService,
emailService: emailService,
Expand All @@ -43,7 +43,7 @@ func NewAuthService(db *sql.DB, userService UserServiceInterface, emailService E
}

// Login function to authenticate user by username and password
func (authSrv *AuthService) LoginByUsernamePassword(username, password, ipAddress, userAgent string) (*models.AuthenticationResponse, error) {
func (authSrv *authService) LoginByUsernamePassword(username, password, ipAddress, userAgent string) (*models.AuthenticationResponse, error) {
var (
userId int
passwordHash string
Expand All @@ -65,7 +65,7 @@ func (authSrv *AuthService) LoginByUsernamePassword(username, password, ipAddres
}
return authSrv.generateAuthResponse(*userDetails, ipAddress, userAgent)
}
func (authSrv *AuthService) generateAuthResponse(userDetails models.User, ipAddress, userAgent string) (*models.AuthenticationResponse, error) {
func (authSrv *authService) generateAuthResponse(userDetails models.User, ipAddress, userAgent string) (*models.AuthenticationResponse, error) {
// Check if two authentication is required
if userDetails.TwoFactorEnabled {
if userDetails.TwoFactorMethod != "TOTP" {
Expand All @@ -91,7 +91,7 @@ func (authSrv *AuthService) generateAuthResponse(userDetails models.User, ipAddr
}

// Func loginByUsername this will send an otp to the user which then be verified
func (authSrv *AuthService) PasswordlessLogin(username, sendMethod, ipAddress, userAgent string) (*models.PasswordLessAuthResponse, error) {
func (authSrv *authService) PasswordlessLogin(username, sendMethod, ipAddress, userAgent string) (*models.PasswordLessAuthResponse, error) {
userDetails := authSrv.userService.GetByUsername(username)
if userDetails == nil {
return nil, ErrInvalidUsername
Expand Down Expand Up @@ -127,7 +127,7 @@ func (authSrv *AuthService) PasswordlessLogin(username, sendMethod, ipAddress, u
}

// Func completePasswordLessLogin
func (authSrv *AuthService) CompletePasswordLessLogin(code, requestId string) (*models.AuthenticationResponse, error) {
func (authSrv *authService) CompletePasswordLessLogin(code, requestId string) (*models.AuthenticationResponse, error) {
var (
userId int
userAgent, ipAddress string
Expand Down Expand Up @@ -157,7 +157,7 @@ func (authSrv *AuthService) CompletePasswordLessLogin(code, requestId string) (*
}

// Refresh Token generates a new refresh token that will be used to get a new access token and a refresh token
func (authSrv *AuthService) GenerateRefreshToken(oldRefreshToken, ipAddress, userAgent string) (*models.AuthenticationResponse, error) {
func (authSrv *authService) GenerateRefreshToken(oldRefreshToken, ipAddress, userAgent string) (*models.AuthenticationResponse, error) {
var (
userId int
)
Expand Down Expand Up @@ -214,7 +214,7 @@ func (authSrv *AuthService) GenerateRefreshToken(oldRefreshToken, ipAddress, use
return authResult, nil
}

func (authSrv *AuthService) ResetPasswordRequest(username string) (bool, error) {
func (authSrv *authService) ResetPasswordRequest(username string) (bool, error) {
//check if the username exists and then send vertification code
var (
userId int
Expand Down Expand Up @@ -256,7 +256,7 @@ func (authSrv *AuthService) ResetPasswordRequest(username string) (bool, error)
}

// VerifyAndSetNewPassword functions to verify and reset password
func (authSrv *AuthService) VerifyAndSetNewPassword(code string, password string) (bool, error) {
func (authSrv *authService) VerifyAndSetNewPassword(code string, password string) (bool, error) {
if !utilities.StrongPasswordCheck(password) {
return false, ErrStrongPassword
}
Expand Down Expand Up @@ -297,7 +297,7 @@ func (authSrv *AuthService) VerifyAndSetNewPassword(code string, password string
return true, nil
}

func (authSrv *AuthService) twoFactorRequest(userDetails models.User, ipAddress string, userAgent string) (*models.AuthenticationResponse, error) {
func (authSrv *authService) twoFactorRequest(userDetails models.User, ipAddress string, userAgent string) (*models.AuthenticationResponse, error) {
tx, err := authSrv.db.Begin()
defer tx.Rollback()
if err != nil {
Expand Down Expand Up @@ -333,7 +333,7 @@ func (authSrv *AuthService) twoFactorRequest(userDetails models.User, ipAddress
return authResult, nil
}

func (authSrv *AuthService) generateTokenDetails(userDetails models.User, ipAddress string, userAgent string) (*models.AuthenticationResponse, error) {
func (authSrv *authService) generateTokenDetails(userDetails models.User, ipAddress string, userAgent string) (*models.AuthenticationResponse, error) {
authResult := &models.AuthenticationResponse{}
tokenExpiry := time.Duration(authSrv.tokenTime)
// Generates JWT Token and Refresh token that expires after xminutes
Expand Down Expand Up @@ -364,7 +364,7 @@ func (authSrv *AuthService) generateTokenDetails(userDetails models.User, ipAddr
}

// Validate the two factor authentication request and complete the authentication request
func (authSrv *AuthService) ValidateTwoFactor(code, requestId string, ipAddress, userAgent string) (*models.AuthenticationResponse, error) {
func (authSrv *authService) ValidateTwoFactor(code, requestId string, ipAddress, userAgent string) (*models.AuthenticationResponse, error) {
var userId int
row := authSrv.db.QueryRow("SELECT user_id FROM two_factor_requests WHERE code = $1 AND request_id = $2 AND expiry_time > NOW() ", code, requestId)
row.Scan(&userId)
Expand All @@ -382,7 +382,7 @@ func (authSrv *AuthService) ValidateTwoFactor(code, requestId string, ipAddress,
}

// Delete expired tokens
func (authSrv *AuthService) DeleteExpiredTokens(days int) error {
func (authSrv *authService) DeleteExpiredTokens(days int) error {
// Deletes User Refresh tokens
result, err := authSrv.db.Exec("DELETE FROM user_refresh_tokens WHERE (DATE_PART('day', AGE(NOW()::date ,expiry_time::date))) >= $1", days)
if err != nil {
Expand All @@ -405,13 +405,13 @@ func (authSrv *AuthService) DeleteExpiredTokens(days int) error {
}

// Verify the passcode
func (authSrv *AuthService) VerifyPassCode(userId int, passCode string) bool {
func (authSrv *authService) VerifyPassCode(userId int, passCode string) bool {
userDetails := authSrv.userService.Get(userId)
return totp.Validate(passCode, userDetails.TOTPSecret)
}

// Validates the TOTP before the user finally logs in
func (authSrv *AuthService) VerifyTOTP(userId int, passCode, ipAddress, userAgent string) (*models.AuthenticationResponse, error) {
func (authSrv *authService) VerifyTOTP(userId int, passCode, ipAddress, userAgent string) (*models.AuthenticationResponse, error) {
userDetails := authSrv.userService.Get(userId)
if !authSrv.VerifyPassCode(userId, passCode) {
return nil, ErrPassCode
Expand Down
16 changes: 8 additions & 8 deletions internal/services/email_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
"gopkg.in/gomail.v2"
)

type EmailServiceInterface interface {
type EmailService interface {
SendTwoFactorRequest(randomCodes string, userDetails models.User) error
SendEmailLoginRequest(randomCodes string, userDetails models.User) error
SendPasswordResetRequest(randomCodes string, userDetails models.User) error
}
type EmailService struct {
type emailService struct {
smtpHost string
smtpUsername string
smtpPassword string
Expand All @@ -26,8 +26,8 @@ type EmailService struct {
secure bool
}

func NewEmailService(secure bool) *EmailService {
return &EmailService{
func NewEmailService(secure bool) EmailService {
return &emailService{
smtpHost: os.Getenv("SMTP_HOST"),
smtpUsername: os.Getenv("SMTP_USERNAME"),
smtpPort: os.Getenv("SMTP_PORT"),
Expand All @@ -38,7 +38,7 @@ func NewEmailService(secure bool) *EmailService {
}

// SendEmail funnction sends email directly to an external server
func (emSrv *EmailService) sendEmail(to []string, subject, message string) error {
func (emSrv *emailService) sendEmail(to []string, subject, message string) error {
portNumber, _ := strconv.Atoi(emSrv.smtpPort)
d := gomail.NewDialer(emSrv.smtpHost, portNumber, emSrv.smtpUsername, emSrv.smtpPassword)
d.TLSConfig = &tls.Config{InsecureSkipVerify: emSrv.secure}
Expand All @@ -56,7 +56,7 @@ func (emSrv *EmailService) sendEmail(to []string, subject, message string) error
}

// SendTwoFactorRequest sends two factor mail
func (emSrv *EmailService) SendTwoFactorRequest(randomCodes string, userDetails models.User) error {
func (emSrv *emailService) SendTwoFactorRequest(randomCodes string, userDetails models.User) error {
var twoFactorRequestTemplateBuffer bytes.Buffer
// Get email template from directory and assign random code to it
emailTemplateFile, err := template.ParseFiles("static/email_templates/TwoFactorLogin.html")
Expand All @@ -80,7 +80,7 @@ func (emSrv *EmailService) SendTwoFactorRequest(randomCodes string, userDetails
}

// SendTwoFactorRequest sends two factor mail
func (emSrv *EmailService) SendEmailLoginRequest(randomCodes string, userDetails models.User) error {
func (emSrv *emailService) SendEmailLoginRequest(randomCodes string, userDetails models.User) error {
var twoFactorRequestTemplateBuffer bytes.Buffer
// Get email template from directory and assign random code to it
emailTemplateFile, err := template.ParseFiles("static/email_templates/EmailLogin.html")
Expand All @@ -105,7 +105,7 @@ func (emSrv *EmailService) SendEmailLoginRequest(randomCodes string, userDetails

// SendPasswordRequest
// Sends a password request mail to the receiver
func (emSrv *EmailService) SendPasswordResetRequest(randomCodes string, userDetails models.User) error {
func (emSrv *emailService) SendPasswordResetRequest(randomCodes string, userDetails models.User) error {
var passwordResetTemplateBuffer bytes.Buffer
// Get email template from directory and assign random code to it
emailTemplateFile, err := template.ParseFiles("static/email_templates/PasswordRequest.html")
Expand Down
26 changes: 13 additions & 13 deletions internal/services/user_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"golang.org/x/crypto/bcrypt"
)

type UserServiceInterface interface {
type UserService interface {
List(offset int, limit int) ([]models.User, error)
Get(userId int) *models.User
GetByUsername(username string) *models.User
Expand All @@ -25,18 +25,18 @@ type UserServiceInterface interface {
Register(userRegistrationRequest models.UserRegistrationRequest) (bool, error)
}

type UserService struct {
type userService struct {
db *sql.DB
}

func NewUserService(db *sql.DB) *UserService {
return &UserService{
func NewUserService(db *sql.DB) UserService {
return &userService{
db: db,
}
}

// List a bunch of users
func (usrSrv *UserService) List(offset int, limit int) ([]models.User, error) {
func (usrSrv *userService) List(offset int, limit int) ([]models.User, error) {
users := make([]models.User, 0)
// Get the list of users
queryString :=
Expand Down Expand Up @@ -73,7 +73,7 @@ func (usrSrv *UserService) List(offset int, limit int) ([]models.User, error) {
}

// Get user details based on ID
func (usrSrv *UserService) Get(userId int) *models.User {
func (usrSrv *userService) Get(userId int) *models.User {
userDetails := &models.User{}
queryString :=
`SELECT
Expand Down Expand Up @@ -112,15 +112,15 @@ func (usrSrv *UserService) Get(userId int) *models.User {
}

// GetUsername gets the usersDetails by username
func (usrSrv *UserService) GetByUsername(username string) *models.User {
func (usrSrv *userService) GetByUsername(username string) *models.User {
var userId int
row := usrSrv.db.QueryRow("SELECT id FROM users WHERE username = $1 OR email_address = $1 LIMIT 1 ", username)
row.Scan(&userId)
return usrSrv.Get(userId)
}

// Register a new user
func (usrSrv *UserService) Register(userRegistrationRequest models.UserRegistrationRequest) (bool, error) {
func (usrSrv *userService) Register(userRegistrationRequest models.UserRegistrationRequest) (bool, error) {
// Check passwor strength
if !utilities.StrongPasswordCheck(userRegistrationRequest.Password) {
return false, ErrStrongPassword
Expand Down Expand Up @@ -165,7 +165,7 @@ func (usrSrv *UserService) Register(userRegistrationRequest models.UserRegistrat
}

// GetRoles gets a list of user roles
func (usrSrv *UserService) GetRoles(userId int) ([]string, error) {
func (usrSrv *userService) GetRoles(userId int) ([]string, error) {
roles := []string{}
// Get user roles
queryString := `
Expand All @@ -192,7 +192,7 @@ func (usrSrv *UserService) GetRoles(userId int) ([]string, error) {
}

// Update User
func (usrSrv *UserService) Update(userId int, userUpdateRequest models.UserUpdateRequest) error {
func (usrSrv *userService) Update(userId int, userUpdateRequest models.UserUpdateRequest) error {
query := "UPDATE users SET "
var args []any
argCount := 1
Expand Down Expand Up @@ -241,7 +241,7 @@ func (usrSrv *UserService) Update(userId int, userUpdateRequest models.UserUpdat
}

// DeleteToken function to delete refresh Token
func (usrSrv *UserService) DeleteToken(userId int, refreshToken string) (bool, error) {
func (usrSrv *userService) DeleteToken(userId int, refreshToken string) (bool, error) {
if _, err := usrSrv.db.Exec("DELETE FROM user_refresh_tokens WHERE token = $1 AND user_id = $2", refreshToken, userId); err != nil {
log.Println(err)
return false, err
Expand All @@ -250,7 +250,7 @@ func (usrSrv *UserService) DeleteToken(userId int, refreshToken string) (bool, e
}

// EnableTOTP
func (usrSrv *UserService) EnableTwoFactorTOTP(userId int) (*models.EnableTOTPResponse, error) {
func (usrSrv *userService) EnableTwoFactorTOTP(userId int) (*models.EnableTOTPResponse, error) {
userDetails := usrSrv.Get(userId)
if userDetails.TwoFactorMethod == "TOTP" {
return nil, ErrTOTPExists
Expand Down Expand Up @@ -284,7 +284,7 @@ func (usrSrv *UserService) EnableTwoFactorTOTP(userId int) (*models.EnableTOTPRe
}

// EnableTwoFactor SMS OR EMAIL
func (usrSrv *UserService) EnableTwoFactor(userId int, methodCode string) error {
func (usrSrv *userService) EnableTwoFactor(userId int, methodCode string) error {
queryString :=
`UPDATE
users
Expand Down

0 comments on commit 81ebb33

Please sign in to comment.