From 9ff6ce2af5a0ab2138cab6c81d79dea749eceef1 Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Tue, 23 Mar 2021 16:58:31 -0700 Subject: [PATCH] feat: OAuth device flow for user authentication --- test/index.test.ts | 87 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/test/index.test.ts b/test/index.test.ts index 5ff2750f..59ec0e3b 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -803,6 +803,93 @@ test("oauth-user web flow", async () => { `); }); +test("oauth-user device flow", async () => { + const mock = fetchMock + .sandbox() + + .postOnce( + "https://github.com/login/device/code", + { + device_code: "devicecode123", + user_code: "usercode123", + verification_uri: "https://github.com/login/device", + expires_in: 900, + // use low number because jest.useFakeTimers() & jest.runAllTimers() didn't work for me + interval: 0.005, + }, + { + headers: { + accept: "application/json", + "user-agent": "test", + "content-type": "application/json; charset=utf-8", + }, + body: { + client_id: "lv1.1234567890abcdef", + }, + } + ) + .postOnce( + "https://github.com/login/oauth/access_token", + { + access_token: "token123", + scope: "", + }, + { + headers: { + accept: "application/json", + "user-agent": "test", + "content-type": "application/json; charset=utf-8", + }, + body: { + client_id: "lv1.1234567890abcdef", + device_code: "devicecode123", + grant_type: "urn:ietf:params:oauth:grant-type:device_code", + }, + overwriteRoutes: false, + } + ); + + const auth = createAppAuth({ + appId: APP_ID, + privateKey: PRIVATE_KEY, + clientId: "lv1.1234567890abcdef", + clientSecret: "1234567890abcdef1234567890abcdef12345678", + request: request.defaults({ + headers: { + "user-agent": "test", + }, + request: { + fetch: mock, + }, + }), + }); + + const onVerification = jest.fn(); + const authentication = await auth({ + type: "oauth-user", + onVerification, + }); + + expect(authentication).toMatchInlineSnapshot(` + Object { + "clientId": "lv1.1234567890abcdef", + "clientSecret": "1234567890abcdef1234567890abcdef12345678", + "clientType": "github-app", + "token": "token123", + "tokenType": "oauth", + "type": "token", + } + `); + + expect(onVerification).toHaveBeenCalledWith({ + device_code: "devicecode123", + expires_in: 900, + interval: 0.005, + user_code: "usercode123", + verification_uri: "https://github.com/login/device", + }); +}); + test("caches based on installation id", async () => { const createInstallationAccessTokenResponseData = { token: "secret123",