| 1 |
package op_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"crypto/rand" |
| 6 |
"encoding/base64" |
| 7 |
"io" |
| 8 |
mr "math/rand" |
| 9 |
"net/http" |
| 10 |
"net/http/httptest" |
| 11 |
"net/url" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
"time" |
| 15 |
|
| 16 |
"github.com/muhlemmer/gu" |
| 17 |
"github.com/stretchr/testify/assert" |
| 18 |
"github.com/stretchr/testify/require" |
| 19 |
"github.com/zitadel/oidc/v3/example/server/storage" |
| 20 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 21 |
"github.com/zitadel/oidc/v3/pkg/op" |
| 22 |
) |
| 23 |
|
| 24 |
func Test_deviceAuthorizationHandler(t *testing.T) { |
| 25 |
type conf struct { |
| 26 |
UserFormURL string |
| 27 |
UserFormPath string |
| 28 |
} |
| 29 |
tests := []struct { |
| 30 |
name string |
| 31 |
conf conf |
| 32 |
}{ |
| 33 |
{ |
| 34 |
name: "UserFormURL", |
| 35 |
conf: conf{ |
| 36 |
UserFormURL: "https://localhost:9998/device", |
| 37 |
}, |
| 38 |
}, |
| 39 |
{ |
| 40 |
name: "UserFormPath", |
| 41 |
conf: conf{ |
| 42 |
UserFormPath: "/device", |
| 43 |
}, |
| 44 |
}, |
| 45 |
} |
| 46 |
for _, tt := range tests { |
| 47 |
t.Run(tt.name, func(t *testing.T) { |
| 48 |
conf := gu.PtrCopy(testConfig) |
| 49 |
conf.DeviceAuthorization.UserFormURL = tt.conf.UserFormURL |
| 50 |
conf.DeviceAuthorization.UserFormPath = tt.conf.UserFormPath |
| 51 |
provider := newTestProvider(conf) |
| 52 |
|
| 53 |
req := &oidc.DeviceAuthorizationRequest{ |
| 54 |
Scopes: []string{"foo", "bar"}, |
| 55 |
ClientID: "device", |
| 56 |
} |
| 57 |
values := make(url.Values) |
| 58 |
testProvider.Encoder().Encode(req, values) |
| 59 |
body := strings.NewReader(values.Encode()) |
| 60 |
|
| 61 |
r := httptest.NewRequest(http.MethodPost, "/", body) |
| 62 |
r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 63 |
r = r.WithContext(op.ContextWithIssuer(r.Context(), testIssuer)) |
| 64 |
|
| 65 |
w := httptest.NewRecorder() |
| 66 |
|
| 67 |
runWithRandReader(mr.New(mr.NewSource(1)), func() { |
| 68 |
op.DeviceAuthorizationHandler(provider)(w, r) |
| 69 |
}) |
| 70 |
|
| 71 |
result := w.Result() |
| 72 |
|
| 73 |
assert.Less(t, result.StatusCode, 300) |
| 74 |
|
| 75 |
got, _ := io.ReadAll(result.Body) |
| 76 |
assert.JSONEq(t, `{"device_code":"Uv38ByGCZU8WP18PmmIdcg", "expires_in":300, "interval":5, "user_code":"JKRV-FRGK", "verification_uri":"https://localhost:9998/device", "verification_uri_complete":"https://localhost:9998/device?user_code=JKRV-FRGK"}`, string(got)) |
| 77 |
}) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
func TestParseDeviceCodeRequest(t *testing.T) { |
| 82 |
tests := []struct { |
| 83 |
name string |
| 84 |
req *oidc.DeviceAuthorizationRequest |
| 85 |
wantErr bool |
| 86 |
}{ |
| 87 |
{ |
| 88 |
name: "empty request", |
| 89 |
wantErr: true, |
| 90 |
}, |
| 91 |
{ |
| 92 |
name: "missing grant type", |
| 93 |
req: &oidc.DeviceAuthorizationRequest{ |
| 94 |
Scopes: oidc.SpaceDelimitedArray{"foo", "bar"}, |
| 95 |
ClientID: "web", |
| 96 |
}, |
| 97 |
wantErr: true, |
| 98 |
}, |
| 99 |
{ |
| 100 |
name: "client not found", |
| 101 |
req: &oidc.DeviceAuthorizationRequest{ |
| 102 |
Scopes: oidc.SpaceDelimitedArray{"foo", "bar"}, |
| 103 |
ClientID: "foobar", |
| 104 |
}, |
| 105 |
wantErr: true, |
| 106 |
}, |
| 107 |
{ |
| 108 |
name: "success", |
| 109 |
req: &oidc.DeviceAuthorizationRequest{ |
| 110 |
Scopes: oidc.SpaceDelimitedArray{"foo", "bar"}, |
| 111 |
ClientID: "device", |
| 112 |
}, |
| 113 |
}, |
| 114 |
} |
| 115 |
for _, tt := range tests { |
| 116 |
t.Run(tt.name, func(t *testing.T) { |
| 117 |
var body io.Reader |
| 118 |
if tt.req != nil { |
| 119 |
values := make(url.Values) |
| 120 |
testProvider.Encoder().Encode(tt.req, values) |
| 121 |
body = strings.NewReader(values.Encode()) |
| 122 |
} |
| 123 |
|
| 124 |
r := httptest.NewRequest(http.MethodPost, "/", body) |
| 125 |
r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 126 |
|
| 127 |
got, err := op.ParseDeviceCodeRequest(r, testProvider) |
| 128 |
if tt.wantErr { |
| 129 |
require.Error(t, err) |
| 130 |
return |
| 131 |
} |
| 132 |
assert.Equal(t, tt.req, got) |
| 133 |
}) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
func runWithRandReader(r io.Reader, f func()) { |
| 138 |
originalReader := rand.Reader |
| 139 |
rand.Reader = r |
| 140 |
defer func() { |
| 141 |
rand.Reader = originalReader |
| 142 |
}() |
| 143 |
|
| 144 |
f() |
| 145 |
} |
| 146 |
|
| 147 |
func TestNewDeviceCode(t *testing.T) { |
| 148 |
for i := 1; i <= 32; i++ { |
| 149 |
got, err := op.NewDeviceCode(i) |
| 150 |
require.NoError(t, err) |
| 151 |
assert.Len(t, got, base64.RawURLEncoding.EncodedLen(i)) |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
func TestNewUserCode(t *testing.T) { |
| 156 |
type args struct { |
| 157 |
charset []rune |
| 158 |
charAmount int |
| 159 |
dashInterval int |
| 160 |
} |
| 161 |
tests := []struct { |
| 162 |
name string |
| 163 |
args args |
| 164 |
reader io.Reader |
| 165 |
want string |
| 166 |
wantErr bool |
| 167 |
}{ |
| 168 |
{ |
| 169 |
name: "reader error", |
| 170 |
args: args{ |
| 171 |
charset: []rune(op.CharSetBase20), |
| 172 |
charAmount: 8, |
| 173 |
dashInterval: 4, |
| 174 |
}, |
| 175 |
reader: errReader{}, |
| 176 |
wantErr: true, |
| 177 |
}, |
| 178 |
{ |
| 179 |
name: "base20", |
| 180 |
args: args{ |
| 181 |
charset: []rune(op.CharSetBase20), |
| 182 |
charAmount: 8, |
| 183 |
dashInterval: 4, |
| 184 |
}, |
| 185 |
reader: mr.New(mr.NewSource(1)), |
| 186 |
want: "XKCD-HTTD", |
| 187 |
}, |
| 188 |
{ |
| 189 |
name: "digits", |
| 190 |
args: args{ |
| 191 |
charset: []rune(op.CharSetDigits), |
| 192 |
charAmount: 9, |
| 193 |
dashInterval: 3, |
| 194 |
}, |
| 195 |
reader: mr.New(mr.NewSource(1)), |
| 196 |
want: "271-256-225", |
| 197 |
}, |
| 198 |
{ |
| 199 |
name: "no dashes", |
| 200 |
args: args{ |
| 201 |
charset: []rune(op.CharSetDigits), |
| 202 |
charAmount: 9, |
| 203 |
}, |
| 204 |
reader: mr.New(mr.NewSource(1)), |
| 205 |
want: "271256225", |
| 206 |
}, |
| 207 |
} |
| 208 |
for _, tt := range tests { |
| 209 |
t.Run(tt.name, func(t *testing.T) { |
| 210 |
runWithRandReader(tt.reader, func() { |
| 211 |
got, err := op.NewUserCode(tt.args.charset, tt.args.charAmount, tt.args.dashInterval) |
| 212 |
if tt.wantErr { |
| 213 |
require.ErrorIs(t, err, io.ErrNoProgress) |
| 214 |
} else { |
| 215 |
require.NoError(t, err) |
| 216 |
} |
| 217 |
assert.Equal(t, tt.want, got) |
| 218 |
}) |
| 219 |
|
| 220 |
}) |
| 221 |
} |
| 222 |
|
| 223 |
t.Run("crypto/rand", func(t *testing.T) { |
| 224 |
const testN = 100000 |
| 225 |
|
| 226 |
for _, c := range []op.UserCodeConfig{op.UserCodeBase20, op.UserCodeDigits} { |
| 227 |
t.Run(c.CharSet, func(t *testing.T) { |
| 228 |
results := make(map[string]int) |
| 229 |
|
| 230 |
for i := 0; i < testN; i++ { |
| 231 |
code, err := op.NewUserCode([]rune(c.CharSet), c.CharAmount, c.DashInterval) |
| 232 |
require.NoError(t, err) |
| 233 |
results[code]++ |
| 234 |
} |
| 235 |
|
| 236 |
t.Log(results) |
| 237 |
|
| 238 |
var duplicates int |
| 239 |
for code, count := range results { |
| 240 |
assert.Less(t, count, 3, code) |
| 241 |
if count == 2 { |
| 242 |
duplicates++ |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
}) |
| 247 |
} |
| 248 |
}) |
| 249 |
} |
| 250 |
|
| 251 |
func BenchmarkNewUserCode(b *testing.B) { |
| 252 |
type args struct { |
| 253 |
charset []rune |
| 254 |
charAmount int |
| 255 |
dashInterval int |
| 256 |
} |
| 257 |
tests := []struct { |
| 258 |
name string |
| 259 |
args args |
| 260 |
reader io.Reader |
| 261 |
}{ |
| 262 |
{ |
| 263 |
name: "math rand, base20", |
| 264 |
args: args{ |
| 265 |
charset: []rune(op.CharSetBase20), |
| 266 |
charAmount: 8, |
| 267 |
dashInterval: 4, |
| 268 |
}, |
| 269 |
reader: mr.New(mr.NewSource(1)), |
| 270 |
}, |
| 271 |
{ |
| 272 |
name: "math rand, digits", |
| 273 |
args: args{ |
| 274 |
charset: []rune(op.CharSetDigits), |
| 275 |
charAmount: 9, |
| 276 |
dashInterval: 3, |
| 277 |
}, |
| 278 |
reader: mr.New(mr.NewSource(1)), |
| 279 |
}, |
| 280 |
{ |
| 281 |
name: "crypto rand, base20", |
| 282 |
args: args{ |
| 283 |
charset: []rune(op.CharSetBase20), |
| 284 |
charAmount: 8, |
| 285 |
dashInterval: 4, |
| 286 |
}, |
| 287 |
reader: rand.Reader, |
| 288 |
}, |
| 289 |
{ |
| 290 |
name: "crypto rand, digits", |
| 291 |
args: args{ |
| 292 |
charset: []rune(op.CharSetDigits), |
| 293 |
charAmount: 9, |
| 294 |
dashInterval: 3, |
| 295 |
}, |
| 296 |
reader: rand.Reader, |
| 297 |
}, |
| 298 |
} |
| 299 |
for _, tt := range tests { |
| 300 |
runWithRandReader(tt.reader, func() { |
| 301 |
b.Run(tt.name, func(b *testing.B) { |
| 302 |
for i := 0; i < b.N; i++ { |
| 303 |
_, err := op.NewUserCode(tt.args.charset, tt.args.charAmount, tt.args.dashInterval) |
| 304 |
require.NoError(b, err) |
| 305 |
} |
| 306 |
}) |
| 307 |
|
| 308 |
}) |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
func TestDeviceAccessToken(t *testing.T) { |
| 313 |
storage := testProvider.Storage().(*storage.Storage) |
| 314 |
storage.StoreDeviceAuthorization(context.Background(), "native", "qwerty", "yuiop", time.Now().Add(time.Minute), []string{"foo"}) |
| 315 |
storage.CompleteDeviceAuthorization(context.Background(), "yuiop", "tim") |
| 316 |
|
| 317 |
values := make(url.Values) |
| 318 |
values.Set("client_id", "native") |
| 319 |
values.Set("grant_type", string(oidc.GrantTypeDeviceCode)) |
| 320 |
values.Set("device_code", "qwerty") |
| 321 |
|
| 322 |
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(values.Encode())) |
| 323 |
r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 324 |
w := httptest.NewRecorder() |
| 325 |
|
| 326 |
op.DeviceAccessToken(w, r, testProvider) |
| 327 |
|
| 328 |
result := w.Result() |
| 329 |
got, _ := io.ReadAll(result.Body) |
| 330 |
t.Log(string(got)) |
| 331 |
assert.Less(t, result.StatusCode, 300) |
| 332 |
assert.NotEmpty(t, string(got)) |
| 333 |
} |
| 334 |
|
| 335 |
func TestCheckDeviceAuthorizationState(t *testing.T) { |
| 336 |
now := time.Now() |
| 337 |
|
| 338 |
storage := testProvider.Storage().(*storage.Storage) |
| 339 |
storage.StoreDeviceAuthorization(context.Background(), "native", "pending", "pending", now.Add(time.Minute), []string{"foo"}) |
| 340 |
storage.StoreDeviceAuthorization(context.Background(), "native", "denied", "denied", now.Add(time.Minute), []string{"foo"}) |
| 341 |
storage.StoreDeviceAuthorization(context.Background(), "native", "completed", "completed", now.Add(time.Minute), []string{"foo"}) |
| 342 |
storage.StoreDeviceAuthorization(context.Background(), "native", "expired", "expired", now.Add(-time.Minute), []string{"foo"}) |
| 343 |
|
| 344 |
storage.DenyDeviceAuthorization(context.Background(), "denied") |
| 345 |
storage.CompleteDeviceAuthorization(context.Background(), "completed", "tim") |
| 346 |
|
| 347 |
exceededCtx, cancel := context.WithTimeout(context.Background(), -time.Second) |
| 348 |
defer cancel() |
| 349 |
|
| 350 |
type args struct { |
| 351 |
ctx context.Context |
| 352 |
clientID string |
| 353 |
deviceCode string |
| 354 |
} |
| 355 |
tests := []struct { |
| 356 |
name string |
| 357 |
args args |
| 358 |
want *op.DeviceAuthorizationState |
| 359 |
wantErr error |
| 360 |
}{ |
| 361 |
{ |
| 362 |
name: "pending", |
| 363 |
args: args{ |
| 364 |
ctx: context.Background(), |
| 365 |
clientID: "native", |
| 366 |
deviceCode: "pending", |
| 367 |
}, |
| 368 |
want: &op.DeviceAuthorizationState{ |
| 369 |
ClientID: "native", |
| 370 |
Scopes: []string{"foo"}, |
| 371 |
Expires: now.Add(time.Minute), |
| 372 |
}, |
| 373 |
wantErr: oidc.ErrAuthorizationPending(), |
| 374 |
}, |
| 375 |
{ |
| 376 |
name: "slow down", |
| 377 |
args: args{ |
| 378 |
ctx: exceededCtx, |
| 379 |
clientID: "native", |
| 380 |
deviceCode: "ok", |
| 381 |
}, |
| 382 |
wantErr: oidc.ErrSlowDown(), |
| 383 |
}, |
| 384 |
{ |
| 385 |
name: "wrong client", |
| 386 |
args: args{ |
| 387 |
ctx: context.Background(), |
| 388 |
clientID: "foo", |
| 389 |
deviceCode: "ok", |
| 390 |
}, |
| 391 |
wantErr: oidc.ErrAccessDenied(), |
| 392 |
}, |
| 393 |
{ |
| 394 |
name: "denied", |
| 395 |
args: args{ |
| 396 |
ctx: context.Background(), |
| 397 |
clientID: "native", |
| 398 |
deviceCode: "denied", |
| 399 |
}, |
| 400 |
want: &op.DeviceAuthorizationState{ |
| 401 |
ClientID: "native", |
| 402 |
Scopes: []string{"foo"}, |
| 403 |
Expires: now.Add(time.Minute), |
| 404 |
Denied: true, |
| 405 |
}, |
| 406 |
wantErr: oidc.ErrAccessDenied(), |
| 407 |
}, |
| 408 |
{ |
| 409 |
name: "completed", |
| 410 |
args: args{ |
| 411 |
ctx: context.Background(), |
| 412 |
clientID: "native", |
| 413 |
deviceCode: "completed", |
| 414 |
}, |
| 415 |
want: &op.DeviceAuthorizationState{ |
| 416 |
ClientID: "native", |
| 417 |
Scopes: []string{"foo"}, |
| 418 |
Expires: now.Add(time.Minute), |
| 419 |
Subject: "tim", |
| 420 |
Done: true, |
| 421 |
}, |
| 422 |
}, |
| 423 |
{ |
| 424 |
name: "expired", |
| 425 |
args: args{ |
| 426 |
ctx: context.Background(), |
| 427 |
clientID: "native", |
| 428 |
deviceCode: "expired", |
| 429 |
}, |
| 430 |
want: &op.DeviceAuthorizationState{ |
| 431 |
ClientID: "native", |
| 432 |
Scopes: []string{"foo"}, |
| 433 |
Expires: now.Add(-time.Minute), |
| 434 |
}, |
| 435 |
wantErr: oidc.ErrExpiredDeviceCode(), |
| 436 |
}, |
| 437 |
} |
| 438 |
for _, tt := range tests { |
| 439 |
t.Run(tt.name, func(t *testing.T) { |
| 440 |
got, err := op.CheckDeviceAuthorizationState(tt.args.ctx, tt.args.clientID, tt.args.deviceCode, testProvider) |
| 441 |
require.ErrorIs(t, err, tt.wantErr) |
| 442 |
assert.Equal(t, tt.want, got) |
| 443 |
}) |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
func TestCreateDeviceTokenResponse(t *testing.T) { |
| 448 |
tests := []struct { |
| 449 |
name string |
| 450 |
tokenRequest op.TokenRequest |
| 451 |
wantAccessToken bool |
| 452 |
wantRefreshToken bool |
| 453 |
wantIDToken bool |
| 454 |
wantErr bool |
| 455 |
}{ |
| 456 |
{ |
| 457 |
name: "access token", |
| 458 |
tokenRequest: &op.DeviceAuthorizationState{ |
| 459 |
ClientID: "client1", |
| 460 |
Subject: "id1", |
| 461 |
AMR: []string{"password"}, |
| 462 |
AuthTime: time.Now(), |
| 463 |
}, |
| 464 |
wantAccessToken: true, |
| 465 |
}, |
| 466 |
{ |
| 467 |
name: "access and refresh tokens", |
| 468 |
tokenRequest: &op.DeviceAuthorizationState{ |
| 469 |
ClientID: "client1", |
| 470 |
Subject: "id1", |
| 471 |
AMR: []string{"password"}, |
| 472 |
AuthTime: time.Now(), |
| 473 |
Scopes: []string{oidc.ScopeOfflineAccess}, |
| 474 |
}, |
| 475 |
wantAccessToken: true, |
| 476 |
wantRefreshToken: true, |
| 477 |
}, |
| 478 |
{ |
| 479 |
name: "access and id token", |
| 480 |
tokenRequest: &op.DeviceAuthorizationState{ |
| 481 |
ClientID: "client1", |
| 482 |
Subject: "id1", |
| 483 |
AMR: []string{"password"}, |
| 484 |
AuthTime: time.Now(), |
| 485 |
Scopes: []string{oidc.ScopeOpenID}, |
| 486 |
}, |
| 487 |
wantAccessToken: true, |
| 488 |
wantIDToken: true, |
| 489 |
}, |
| 490 |
{ |
| 491 |
name: "access, refresh and id token", |
| 492 |
tokenRequest: &op.DeviceAuthorizationState{ |
| 493 |
ClientID: "client1", |
| 494 |
Subject: "id1", |
| 495 |
AMR: []string{"password"}, |
| 496 |
AuthTime: time.Now(), |
| 497 |
Scopes: []string{oidc.ScopeOfflineAccess, oidc.ScopeOpenID}, |
| 498 |
}, |
| 499 |
wantAccessToken: true, |
| 500 |
wantRefreshToken: true, |
| 501 |
wantIDToken: true, |
| 502 |
}, |
| 503 |
{ |
| 504 |
name: "id token creation error", |
| 505 |
tokenRequest: &op.DeviceAuthorizationState{ |
| 506 |
ClientID: "client1", |
| 507 |
Subject: "foobar", |
| 508 |
AMR: []string{"password"}, |
| 509 |
AuthTime: time.Now(), |
| 510 |
Scopes: []string{oidc.ScopeOfflineAccess, oidc.ScopeOpenID}, |
| 511 |
}, |
| 512 |
wantErr: true, |
| 513 |
}, |
| 514 |
} |
| 515 |
for _, tt := range tests { |
| 516 |
t.Run(tt.name, func(t *testing.T) { |
| 517 |
client, err := testProvider.Storage().GetClientByClientID(context.Background(), "native") |
| 518 |
require.NoError(t, err) |
| 519 |
|
| 520 |
got, err := op.CreateDeviceTokenResponse(context.Background(), tt.tokenRequest, testProvider, client) |
| 521 |
if tt.wantErr { |
| 522 |
require.Error(t, err) |
| 523 |
return |
| 524 |
} |
| 525 |
require.NoError(t, err) |
| 526 |
assert.InDelta(t, 300, got.ExpiresIn, 2) |
| 527 |
if tt.wantAccessToken { |
| 528 |
assert.NotEmpty(t, got.AccessToken, "access token") |
| 529 |
} |
| 530 |
if tt.wantRefreshToken { |
| 531 |
assert.NotEmpty(t, got.RefreshToken, "refresh token") |
| 532 |
} |
| 533 |
if tt.wantIDToken { |
| 534 |
assert.NotEmpty(t, got.IDToken, "id token") |
| 535 |
} |
| 536 |
}) |
| 537 |
} |
| 538 |
} |