Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.git
.gitignore
docs/
*.md
LICENSE
VERSION
dist/
*.json
*.exe
16 changes: 12 additions & 4 deletions internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ func New(secret string) *AuthService {
if secret == "" {
// Generate a random secret
b := make([]byte, 32)
rand.Read(b)
if _, err := rand.Read(b); err != nil {
panic(fmt.Sprintf("crypto/rand failed: %v", err))
}
secret = hex.EncodeToString(b)
}
return &AuthService{secret: secret}
Expand All @@ -27,15 +29,19 @@ func New(secret string) *AuthService {
// GenerateToken creates a new bearer token.
func (a *AuthService) GenerateToken(workspace string) string {
b := make([]byte, 24)
rand.Read(b)
if _, err := rand.Read(b); err != nil {
panic(fmt.Sprintf("crypto/rand failed: %v", err))
}
h := sha256.Sum256(append(b, []byte(a.secret)...))
return "tp_" + hex.EncodeToString(h[:16])
}

// GenerateOTP creates a 6-digit OTP code.
func (a *AuthService) GenerateOTP() string {
b := make([]byte, 4)
rand.Read(b)
if _, err := rand.Read(b); err != nil {
panic(fmt.Sprintf("crypto/rand failed: %v", err))
}
val := uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3])
code := fmt.Sprintf("%06d", val%1000000)
return code
Expand All @@ -45,7 +51,9 @@ func (a *AuthService) GenerateOTP() string {
func GenerateHandle(prefix string) string {
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
b := make([]byte, 5)
rand.Read(b)
if _, err := rand.Read(b); err != nil {
panic(fmt.Sprintf("crypto/rand failed: %v", err))
}
for i := range b {
b[i] = charset[int(b[i])%len(charset)]
}
Expand Down
Loading