Skip to content

Instantly share code, notes, and snippets.

@alok87

alok87/user.go Secret

Last active August 4, 2022 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alok87/8dd2f9bf08d8c4a47b1e01dd16144434 to your computer and use it in GitHub Desktop.
Save alok87/8dd2f9bf08d8c4a47b1e01dd16144434 to your computer and use it in GitHub Desktop.
internal/user.go
package user
import (
"context"
"fmt"
v1 "github.com/alok87/config/internal/api/v1"
metav1 "github.com/alok87/config/internal/meta/v1"
"golang.org/x/crypto/bcrypt"
)
type User interface {
Create(ctx context.Context, username, pass, role string) (*v1.User, error)
}
func New() User { return &user{} }
type user struct{}
func (u *user) Create(ctx context.Context, username, password string, role string) (*v1.User, error) {
hashedPassword, err := bcrypt.GenerateFromPassword(
[]byte(password),
bcrypt.DefaultCost,
)
if err != nil {
return nil, fmt.Errorf("error hashing password: %w", err)
}
user := &v1.User{
ObjectMeta: metav1.ObjectMeta{
Name: username,
},
Spec: v1.UserSpec{
HashedPassword: string(hashedPassword),
Role: role,
},
}
return user, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment