fix device test (drop read error)

This commit is contained in:
Tim Möhlmann 2025-03-14 14:22:54 +02:00
parent f5e0db7ad5
commit c95b248de8
2 changed files with 12 additions and 22 deletions

View file

@ -91,10 +91,7 @@ func createDeviceAuthorization(ctx context.Context, req *oidc.DeviceAuthorizatio
} }
config := o.DeviceAuthorization() config := o.DeviceAuthorization()
deviceCode, err := NewDeviceCode(RecommendedDeviceCodeBytes) deviceCode, _ := NewDeviceCode(RecommendedDeviceCodeBytes)
if err != nil {
return nil, NewStatusError(err, http.StatusInternalServerError)
}
userCode, err := NewUserCode([]rune(config.UserCode.CharSet), config.UserCode.CharAmount, config.UserCode.DashInterval) userCode, err := NewUserCode([]rune(config.UserCode.CharSet), config.UserCode.CharAmount, config.UserCode.DashInterval)
if err != nil { if err != nil {
return nil, NewStatusError(err, http.StatusInternalServerError) return nil, NewStatusError(err, http.StatusInternalServerError)
@ -163,11 +160,14 @@ func ParseDeviceCodeRequest(r *http.Request, o OpenIDProvider) (*oidc.DeviceAuth
// results in a 22 character base64 encoded string. // results in a 22 character base64 encoded string.
const RecommendedDeviceCodeBytes = 16 const RecommendedDeviceCodeBytes = 16
// NewDeviceCode generates a new cryptographically secure device code as a base64 encoded string.
// The length of the string is nBytes * 4 / 3.
// An error is never returned.
//
// TODO(v4): change return type to string alone.
func NewDeviceCode(nBytes int) (string, error) { func NewDeviceCode(nBytes int) (string, error) {
bytes := make([]byte, nBytes) bytes := make([]byte, nBytes)
if _, err := rand.Read(bytes); err != nil { rand.Read(bytes)
return "", fmt.Errorf("%w getting entropy for device code", err)
}
return base64.RawURLEncoding.EncodeToString(bytes), nil return base64.RawURLEncoding.EncodeToString(bytes), nil
} }

View file

@ -145,21 +145,11 @@ func runWithRandReader(r io.Reader, f func()) {
} }
func TestNewDeviceCode(t *testing.T) { func TestNewDeviceCode(t *testing.T) {
t.Run("reader error", func(t *testing.T) {
runWithRandReader(errReader{}, func() {
_, err := op.NewDeviceCode(16)
require.Error(t, err)
})
})
t.Run("different lengths, rand reader", func(t *testing.T) {
for i := 1; i <= 32; i++ { for i := 1; i <= 32; i++ {
got, err := op.NewDeviceCode(i) got, err := op.NewDeviceCode(i)
require.NoError(t, err) require.NoError(t, err)
assert.Len(t, got, base64.RawURLEncoding.EncodedLen(i)) assert.Len(t, got, base64.RawURLEncoding.EncodedLen(i))
} }
})
} }
func TestNewUserCode(t *testing.T) { func TestNewUserCode(t *testing.T) {