Skip to content

Commit

Permalink
feat: OAuth device flow for user authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Mar 24, 2021
1 parent 7e8141a commit 9ff6ce2
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 9ff6ce2

Please sign in to comment.