35 lines
857 B
Go
35 lines
857 B
Go
|
package services
|
||
|
|
||
|
import (
|
||
|
"crypto/rand"
|
||
|
"encoding/hex"
|
||
|
"fmt"
|
||
|
)
|
||
|
|
||
|
// size in bytes of the client ids and secrets
|
||
|
const idSecretSize = 32
|
||
|
|
||
|
func generateRandomHex(size int) (string, error) {
|
||
|
raw := make([]byte, size)
|
||
|
n, err := rand.Read(raw)
|
||
|
if err != nil {
|
||
|
return "", fmt.Errorf("failed to read from random generator: %w", err)
|
||
|
}
|
||
|
if n != size {
|
||
|
return "", fmt.Errorf("failed to read from random generator (%d/%d)", n, size)
|
||
|
}
|
||
|
return hex.EncodeToString(raw), nil
|
||
|
}
|
||
|
|
||
|
func GenerateClientIDSecret() (string, string, error) {
|
||
|
clientID, err := generateRandomHex(idSecretSize)
|
||
|
if err != nil {
|
||
|
return "", "", fmt.Errorf("failed to generate client id: %w", err)
|
||
|
}
|
||
|
clientSecret, err := generateRandomHex(idSecretSize)
|
||
|
if err != nil {
|
||
|
return "", "", fmt.Errorf("failed to generate client secret: %w", err)
|
||
|
}
|
||
|
return clientID, clientSecret, nil
|
||
|
}
|