Files
services/password/handler/password.go
2022-03-02 10:11:46 +00:00

50 lines
949 B
Go

package handler
import (
"context"
"crypto/rand"
pb "github.com/micro/services/password/proto"
)
var (
minLength = 8
special = "!@#$%&*"
numbers = "0123456789"
lowercase = "abcdefghijklmnopqrstuvwxyz"
uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
allChars = special + numbers + lowercase + uppercase
)
func random(chars string, i int) string {
bytes := make([]byte, i)
for {
rand.Read(bytes)
for i, b := range bytes {
bytes[i] = chars[b%byte(len(chars))]
}
break
}
return string(bytes)
}
type Password struct{}
func (p *Password) Generate(ctx context.Context, req *pb.GenerateRequest, rsp *pb.GenerateResponse) error {
if req.Length <= 0 {
req.Length = int32(minLength)
}
// TODO; allow user to specify types of characters
// generate and return password
rsp.Password = random(allChars, int(req.Length))
return nil
}