-
-
Save alok87/8dd2f9bf08d8c4a47b1e01dd16144434 to your computer and use it in GitHub Desktop.
internal/user.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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