Skip to content

Commit 2fbdd87

Browse files
authored
Fix device code verification URI unmarshalling (#556)
1 parent 51fa822 commit 2fbdd87

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

apps/internal/oauth/ops/accesstokens/accesstokens.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ type DeviceCodeResponse struct {
6868

6969
UserCode string `json:"user_code"`
7070
DeviceCode string `json:"device_code"`
71-
VerificationURL string `json:"verification_url"`
71+
VerificationURL string `json:"verification_uri"`
7272
ExpiresIn int `json:"expires_in"`
7373
Interval int `json:"interval"`
7474
Message string `json:"message"`

apps/public/public_test.go

+79
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,85 @@ func TestAcquireTokenSilentWithoutAccount(t *testing.T) {
198198
}
199199
}
200200

201+
func TestAcquireTokenByDeviceCode(t *testing.T) {
202+
accessToken := "*"
203+
expected := accesstokens.DeviceCodeResult{
204+
ClientID: "client-id",
205+
DeviceCode: "device-code",
206+
Message: "msg",
207+
Interval: 1,
208+
Scopes: tokenScope,
209+
UserCode: "user-code",
210+
VerificationURL: "https://device.code.local",
211+
}
212+
mockClient := mock.Client{}
213+
mockClient.AppendResponse(mock.WithBody(mock.GetTenantDiscoveryBody("http://localhost", "tenant")))
214+
mockClient.AppendResponse(mock.WithBody([]byte(
215+
fmt.Sprintf(
216+
`{"device_code":%q,"expires_in":60,"interval":%d,"message":%q,"user_code":%q,"verification_uri":%q}`,
217+
expected.DeviceCode,
218+
expected.Interval,
219+
expected.Message,
220+
expected.UserCode,
221+
expected.VerificationURL,
222+
),
223+
)))
224+
mockClient.AppendResponse(
225+
mock.WithBody(mock.GetAccessTokenBody(accessToken, "", "rt", "", 3600)),
226+
mock.WithCallback(func(r *http.Request) {
227+
if r.Method != http.MethodPost {
228+
t.Fatalf("unexpected method %q", r.Method)
229+
}
230+
if err := r.ParseForm(); err != nil {
231+
t.Fatal(err)
232+
}
233+
if v := r.Form.Get("client_id"); v != expected.ClientID {
234+
t.Fatalf("unexpected client_id %q", v)
235+
}
236+
if v := r.Form.Get("device_code"); v != expected.DeviceCode {
237+
t.Fatalf("unexpected device_code %q", v)
238+
}
239+
}),
240+
)
241+
client, err := New(expected.ClientID, WithHTTPClient(&mockClient))
242+
if err != nil {
243+
t.Fatal(err)
244+
}
245+
dc, err := client.AcquireTokenByDeviceCode(context.Background(), tokenScope)
246+
if err != nil {
247+
t.Fatal(err)
248+
}
249+
actual := dc.Result
250+
if actual.ClientID != expected.ClientID {
251+
t.Fatalf("unexpected client ID %q", actual.ClientID)
252+
}
253+
if actual.DeviceCode != expected.DeviceCode {
254+
t.Fatalf("unexpected device code %q", actual.DeviceCode)
255+
}
256+
if !actual.ExpiresOn.After(time.Now()) {
257+
t.Fatalf("expected a future expiration time but got %v", actual.ExpiresOn)
258+
}
259+
if actual.Interval != expected.Interval {
260+
t.Fatalf("unexpected interval %d", actual.Interval)
261+
}
262+
if actual.Message != expected.Message {
263+
t.Fatalf("unexpected message %q", actual.Message)
264+
}
265+
if actual.UserCode != expected.UserCode {
266+
t.Fatalf("unexpected user code %q", actual.UserCode)
267+
}
268+
if actual.VerificationURL != expected.VerificationURL {
269+
t.Fatalf("unexpected verification URL %q", actual.VerificationURL)
270+
}
271+
ar, err := dc.AuthenticationResult(context.Background())
272+
if err != nil {
273+
t.Fatal(err)
274+
}
275+
if ar.AccessToken != accessToken {
276+
t.Fatalf("unexpected access token %q", ar.AccessToken)
277+
}
278+
}
279+
201280
func TestAcquireTokenWithTenantID(t *testing.T) {
202281
accessToken := "*"
203282
clientInfo := base64.RawStdEncoding.EncodeToString([]byte(`{"uid":"uid","utid":"utid"}`))

0 commit comments

Comments
 (0)